pkgs/cli: Add require_flake clan validation logic

Add a `require_flake` function that checks, if no argument is passed, if
we are in a clan directory.
If not will throw a helpful error.

Before `clan show`:

```
Traceback (most recent call last):
  File "/nix/store/8kb3l3yvz6svygnxdlrw5lmd3h3chc8a-clan-cli/bin/.clan-wrapped", line 9, in <module>
    sys.exit(main())
             ~~~~^^
  File "/nix/store/8kb3l3yvz6svygnxdlrw5lmd3h3chc8a-clan-cli/lib/python3.13/site-packages/clan_cli/cli.py", line 493, in main
    args.func(args)
    ~~~~~~~~~^^^^^^
  File "/nix/store/8kb3l3yvz6svygnxdlrw5lmd3h3chc8a-clan-cli/lib/python3.13/site-packages/clan_cli/clan/show.py", line 12, in show_command
    meta = get_clan_details(flake)
  File "/nix/store/8kb3l3yvz6svygnxdlrw5lmd3h3chc8a-clan-cli/lib/python3.13/site-packages/clan_lib/clan/get.py", line 22, in get_clan_details
    if flake.is_local and not flake.path.exists():
       ^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'is_local'
```

with `require_flake`:

```
No clan flake found in the current directory or its parents - Use the --flake flag to specify a clan flake path or URL
```
This commit is contained in:
a-kenji
2025-07-15 12:01:20 +02:00
parent 01ab9e5dbf
commit d4cb206e3e
2 changed files with 26 additions and 1 deletions

View File

@@ -1 +1 @@
from .flake import Flake # noqa from .flake import Flake, require_flake # noqa

View File

@@ -931,3 +931,28 @@ class Flake:
full_selector = f'clanInternals.machines."{system}"."{machine_name}".{selector}' full_selector = f'clanInternals.machines."{system}"."{machine_name}".{selector}'
return self.select(full_selector) return self.select(full_selector)
def require_flake(flake: Flake | None) -> Flake:
"""
Require that a flake argument is provided, if not in a clan flake.
This should be called by commands that require a flake but don't have
a sensible default when no clan flake is found locally.
Args:
flake: The flake object to check, may be None
Returns:
The validated flake object
Raises:
ClanError: If the flake is None
"""
if flake is None:
msg = "No clan flake found in the current directory or its parents"
raise ClanError(
msg,
description="Use the --flake flag to specify a clan flake path or URL",
)
return flake