Skip to content

Commit

Permalink
feat: ability to inject env variables to installation
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbraun89 authored Mar 13, 2023
1 parent 6d2a61b commit 94253d3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
37 changes: 20 additions & 17 deletions dcontainer/cli/feature.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import logging
import pathlib
from typing import List, Optional
from typing import List, Optional, Dict

import typer

Expand Down Expand Up @@ -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(
Expand All @@ -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)
3 changes: 2 additions & 1 deletion dcontainer/cli/install/install_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions dcontainer/oci/oci_feature_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit 94253d3

Please sign in to comment.