From 94253d3288e9a2156b6dbda1a84aa2a2ce6d451e Mon Sep 17 00:00:00 2001 From: Daniel Braun Date: Mon, 13 Mar 2023 16:46:12 +0000 Subject: [PATCH] feat: ability to inject env variables to installation --- dcontainer/cli/feature.py | 37 ++++++++++++----------- dcontainer/cli/install/install_feature.py | 3 +- dcontainer/oci/oci_feature_installer.py | 4 +-- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/dcontainer/cli/feature.py b/dcontainer/cli/feature.py index 9f240891..1b916b38 100644 --- a/dcontainer/cli/feature.py +++ b/dcontainer/cli/feature.py @@ -1,7 +1,7 @@ import json import logging import pathlib -from typing import List, Optional +from typing import List, Optional, Dict import typer @@ -63,8 +63,24 @@ def inspect_command( def install_command( feature: str, option: Optional[List[str]] = typer.Option(None, callback=_validate_args), + remote_user: Optional[str] = typer.Option(None, callback=_validate_args), + env: Optional[List[str]] = typer.Option(None, callback=_validate_args), verbose: bool = False, ) -> None: + + def _key_val_arg_to_dict(args: Optional[List[str]]) -> Dict[str,str]: + if args is None: + return {} + + args_dict = {} + for single_arg in args: + single_arg = _strip_if_wrapped_around(single_arg, '"') + arg_name = single_arg.split("=")[0] + arg_value = single_arg[len(arg_name) + 1 :] + arg_value = _strip_if_wrapped_around(arg_value, '"') + args_dict[arg_name] = arg_value + return args_dict + def _strip_if_wrapped_around(value: str, char: str) -> str: if len(char) > 1: raise ValueError( @@ -75,20 +91,7 @@ def _strip_if_wrapped_around(value: str, char: str) -> str: return value.strip(char) return value - if option is None: - options = [] - else: - options = option - - options_dict = {} - for single_option in options: - single_option = _strip_if_wrapped_around(single_option, '"') - - option_name = single_option.split("=")[0] - - option_value = single_option[len(option_name) + 1 :] - option_value = _strip_if_wrapped_around(option_value, '"') - - options_dict[option_name] = option_value + options_dict = _key_val_arg_to_dict(option) + envs_dict = _key_val_arg_to_dict(env) - install_feature(feature=feature, options=options_dict, verbose=verbose) + install_feature(feature=feature, options=options_dict, envs=envs_dict, verbose=verbose,remote_user=remote_user) diff --git a/dcontainer/cli/install/install_feature.py b/dcontainer/cli/install/install_feature.py index 4db44365..f3e941d1 100644 --- a/dcontainer/cli/install/install_feature.py +++ b/dcontainer/cli/install/install_feature.py @@ -10,7 +10,8 @@ def install_feature( feature: str, options: Optional[Dict[str, str]] = None, + envs: Optional[Dict[str, str]] = None, remote_user: Optional[str] = None, verbose: bool = False, ) -> None: - OCIFeatureInstaller.install(OCIFeature(feature), options, remote_user, verbose) + OCIFeatureInstaller.install(feature_oci=OCIFeature(feature), envs=envs, options=options, remote_user=remote_user, verbose=verbose) diff --git a/dcontainer/oci/oci_feature_installer.py b/dcontainer/oci/oci_feature_installer.py index 52f54d09..c3634e2f 100644 --- a/dcontainer/oci/oci_feature_installer.py +++ b/dcontainer/oci/oci_feature_installer.py @@ -44,7 +44,7 @@ def install( feature_oci: OCIFeature, options: Optional[Dict[str, Union[str, bool]]] = None, envs: Optional[Dict[str, str]] = None, - remote_user_name: Optional[str] = None, + remote_user: Optional[str] = None, verbose: bool = False, ) -> None: if options is None: @@ -57,7 +57,7 @@ def install( options = cls._resolve_options(feature_obj=feature_obj, options=options) logger.info("resolved options: %s", str(options)) - remote_user = cls._resolve_remote_user(remote_user_name) + remote_user = cls._resolve_remote_user(remote_user) logger.info("resolved remote user: %s", remote_user) envs[cls._REMOTE_USER_ENV] = remote_user.pw_name