Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make config.yaml and values.yaml optional #100

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 21 additions & 29 deletions k8t/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,37 @@ def get_base_dir(root: str, cluster: str, environment: str) -> str:

# pylint: disable=too-many-arguments
def find_files(root: str, cluster: str, environment: str, name: str, file_ok=True, dir_ok=True) -> List[str]:
def check(path):
return (file_ok and os.path.isfile(path)) or (dir_ok and os.path.isdir(root_path))
def append_path(dir_path: str):
path = os.path.join(dir_path, name)
if (file_ok and os.path.isfile(path)) or (dir_ok and os.path.isdir(path)):
paths.append(path)

files: List[str] = []

root_path = os.path.join(root, name)

if check(root_path):
files.append(root_path)
paths: List[str] = []

env_found = environment is None
cluster_found = cluster is None

if environment is not None:
file_path = os.path.join(root, "environments", environment, name)
append_path(root)

if check(file_path):
files.append(file_path)
env_found = True
if environment:
environment_path = os.path.join(root, "environments", environment)
env_found = os.path.isdir(environment_path)
append_path(environment_path)

if cluster is not None:
if cluster:
cluster_path = os.path.join(root, "clusters", cluster)
cluster_found = os.path.isdir(cluster_path)
append_path(cluster_path)

if not os.path.isdir(cluster_path):
raise RuntimeError("no such cluster: {}".format(cluster))

file_path = os.path.join(cluster_path, name)

if check(file_path):
files.append(file_path)

if environment is not None:
file_path = os.path.join(
cluster_path, "environments", environment, name)

if check(file_path):
files.append(file_path)
env_found = True
if cluster and environment:
cluster_environment_path = os.path.join(root, "clusters", cluster, "environments", environment)
env_found = env_found or os.path.isdir(cluster_environment_path)
append_path(cluster_environment_path)

if not cluster_found:
raise RuntimeError("no such cluster: {}".format(cluster))
if not env_found:
raise RuntimeError("no such environment: {}".format(environment))

return files
return paths
19 changes: 19 additions & 0 deletions tests/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,5 +383,24 @@ def test_gen():
assert result.exit_code == 0
assert result.output == file.read()

with open('tests/resources/results/no-config-env.yaml', 'r') as file:
result = runner.invoke(root, ['gen', '-e', 'no-config-env', 'tests/resources/good'])
assert result.exit_code == 0
assert result.output == file.read()

with open('tests/resources/results/no-values-env.yaml', 'r') as file:
result = runner.invoke(root, ['gen', '-e', 'no-values-env', 'tests/resources/good'])
assert result.exit_code == 0
assert result.output == file.read()

with open('tests/resources/results/no-yaml-env.yaml', 'r') as file:
result = runner.invoke(root, ['gen', '-e', 'no-yaml-env', 'tests/resources/good'])
assert result.exit_code == 0
assert result.output == file.read()

result = runner.invoke(root, ['gen', '-e', 'not-existing-env', 'tests/resources/good'])
assert result.exit_code == 1
assert str(result.exception) == 'no such environment: not-existing-env'


# vim: fenc=utf-8:ts=4:sw=4:expandtab
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
env: no-config-env
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions tests/resources/results/no-config-env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
# Source: common-template.yaml.j2
env: "no-config-env"
cluster: "none"
foo: "1"
bar: "test"
buz: "buz_default"
7 changes: 7 additions & 0 deletions tests/resources/results/no-values-env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
# Source: common-template.yaml.j2
env: "none"
cluster: "none"
foo: "1"
bar: "test"
buz: "buz_default"
7 changes: 7 additions & 0 deletions tests/resources/results/no-yaml-env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
# Source: common-template.yaml.j2
env: "none"
cluster: "none"
foo: "1"
bar: "test"
buz: "buz_default"