Skip to content

Commit

Permalink
Merge pull request #68 from Karandash8/67-make-work-directories-confi…
Browse files Browse the repository at this point in the history
…gurable

Configure work directories
  • Loading branch information
Karandash8 authored Jul 2, 2024
2 parents 9a8992a + fdcad76 commit 5a3795b
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,4 @@ cython_debug/

**/.tmp/
.vscode
.DS_store
66 changes: 37 additions & 29 deletions make_argocd_fly/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
import yaml

LOG_CONFIG_FILE = 'log_config.yml'
CONFIG_FILE = 'config.yml'
SOURCE_DIR = 'source'
OUTPUT_DIR = 'output'
TMP_DIR = '.tmp'
Expand All @@ -19,35 +21,23 @@ def __init__(self) -> None:
self.envs = None
self.vars = None

def init_config(self, root_dir: str, config: dict) -> None:
def init_config(self, root_dir: str, config: dict, source_dir: str, output_dir: str, tmp_dir: str) -> None:
self.root_dir = root_dir

self.source_dir = config['source_dir'] if 'source_dir' in config else SOURCE_DIR
self.output_dir = config['output_dir'] if 'output_dir' in config else OUTPUT_DIR
self.tmp_dir = config['tmp_dir'] if 'tmp_dir' in config else TMP_DIR
self.source_dir = source_dir
self.output_dir = output_dir
self.tmp_dir = tmp_dir
self.envs = config['envs'] if 'envs' in config else ENVS
self.vars = config['vars'] if 'vars' in config else VARS

def get_source_dir(self) -> str:
if not self.source_dir:
log.error('Config was not initialized.')
raise Exception

return os.path.join(self.root_dir, self.source_dir)
return get_abs_path(self.root_dir, self.source_dir)

def get_output_dir(self) -> str:
if not self.output_dir:
log.error('Config was not initialized.')
raise Exception

return os.path.join(self.root_dir, self.output_dir)
return get_abs_path(self.root_dir, self.output_dir, allow_missing=True)

def get_tmp_dir(self) -> str:
if not self.tmp_dir:
log.error('Config was not initialized.')
raise Exception

return os.path.join(self.root_dir, self.tmp_dir)
return get_abs_path(self.root_dir, self.tmp_dir, allow_missing=True)

def get_envs(self) -> dict:
if not self.envs:
Expand Down Expand Up @@ -87,17 +77,35 @@ def get_app_vars(self, env_name: str, app_name: str) -> dict:
config = Config()


def read_config(root_dir: str, config_file: str) -> Config:
def get_abs_path(root_dir: str, path: str, allow_missing: bool = False) -> str:
if not path:
log.error('Path is empty.')
raise Exception

if os.path.isabs(path):
abs_path = path
else:
abs_path = os.path.join(root_dir, path)

if (not allow_missing) and (not os.path.exists(abs_path)):
log.error('Path does not exist: {}'.format(abs_path))
raise Exception

return abs_path


def read_config(root_dir: str, config_file: str, source_dir: str, output_dir: str, tmp_dir: str) -> Config:
config_content = {}
try:
with open(os.path.join(root_dir, config_file)) as f:
config_content = yaml.safe_load(f.read())
except FileNotFoundError as error:
log.error('Config file is missing')
log.fatal(error)
raise

config.init_config(root_dir, config_content)
with open(get_abs_path(root_dir, config_file)) as f:
config_content = yaml.safe_load(f.read())

config.init_config(root_dir, config_content, source_dir, output_dir, tmp_dir)

log.debug('Root directory: {}'.format(root_dir))
log.debug('Config file: {}'.format(get_abs_path(root_dir, config_file)))
log.debug('Source directory: {}'.format(config.get_source_dir()))
log.debug('Output directory: {}'.format(config.get_output_dir()))
log.debug('Temporary directory: {}'.format(config.get_tmp_dir()))

return config

Expand Down
15 changes: 8 additions & 7 deletions make_argocd_fly/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
import yaml
import yamllint

from make_argocd_fly.config import read_config, get_config
from make_argocd_fly.config import read_config, get_config, LOG_CONFIG_FILE, CONFIG_FILE, \
SOURCE_DIR, OUTPUT_DIR, TMP_DIR
from make_argocd_fly.utils import multi_resource_parser, generate_filename
from make_argocd_fly.resource import ResourceViewer, ResourceWriter
from make_argocd_fly.application import application_factory

LOG_CONFIG_FILE = 'log_config.yml'
CONFIG_FILE = 'config.yml'

logging.basicConfig(level='INFO')

Expand Down Expand Up @@ -87,8 +86,11 @@ async def generate(render_envs, render_apps) -> None:

def main() -> None:
parser = argparse.ArgumentParser(description='Render ArgoCD Applications.')
parser.add_argument('--root-dir', type=str, default=os.getcwd(), help='Root directory')
parser.add_argument('--config-file', type=str, default=CONFIG_FILE, help='Configuration file')
parser.add_argument('--root-dir', type=str, default=os.getcwd(), help='Root directory (default: current directory)')
parser.add_argument('--config-file', type=str, default=CONFIG_FILE, help='Configuration file (default: config.yaml)')
parser.add_argument('--source-dir', type=str, default=SOURCE_DIR, help='Source files directory (default: source)')
parser.add_argument('--output-dir', type=str, default=OUTPUT_DIR, help='Output files directory (default: output)')
parser.add_argument('--tmp-dir', type=str, default=TMP_DIR, help='Temporary files directory (default: .tmp)')
parser.add_argument('--render-apps', type=str, default=None, help='Comma separate list of applications to render')
parser.add_argument('--render-envs', type=str, default=None, help='Comma separate list of environments to render')
parser.add_argument('--skip-generate', action='store_true', help='Skip resource generation')
Expand All @@ -101,8 +103,7 @@ def main() -> None:

init_logging(args.loglevel)

log.debug('Root directory path: {}'.format(os.path.abspath(args.root_dir)))
config = read_config(os.path.abspath(args.root_dir), args.config_file)
config = read_config(os.path.abspath(args.root_dir), args.config_file, args.source_dir, args.output_dir, args.tmp_dir)

tmp_dir = config.get_tmp_dir()
if os.path.exists(tmp_dir):
Expand Down
4 changes: 0 additions & 4 deletions tests/manual/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
source_dir: source
output_dir: output
tmp_dir: .tmp

# caveat: the folloving variable names are resenved: __application
vars:
argocd:
Expand Down
90 changes: 90 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import pytest
from make_argocd_fly.config import get_abs_path, read_config, Config


def test_get_abs_path_with_relative_path_in_current_directory(tmp_path):
root_dir = tmp_path
path = 'config.py'
expected = tmp_path / 'config.py'
expected.write_text('test')

assert get_abs_path(root_dir, path) == str(expected)

def test_get_abs_path_with_absolute_path_in_current_directory(tmp_path):
root_dir = tmp_path
path = tmp_path / 'config.py'
expected = tmp_path / 'config.py'
expected.write_text('test')

assert get_abs_path(root_dir, path) == expected

def test_get_abs_path_with_relative_path_in_subdirectory(tmp_path):
root_dir = tmp_path
src_dir = tmp_path / 'source'
src_dir.mkdir()
path = 'source/app.py'
expected = tmp_path / 'source/app.py'
expected.write_text('test')

assert get_abs_path(str(root_dir), path) == str(expected)

def test_get_abs_path_with_absolute_path_in_subdirectory(tmp_path):
root_dir = tmp_path
src_dir = tmp_path / 'source'
src_dir.mkdir()
path = tmp_path / 'source/app.py'
expected = tmp_path / 'source/app.py'
expected.write_text('test')

assert get_abs_path(root_dir, path) == expected

def test_get_abs_path_with_empty_path(tmp_path, caplog):
root_dir = tmp_path
path = ''

with pytest.raises(Exception):
get_abs_path(root_dir, path)
assert 'Path is empty.' in caplog.text

def test_get_abs_path_with_none_path(tmp_path, caplog):
root_dir = tmp_path
path = None

with pytest.raises(Exception):
get_abs_path(root_dir, path)
assert 'Path is empty.' in caplog.text

def test_get_abs_path_with_nonexistent_path(tmp_path, caplog):
root_dir = str(tmp_path)
path = 'nonexistent_file.py'
non_existent_path = tmp_path / 'nonexistent_file.py'

with pytest.raises(Exception):
get_abs_path(root_dir, path)
assert 'Path does not exist: {}'.format(non_existent_path) in caplog.text

def test_get_abs_path_with_nonexistent_path_allow_missing(tmp_path, caplog):
root_dir = str(tmp_path)
path = 'nonexistent_file.py'
non_existent_path = tmp_path / 'nonexistent_file.py'

assert get_abs_path(root_dir, path, allow_missing=True) == str(non_existent_path)


##################
### Config
##################

def test_read_config_with_valid_config_file(tmp_path):
root_dir = tmp_path
config_file = 'config.yml'
source_dir = 'source'
output_dir = 'output'
tmp_dir = '.tmp'
config_file_path = tmp_path / config_file
config_file_path.write_text('test')
source_dir_path = tmp_path / source_dir
source_dir_path.mkdir()
config = read_config(root_dir, config_file, source_dir, output_dir, tmp_dir)
assert isinstance(config, Config)
assert config.get_source_dir() == str(source_dir_path)

0 comments on commit 5a3795b

Please sign in to comment.