add option to not create a git in flakes create

This commit is contained in:
Jörg Thalheim
2024-07-22 08:30:24 +02:00
parent bba39c5c7d
commit 8197bced46

View File

@@ -17,11 +17,11 @@ from ..nix import nix_command, nix_shell
@dataclass @dataclass
class CreateClanResponse: class CreateClanResponse:
flake_init: CmdOut flake_init: CmdOut
git_init: CmdOut | None
git_add: CmdOut
git_config_username: CmdOut | None
git_config_email: CmdOut | None
flake_update: CmdOut flake_update: CmdOut
git_init: CmdOut | None = None
git_add: CmdOut | None = None
git_config_username: CmdOut | None = None
git_config_email: CmdOut | None = None
@dataclass @dataclass
@@ -33,6 +33,7 @@ class CreateOptions:
# URL to the template to use. Defaults to the "minimal" template # URL to the template to use. Defaults to the "minimal" template
template: str = "minimal" template: str = "minimal"
setup_json_inventory: bool = True setup_json_inventory: bool = True
setup_git: bool = True
def git_command(directory: Path, *args: str) -> list[str]: def git_command(directory: Path, *args: str) -> list[str]:
@@ -66,24 +67,31 @@ def create_clan(options: CreateOptions) -> CreateClanResponse:
] ]
) )
flake_init = run(command, cwd=directory) flake_init = run(command, cwd=directory)
flake_update = run(
nix_shell(["nixpkgs#nix"], ["nix", "flake", "update"]), cwd=directory
)
git_init = None response = CreateClanResponse(
if not directory.joinpath(".git").exists(): flake_init=flake_init,
git_init = run(git_command(directory, "init")) flake_update=flake_update,
git_add = run(git_command(directory, "add", ".")) )
if not options.setup_git:
return response
response.git_init = run(git_command(directory, "init"))
response.git_add = run(git_command(directory, "add", "."))
# check if username is set # check if username is set
has_username = run(git_command(directory, "config", "user.name"), check=False) has_username = run(git_command(directory, "config", "user.name"), check=False)
git_config_username = None response.git_config_username = None
if has_username.returncode != 0: if has_username.returncode != 0:
git_config_username = run( response.git_config_username = run(
git_command(directory, "config", "user.name", "clan-tool") git_command(directory, "config", "user.name", "clan-tool")
) )
has_username = run(git_command(directory, "config", "user.email"), check=False) has_username = run(git_command(directory, "config", "user.email"), check=False)
git_config_email = None
if has_username.returncode != 0: if has_username.returncode != 0:
git_config_email = run( response.git_config_email = run(
git_command(directory, "config", "user.email", "clan@example.com") git_command(directory, "config", "user.email", "clan@example.com")
) )
@@ -95,18 +103,6 @@ def create_clan(options: CreateOptions) -> CreateClanResponse:
# Persist creates a commit message for each change # Persist creates a commit message for each change
save_inventory(inventory, directory, "Init inventory") save_inventory(inventory, directory, "Init inventory")
flake_update = run(
nix_shell(["nixpkgs#nix"], ["nix", "flake", "update"]), cwd=directory
)
response = CreateClanResponse(
flake_init=flake_init,
git_init=git_init,
git_add=git_add,
git_config_username=git_config_username,
git_config_email=git_config_email,
flake_update=flake_update,
)
return response return response
@@ -127,6 +123,12 @@ def register_create_parser(parser: argparse.ArgumentParser) -> None:
action=AppendOptionAction, action=AppendOptionAction,
default=[], default=[],
) )
parser.add_argument(
"--no-git",
help="Do not setup git",
action="store_true",
default=False,
)
parser.add_argument( parser.add_argument(
"path", type=Path, help="Path to the clan directory", default=Path(".") "path", type=Path, help="Path to the clan directory", default=Path(".")
@@ -135,7 +137,10 @@ def register_create_parser(parser: argparse.ArgumentParser) -> None:
def create_flake_command(args: argparse.Namespace) -> None: def create_flake_command(args: argparse.Namespace) -> None:
create_clan( create_clan(
CreateOptions( CreateOptions(
directory=args.path, template=args.template, setup_json_inventory=False directory=args.path,
template=args.template,
setup_json_inventory=False,
setup_git=not args.no_git,
) )
) )