Skip to content

Commit

Permalink
refactor: Update _get_config with 'depth' argument
Browse files Browse the repository at this point in the history
  • Loading branch information
bow committed Oct 27, 2023
1 parent baae68e commit 735756f
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions volt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def import_xcmd(
return mod

def list_commands(self, ctx: click.Context) -> list[str]:
config = _get_config(ctx.parent)
config = _get_config(ctx, depth=1)

if (mod := self.import_xcmd(config)) is None:
return []
Expand All @@ -110,7 +110,7 @@ def list_commands(self, ctx: click.Context) -> list[str]:
return rv

def get_command(self, ctx: click.Context, name: str) -> Any:
config = _get_config(ctx.parent)
config = _get_config(ctx, depth=1)
self.import_xcmd(config)
return self.commands.get(name)

Expand Down Expand Up @@ -350,7 +350,7 @@ def build(
directory is specified, no repeated lookups will be performed.
"""
config = _get_config(ctx.parent)
config = _get_config(ctx, depth=1)

session.build(config=config, with_draft=draft, clean=clean)

Expand Down Expand Up @@ -452,7 +452,7 @@ def serve(
file=sys.stderr,
)

config = _get_config(ctx.parent)
config = _get_config(ctx, depth=1)
log_level = cast(str, cast(click.Context, ctx.parent).params["log_level"])
log_color = cast(bool, cast(click.Context, ctx.parent).params["log_color"])

Expand Down Expand Up @@ -506,7 +506,7 @@ def serve_draft(
elif str_value == "off":
value = False

config = _get_config(cast(click.Context, ctx.parent).parent)
config = _get_config(ctx, depth=2)
session.serve_draft(config=config, value=value)

return None
Expand All @@ -529,14 +529,21 @@ def xcmd(ctx: click.Context) -> None:
"""


def _get_config(ctx: Optional[click.Context]) -> Config:
def _get_config(ctx: Optional[click.Context], depth: int) -> Config:
if ctx is None:
raise ValueError("missing expected context")

config = cast(Optional[Config], ctx.params.get("config"))
target_ctx: Optional[Config] = ctx
for d in range(depth):
if (parent_ctx := getattr(target_ctx, "parent", None)) is None:
raise ValueError(f"missing expected parent context at depth {depth-d}")
target_ctx = parent_ctx

config = cast(Optional[Config], target_ctx.params.get("config"))
if config is None:
raise VoltCliError(
f"command {ctx.invoked_subcommand!r} works only within a Volt project"
f"command {target_ctx.invoked_subcommand!r} works only within a"
" Volt project"
)

return config

0 comments on commit 735756f

Please sign in to comment.