Skip to content

[dg] verb-first env cli commands #29115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Usage: dg [OPTIONS] COMMAND [ARGS]...
│ check Commands for checking the integrity of your Dagster code. │
│ dev Start a local instance of Dagster. │
│ docs Commands for generating docs from your Dagster code. │
│ env Commands for managing environment variables. │
│ init Initialize a new Dagster project or workspace. │
│ launch Launch a Dagster run. │
│ list Commands for listing Dagster entities. │
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from dagster_dg.cli.check import check_group
from dagster_dg.cli.dev import dev_command
from dagster_dg.cli.docs import docs_group
from dagster_dg.cli.env import env_group
from dagster_dg.cli.init import init_command
from dagster_dg.cli.launch import launch_command
from dagster_dg.cli.list import list_group
Expand All @@ -29,7 +28,6 @@ def create_dg_cli():
commands={
"check": check_group,
"docs": docs_group,
"env": env_group,
"utils": utils_group,
"launch": launch_command,
"list": list_group,
Expand Down
44 changes: 0 additions & 44 deletions python_modules/libraries/dagster-dg/dagster_dg/cli/env.py

This file was deleted.

28 changes: 28 additions & 0 deletions python_modules/libraries/dagster-dg/dagster_dg/cli/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from dagster_dg.component import PluginObjectFeature, RemotePluginRegistry
from dagster_dg.config import normalize_cli_config
from dagster_dg.context import DgContext
from dagster_dg.env import ProjectEnvVars
from dagster_dg.utils import DgClickCommand, DgClickGroup
from dagster_dg.utils.telemetry import cli_telemetry_wrapper

Expand Down Expand Up @@ -309,3 +310,30 @@ def list_defs_command(output_json: bool, **global_options: object) -> None:
table.add_row("Sensors", _get_sensors_table(sensors))

console.print(table)


# ########################
# ##### ENVIRONMENT
# ########################


@list_group.command(name="env", cls=DgClickCommand)
@dg_global_options
@cli_telemetry_wrapper
def list_env_command(**global_options: object) -> None:
"""List environment variables from the .env file of the current project."""
cli_config = normalize_cli_config(global_options, click.get_current_context())
dg_context = DgContext.for_project_environment(Path.cwd(), cli_config)

env = ProjectEnvVars.from_ctx(dg_context)
if not env.values:
click.echo("No environment variables are defined for this project.")
return

table = Table(border_style="dim")
table.add_column("Env Var")
table.add_column("Value")
for key, value in env.values.items():
table.add_row(key, value)
console = Console()
console.print(table)
8 changes: 4 additions & 4 deletions python_modules/libraries/dagster-dg/dagster_dg/cli/plus.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ def login_command() -> None:
# ########################


@plus_group.group(name="env", cls=DgClickGroup)
def plus_env_group():
"""Commands for managing environment variables in Dagster Plus."""
@plus_group.group(name="plus", cls=DgClickGroup)
def plus_pull_group():
"""Commands for pulling configuration from Dagster Plus."""


def _get_local_secrets_for_locations(
Expand All @@ -99,7 +99,7 @@ def _get_local_secrets_for_locations(
return secrets_by_location


@plus_env_group.command(name="pull", cls=DgClickCommand)
@plus_pull_group.command(name="env", cls=DgClickCommand)
@dg_global_options
@cli_telemetry_wrapper
def pull_env_command(**global_options: object) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_pull_env_command_no_auth(monkeypatch):
):
monkeypatch.setenv("DG_CLI_CONFIG", str(Path(cloud_config_dir) / "dg.toml"))
monkeypatch.setenv("DAGSTER_CLOUD_CLI_CONFIG", str(Path(cloud_config_dir) / "config"))
result = runner.invoke("plus", "env", "pull")
result = runner.invoke("plus", "pull", "env")
assert result.exit_code != 0, result.output + " " + str(result.exception)
assert (
"`dg plus env pull` requires authentication with Dagster Plus. Run `dg plus login` to authenticate."
Expand All @@ -50,7 +50,7 @@ def test_pull_env_command_auth_err(dg_plus_cli_config):
},
expected_variables={"onlyViewable": True, "scopes": {"localDeploymentScope": True}},
)
result = runner.invoke("plus", "env", "pull")
result = runner.invoke("plus", "pull", "env")
assert result.exit_code != 0, result.output + " " + str(result.exception)
assert "Unauthorized: Not authorized" in str(result.output)

Expand All @@ -74,7 +74,7 @@ def test_pull_env_command_python_err(dg_plus_cli_config):
},
expected_variables={"onlyViewable": True, "scopes": {"localDeploymentScope": True}},
)
result = runner.invoke("plus", "env", "pull")
result = runner.invoke("plus", "pull", "env")
assert result.exit_code != 0, result.output + " " + str(result.exception)
assert "Error: An error has occurred" in str(result.output)

Expand Down Expand Up @@ -121,7 +121,7 @@ def test_pull_env_command_project(dg_plus_cli_config):
},
expected_variables={"onlyViewable": True, "scopes": {"localDeploymentScope": True}},
)
result = runner.invoke("plus", "env", "pull")
result = runner.invoke("plus", "pull", "env")
assert result.exit_code == 0, result.output + " " + str(result.exception)
assert result.output.strip() == "Environment variables saved to .env"

Expand Down Expand Up @@ -174,7 +174,7 @@ def test_pull_env_command_workspace(dg_plus_cli_config):
},
expected_variables={"onlyViewable": True, "scopes": {"localDeploymentScope": True}},
)
result = runner.invoke("plus", "env", "pull")
result = runner.invoke("plus", "pull", "env")
assert result.exit_code == 0, result.output + " " + str(result.exception)
assert (
result.output.strip()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

ensure_dagster_dg_tests_import()

from dagster_dg.utils import ensure_dagster_dg_tests_import

from dagster_dg_tests.utils import (
ProxyRunner,
assert_runner_result,
Expand Down Expand Up @@ -432,3 +434,37 @@ def _sample_env_var_assets():
@asset(kinds={"sling"}, group_name=os.getenv("GROUP_NAME", "MISSING"))
def alpha():
pass


# ########################
# ##### LIST
# ########################


def test_list_env_succeeds():
with (
ProxyRunner.test(use_fixed_test_components=True) as runner,
isolated_example_project_foo_bar(runner, in_workspace=False),
):
result = runner.invoke("list", "env")
assert_runner_result(result)
assert (
result.output.strip()
== textwrap.dedent("""
No environment variables are defined for this project.
""").strip()
)

Path(".env").write_text("FOO=bar")
result = runner.invoke("list", "env")
assert_runner_result(result)
assert (
result.output.strip()
== textwrap.dedent("""
┏━━━━━━━━━┳━━━━━━━┓
┃ Env Var ┃ Value ┃
┡━━━━━━━━━╇━━━━━━━┩
│ FOO │ bar │
└─────────┴───────┘
""").strip()
)
Loading