From 060027812238e994214a0a1df7fc4d1c5d143778 Mon Sep 17 00:00:00 2001 From: lassulus Date: Sun, 29 Oct 2023 20:48:09 +0100 Subject: [PATCH] cli machines update: find flake dir automatic if not provided, support flake as path --- pkgs/clan-cli/clan_cli/dirs.py | 35 ++++++++++++----------- pkgs/clan-cli/clan_cli/machines/update.py | 10 +++++-- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/dirs.py b/pkgs/clan-cli/clan_cli/dirs.py index 1bd45ebb1..c70529613 100644 --- a/pkgs/clan-cli/clan_cli/dirs.py +++ b/pkgs/clan-cli/clan_cli/dirs.py @@ -2,6 +2,7 @@ import logging import os import sys from pathlib import Path +from typing import Optional from .errors import ClanError from .types import FlakeName @@ -9,27 +10,27 @@ from .types import FlakeName log = logging.getLogger(__name__) -# def _get_clan_flake_toplevel() -> Path: -# return find_toplevel([".clan-flake", ".git", ".hg", ".svn", "flake.nix"]) +def get_clan_flake_toplevel() -> Path: + return find_toplevel([".clan-flake", ".git", ".hg", ".svn", "flake.nix"]) -# def find_git_repo_root() -> Optional[Path]: -# try: -# return find_toplevel([".git"]) -# except ClanError: -# return None +def find_git_repo_root() -> Optional[Path]: + try: + return find_toplevel([".git"]) + except ClanError: + return None -# def find_toplevel(top_level_files: list[str]) -> Path: -# """Returns the path to the toplevel of the clan flake""" -# for project_file in top_level_files: -# initial_path = Path(os.getcwd()) -# path = Path(initial_path) -# while path.parent != path: -# if (path / project_file).exists(): -# return path -# path = path.parent -# raise ClanError("Could not find clan flake toplevel directory") +def find_toplevel(top_level_files: list[str]) -> Path: + """Returns the path to the toplevel of the clan flake""" + for project_file in top_level_files: + initial_path = Path(os.getcwd()) + path = Path(initial_path) + while path.parent != path: + if (path / project_file).exists(): + return path + path = path.parent + raise ClanError("Could not find clan flake toplevel directory") def user_config_dir() -> Path: diff --git a/pkgs/clan-cli/clan_cli/machines/update.py b/pkgs/clan-cli/clan_cli/machines/update.py index ac0bcf03d..5bcaf3f7c 100644 --- a/pkgs/clan-cli/clan_cli/machines/update.py +++ b/pkgs/clan-cli/clan_cli/machines/update.py @@ -4,7 +4,7 @@ import os import subprocess from pathlib import Path -from ..dirs import specific_flake_dir +from ..dirs import get_clan_flake_toplevel from ..machines.machines import Machine from ..nix import nix_build, nix_command, nix_config from ..secrets.generate import generate_secrets @@ -116,7 +116,10 @@ def get_selected_machines(machine_names: list[str], flake_dir: Path) -> HostGrou # FIXME: we want some kind of inventory here. def update(args: argparse.Namespace) -> None: - flake_dir = specific_flake_dir(args.flake) + if args.flake is None: + flake_dir = get_clan_flake_toplevel() + else: + flake_dir = args.flake if len(args.machines) == 1 and args.target_host is not None: machine = Machine(name=args.machines[0], flake_dir=flake_dir) machine.deployment_address = args.target_host @@ -148,9 +151,10 @@ def register_update_parser(parser: argparse.ArgumentParser) -> None: default=[], ) parser.add_argument( - "flake", + "--flake", type=str, help="name of the flake to update machine for", + default=None, ) parser.add_argument( "--target-host",