Skip to content

Commit

Permalink
Add option to pass environment variable in the CLI (#82)
Browse files Browse the repository at this point in the history
* add option to pass env vars in cli

* remove unused import

* use the callback

* fix type error

* include format in help text
  • Loading branch information
rsarm authored Jan 8, 2024
1 parent a331cae commit fb28164
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions firecrest/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# SPDX-License-Identifier: BSD-3-Clause
#
import logging
import re
import typer
import yaml

Expand Down Expand Up @@ -896,6 +897,15 @@ def upload(
raise typer.Exit(code=1)


def validate_env_var_format(value: List[str]):
pattern = re.compile(r'\S+=\S+')
for item in value:
if not pattern.match(item):
raise typer.BadParameter(f"Please use the format `VAR=VALUE`.")

return value


@app.command(rich_help_panel="Compute commands")
def submit(
config_from_parent: str = typer.Option(None,
Expand All @@ -919,13 +929,24 @@ def submit(
True,
help="The batch file can be local (default) or on the system's filesystem.",
),
env_vars: List[str] = typer.Option(
[], "-e", "--env-var",
metavar="VAR=VALUE",
help="Environment variable to be exported in the environment where the job script will be submitted (format `VAR=VALUE`).",
callback=validate_env_var_format
)
):
"""Submit a batch script to the workload manager of the target system"""
envvars = {}
for var_value_pair in env_vars:
var, val = var_value_pair.split('=', 1)
envvars[var] = val

try:
if local:
console.print(client.submit(system, script_local_path=job_script, account=account))
console.print(client.submit(system, script_local_path=job_script, account=account, env_vars=envvars))
else:
console.print(client.submit(system, script_remote_path=job_script, account=account))
console.print(client.submit(system, script_remote_path=job_script, account=account, env_vars=envvars))
except Exception as e:
examine_exeption(e)
raise typer.Exit(code=1)
Expand Down

0 comments on commit fb28164

Please sign in to comment.