Skip to content

Commit

Permalink
Make library usage more friendly
Browse files Browse the repository at this point in the history
misc: rename cli_args to params
misc: centralize default values
  • Loading branch information
Karandash8 committed Nov 25, 2024
1 parent be30fdc commit b060832
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 220 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ python3 -m venv .venv
pip install make-argocd-fly
make-argocd-fly -h
usage: main.py [-h] [--root-dir ROOT_DIR] [--config-file CONFIG_FILE] [--source-dir SOURCE_DIR] [--output-dir OUTPUT_DIR] [--tmp-dir TMP_DIR] [--render-apps RENDER_APPS]
[--render-envs RENDER_ENVS] [--skip-generate] [--preserve-tmp-dir] [--clean] [--print-vars] [--var-identifier VAR_IDENTIFIER] [--skip-latest-version-check]
[--yaml-linter] [--kube-linter] [--loglevel LOGLEVEL]
usage: make-argocd-fly [-h] [--root-dir ROOT_DIR] [--config-file CONFIG_FILE] [--source-dir SOURCE_DIR] [--output-dir OUTPUT_DIR] [--tmp-dir TMP_DIR]
[--render-apps RENDER_APPS] [--render-envs RENDER_ENVS] [--skip-generate] [--preserve-tmp-dir] [--remove-output-dir] [--print-vars]
[--var-identifier VAR_IDENTIFIER] [--skip-latest-version-check] [--yaml-linter] [--kube-linter] [--loglevel LOGLEVEL]
Render ArgoCD Applications.
Expand Down
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from make_argocd_fly.main import main
from make_argocd_fly.main import cli_entry_point


if __name__ == '__main__':
main()
cli_entry_point()
26 changes: 13 additions & 13 deletions make_argocd_fly/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from make_argocd_fly.resource import ResourceViewer
from make_argocd_fly.utils import merge_dicts, VarsResolver, get_app_rel_path
from make_argocd_fly.config import get_config
from make_argocd_fly.cli_args import get_cli_args
from make_argocd_fly.params import get_params
from make_argocd_fly.steps import FindAppsStep, RenderYamlStep, RenderJinjaFromViewerStep, RenderJinjaFromMemoryStep, \
WriteResourcesStep, ReadSourceStep, RunKustomizeStep
from make_argocd_fly.exceptions import MissingSourceResourcesError
Expand All @@ -24,7 +24,7 @@ def __init__(self, app_name: str, env_name: str, app_viewer: ResourceViewer = No
self.env_name = env_name
self.app_viewer = app_viewer
self.config = get_config()
self.cli_args = get_cli_args()
self.params = get_params()

@abstractmethod
async def process(self) -> None:
Expand Down Expand Up @@ -95,15 +95,15 @@ async def process(self) -> None:
})
global_vars_resolved = VarsResolver.resolve_all(global_vars,
global_vars,
var_identifier=self.cli_args.get_var_identifier())
var_identifier=self.params.get_var_identifier())
env_vars_resolved = merge_dicts(global_vars_resolved,
VarsResolver.resolve_all(self.config.get_env_vars(env_name),
merge_dicts(global_vars_resolved, self.config.get_env_vars(self.env_name)),
var_identifier=self.cli_args.get_var_identifier()))
var_identifier=self.params.get_var_identifier()))
template_vars = merge_dicts(env_vars_resolved,
VarsResolver.resolve_all(self.config.get_app_vars(env_name, app_name),
merge_dicts(env_vars_resolved, self.config.get_app_vars(env_name, app_name)),
var_identifier=self.cli_args.get_var_identifier()))
var_identifier=self.params.get_var_identifier()))

self.render_jinja_step.configure(textwrap.dedent(self.APPLICATION_RESOUCE_TEMPLATE), self.app_name, self.env_name, template_vars)
await self.render_jinja_step.run()
Expand Down Expand Up @@ -139,17 +139,17 @@ async def process(self) -> None:
})
global_vars_resolved = VarsResolver.resolve_all(global_vars,
global_vars,
var_identifier=self.cli_args.get_var_identifier())
var_identifier=self.params.get_var_identifier())
env_vars_resolved = merge_dicts(global_vars_resolved,
VarsResolver.resolve_all(self.config.get_env_vars(self.env_name),
merge_dicts(global_vars_resolved, self.config.get_env_vars(self.env_name)),
var_identifier=self.cli_args.get_var_identifier()))
var_identifier=self.params.get_var_identifier()))
template_vars = merge_dicts(env_vars_resolved,
VarsResolver.resolve_all(self.config.get_app_vars(self.env_name, self.app_name),
merge_dicts(env_vars_resolved, self.config.get_app_vars(self.env_name, self.app_name)),
var_identifier=self.cli_args.get_var_identifier()))
var_identifier=self.params.get_var_identifier()))

if self.cli_args.get_print_vars():
if self.params.get_print_vars():
log.info('Variables for application {} in environment {}:\n{}'.format(self.app_name, self.env_name, pformat(template_vars)))

yml_children = self.app_viewer.get_files_children(r'(\.yml)$')
Expand Down Expand Up @@ -195,17 +195,17 @@ async def process(self) -> None:
})
global_vars_resolved = VarsResolver.resolve_all(global_vars,
global_vars,
var_identifier=self.cli_args.get_var_identifier())
var_identifier=self.params.get_var_identifier())
env_vars_resolved = merge_dicts(global_vars_resolved,
VarsResolver.resolve_all(self.config.get_env_vars(self.env_name),
merge_dicts(global_vars_resolved, self.config.get_env_vars(self.env_name)),
var_identifier=self.cli_args.get_var_identifier()))
var_identifier=self.params.get_var_identifier()))
template_vars = merge_dicts(env_vars_resolved,
VarsResolver.resolve_all(self.config.get_app_vars(self.env_name, self.app_name),
merge_dicts(env_vars_resolved, self.config.get_app_vars(self.env_name, self.app_name)),
var_identifier=self.cli_args.get_var_identifier()))
var_identifier=self.params.get_var_identifier()))

if self.cli_args.get_print_vars():
if self.params.get_print_vars():
log.info('Variables for application {} in environment {}:\n{}'.format(self.app_name, self.env_name, pformat(template_vars)))

yml_children = self.app_viewer.get_files_children(r'(\.yml)$', ['base', self.env_name])
Expand Down
116 changes: 0 additions & 116 deletions make_argocd_fly/cli_args.py

This file was deleted.

41 changes: 19 additions & 22 deletions make_argocd_fly/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,27 @@
import os
import yaml

from make_argocd_fly.cli_args import CLIArgs
from make_argocd_fly import defaults

LOG_CONFIG_FILE = 'log_config.yml'
CONFIG_FILE = 'config.yml'
SOURCE_DIR = 'source'
OUTPUT_DIR = 'output'
TMP_DIR = '.tmp'
ENVS = {}
VARS = {}

log = logging.getLogger(__name__)


class Config:
def __init__(self) -> None:
self.source_dir = None
self.output_dir = None
self.tmp_dir = None
self.envs = None
self.vars = None
self.source_dir = defaults.SOURCE_DIR
self.output_dir = defaults.OUTPUT_DIR
self.tmp_dir = defaults.TMP_DIR
self.envs = defaults.ENVS
self.vars = defaults.VARS

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 = 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
self.envs = config['envs'] if 'envs' in config else defaults.ENVS
self.vars = config['vars'] if 'vars' in config else defaults.VARS

def get_source_dir(self) -> str:
return get_abs_path(self.root_dir, self.source_dir)
Expand Down Expand Up @@ -109,20 +102,24 @@ def get_abs_path(root_dir: str, path: str, allow_missing: bool = False) -> str:
return abs_path


def read_config(root_dir: str, config_file: str, cli_args: CLIArgs) -> Config:
def read_config(root_dir: str = defaults.ROOT_DIR,
config_file: str = defaults.CONFIG_FILE,
source_dir: str = defaults.SOURCE_DIR,
output_dir: str = defaults.OUTPUT_DIR,
tmp_dir: str = defaults.TMP_DIR) -> Config:
root_dir = os.path.abspath(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, cli_args.get_source_dir(), cli_args.get_output_dir(), cli_args.get_tmp_dir())
config.init_config(root_dir, config_content, source_dir, output_dir, tmp_dir)

log.info('Root directory: {}'.format(root_dir))
log.info('Config file: {}'.format(get_abs_path(root_dir, config_file)))
log.info('Source directory: {}'.format(config.get_source_dir()))
log.info('Output directory: {}'.format(config.get_output_dir()))
log.info('Temporary directory: {}'.format(config.get_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
12 changes: 12 additions & 0 deletions make_argocd_fly/defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os

ROOT_DIR = os.getcwd()
CONFIG_FILE = 'config.yml'
SOURCE_DIR = 'source'
OUTPUT_DIR = 'output'
TMP_DIR = '.tmp'
LOG_CONFIG_FILE = 'log_config.yml'
ENVS = {}
VARS = {}
VAR_IDENTIFIER = '$'
LOGLEVEL = 'INFO'
Loading

0 comments on commit b060832

Please sign in to comment.