fix upd
alias, improve run
command
This commit is contained in:
parent
157bd3c8af
commit
9e564a0a8e
1 changed files with 23 additions and 4 deletions
|
@ -4,22 +4,34 @@ from pathlib import Path
|
|||
import subprocess
|
||||
from socket import gethostname
|
||||
import colors
|
||||
from typing import Mapping
|
||||
|
||||
def format_link(link: str, text: str) -> str:
|
||||
return f"\033]8;;{link}\033\\{text}\033]8;;\033\\"
|
||||
|
||||
def run(cmd: list[str], cwd: Path = Path.cwd(), exit_on_error: bool = True, **kwargs) -> subprocess.CompletedProcess:
|
||||
def get_environment() -> dict[str, str]:
|
||||
env = dict(environ)
|
||||
env["DIRENV_DISABLE"] = "1"
|
||||
return env
|
||||
|
||||
def run(cmd: list[str], cwd: Path = Path.cwd(), exit_on_error: bool = True, env: dict[str, str] | None = None, **kwargs) -> subprocess.CompletedProcess:
|
||||
if not env:
|
||||
env = get_environment()
|
||||
c = colors.Colors
|
||||
|
||||
print(f"{c.GREEN}Running command: {c.PURPLE}'{' '.join(cmd)}'{c.END}")
|
||||
if cwd != Path.cwd():
|
||||
print(f"{c.GREEN} in directory: {c.YELLOW}'{format_link(link="file://" + str(cwd), text=cwd)}'{c.END}")
|
||||
result = subprocess.run(cmd, cwd=cwd, check=False, **kwargs)
|
||||
|
||||
result = subprocess.run(cmd, cwd=cwd, check=False, env=env, **kwargs)
|
||||
|
||||
if result.returncode != 0:
|
||||
print(f"{c.RED}Command exited with non-zero exit code {c.CYAN}{c.BOLD}{result.returncode}{c.END}")
|
||||
if exit_on_error is True:
|
||||
result.check_returncode()
|
||||
else:
|
||||
print(f"{c.GREEN}Command exited with exit code {c.CYAN}{c.BOLD}{result.returncode}{c.END}")
|
||||
|
||||
return result
|
||||
|
||||
@aliases.register
|
||||
|
@ -49,6 +61,13 @@ def _vm(args):
|
|||
@aliases.register
|
||||
def _upd(args: list):
|
||||
path = Path("/etc/nixos")
|
||||
possible_subcommands = ("switch", "boot")
|
||||
subcommand = "switch"
|
||||
for arg in args:
|
||||
if arg in possible_subcommands:
|
||||
subcommand = arg
|
||||
args.remove(arg)
|
||||
break
|
||||
if path.exists():
|
||||
c = colors.Colors
|
||||
files_to_delete = {
|
||||
|
@ -57,7 +76,7 @@ def _upd(args: list):
|
|||
}
|
||||
if not "--no-pull" in args:
|
||||
print(f"{c.BLUE}Pulling {c.YELLOW}NixOS{c.BLUE} configuration from remote{c.END}")
|
||||
run["git", "pull", cwd=path]
|
||||
run(["git", "pull"], cwd=path)
|
||||
else:
|
||||
args.remove("--no-pull")
|
||||
if "--rewrite-hardware-configuration" in args:
|
||||
|
@ -84,7 +103,7 @@ def _upd(args: list):
|
|||
args.append("--impure")
|
||||
if "--impure" in args:
|
||||
print(f"{c.RED}WARNING: The --impure flag is set!{c.END}")
|
||||
run(["sudo", "nixos-rebuild", "switch", *args], cwd=path)
|
||||
run(["sudo", "nixos-rebuild", subcommand, *args], cwd=path)
|
||||
|
||||
@aliases.register
|
||||
def _lock(args):
|
||||
|
|
Loading…
Add table
Reference in a new issue