From d4d76d4a3df4bfd2d0ea3b9f92d5324fbeb1b7b6 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Wed, 6 Nov 2024 22:48:59 -0600 Subject: [PATCH] new and improved hwylterm --- pkgs/oizys/default.nix | 2 +- pkgs/oizys/nimble.lock | 14 +-- pkgs/oizys/oizys.nimble | 3 +- pkgs/oizys/src/oizys.nim | 210 +++++++++++++++++++-------------------- 4 files changed, 109 insertions(+), 120 deletions(-) diff --git a/pkgs/oizys/default.nix b/pkgs/oizys/default.nix index 7c658ae..da803d0 100644 --- a/pkgs/oizys/default.nix +++ b/pkgs/oizys/default.nix @@ -8,7 +8,7 @@ buildNimblePackage { verions = "unstable"; src = lib.cleanSource ./.; nativeBuildInputs = [ openssl ]; - nimbleDepsHash = "sha256-0F/rKcLUH95vW3ODB2mgMQ2klbN9rjMeP+LUK0Ucj2w="; + nimbleDepsHash = "sha256-YCHyMyy6cvNZgsmxPgskbAMETHs4/bP2Cp6XbjfWm1k="; meta = { description = "nix begat oizys"; diff --git a/pkgs/oizys/nimble.lock b/pkgs/oizys/nimble.lock index 65e044c..067d482 100644 --- a/pkgs/oizys/nimble.lock +++ b/pkgs/oizys/nimble.lock @@ -1,24 +1,14 @@ { "version": 2, "packages": { - "cligen": { - "version": "1.7.7", - "vcsRevision": "f50f887eacfe33866e22f8a0d322e29a1c830cf9", - "url": "https://github.com/c-blake/cligen.git", - "downloadMethod": "git", - "dependencies": [], - "checksums": { - "sha1": "237596b6be88dcf9ee0bd9732a7baaac526f0c07" - } - }, "hwylterm": { "version": "0.1.0", - "vcsRevision": "56bb2bb7c1da402c2c52fb3f7f10620c7c2fca7e", + "vcsRevision": "a4e0f3fc7b34bac8e448dd35f58012d07dd4a0d0", "url": "https://github.com/daylinmorgan/hwylterm", "downloadMethod": "git", "dependencies": [], "checksums": { - "sha1": "8810cf69a74de580e1ab08f0c153afbf33c8a98e" + "sha1": "848f75dc1bc910560e5e0038bf0a9ebf8d096385" } }, "jsony": { diff --git a/pkgs/oizys/oizys.nimble b/pkgs/oizys/oizys.nimble index 317d2fe..3582cdf 100644 --- a/pkgs/oizys/oizys.nimble +++ b/pkgs/oizys/oizys.nimble @@ -11,8 +11,7 @@ bin = @["oizys"] # Dependencies requires "nim >= 2.0.8" -requires "cligen" requires "jsony" requires "zippy" -requires "https://github.com/daylinmorgan/hwylterm#HEAD" +requires "https://github.com/daylinmorgan/hwylterm#a4e0f3f" diff --git a/pkgs/oizys/src/oizys.nim b/pkgs/oizys/src/oizys.nim index 69f36b2..a372abd 100644 --- a/pkgs/oizys/src/oizys.nim +++ b/pkgs/oizys/src/oizys.nim @@ -1,115 +1,115 @@ ## nix begat oizys -import std/[os, osproc, tables, sequtils, strformat, strutils] -import hwylterm, hwylterm/[cligen, logging] -import oizys/[context, github, nix, overlay, logging] - - -proc confirm(q: string): bool = - stderr.write $(q & bb"[yellow] (Y/n) ") - while true: - let ans = readLine(stdin) - case ans.strip().toLowerAscii(): - of "y","yes": return true - of "n","no": return false - else: - stderr.write($bb("[red]Please answer Yes/no\nexpected one of [b]Y,yes,N,no ")) - stderr.write "\n" - -overlay: - proc pre( - flake: string = "", - host: seq[string] = @[], - debug: bool = false, - resetCache: bool = false, - rest: seq[string], - ) = - setupLoggers(debug) - updateContext(host, flake, debug, resetCache) - - proc dry(minimal: bool = false) = - ## dry run build - nixBuildHostDry(minimal, rest) - - proc output(yes: bool = false) = - ## nixos config attr - echo nixosConfigAttrs().join(" ") - - proc update( - yes: bool = false, - preview: bool = false - ) = - ## update and run nixos-rebuild - let hosts = getHosts() - if hosts.len > 1: fatalQuit "operation only supports one host" - let run = getLastUpdateRun() - echo fmt"run created at: {run.created_at}" - echo "nvd diff:\n", getUpdateSummary(run.id, hosts[0]) - if preview: quit 0 - if yes or confirm("Proceed with system update?"): - updateRepo() - nixosRebuild("switch") - - proc build(minimal: bool = false) = - ## nix build - nixBuild(minimal, rest) - - proc cache(name: string = "oizys", service: string = "attic", jobs: int = countProcessors()) = - ## build and push store paths - nixBuildWithCache(name, rest, service, jobs) - - proc osCmd() = - ## nixos-rebuild - if len(rest) == 0: fatalQuit "please provide subcmd" - let subcmd = rest[0] - if subcmd notin nixosSubcmds: - fatalQuit( - &"unknown nixos-rebuild subcmd: {subcmd}\nexpected one of: \n" & - nixosSubcmds.mapIt(" " & it).join("\n") - ) - nixosRebuild(subcmd, rest[1..^1]) - - proc ci(`ref`: string = "main") = - ## trigger GHA - if rest.len == 0: fatalQuit "expected workflow file name" - createDispatch(rest[0], `ref`) +import std/[os, osproc, sequtils, strformat, strutils] +import hwylterm, hwylterm/[hwylcli, logging] +import oizys/[context, github, nix, logging] proc checkExes() = if findExe("nix") == "": fatalQuit "oizys requires nix" +checkexes() +hwylCli: + name "oizys" + globalFlags: + flake "path/to/flake" + host: + T seq[string] + ? "host(s) to build" + # - h conflicts with autoadded help short flag + debug: + T bool + ? "enable debug mode" + - d + resetCache: + T bool + ? "set cache timeout to 0" + - r + preSub: + setupLoggers(debug) + updateContext(host, flake, debug, resetCache) + subcommands: -when isMainModule: - import cligen - checkExes() - hwylCli(clCfg) + --- build + ... "nix build" + flags: + minimal: + T bool + - m + run: + nixBuild(minimal, args) - const - sharedHelp = //{ - "flake" : "path/to/flake", - "host" : "host(s) to build", - "debug" : "enable debug mode", - "resetCache" : "set cache timeout to 0" - } - updateHelp = //{ - "yes" : "skip all confirmation prompts" - } // sharedHelp - ciHelp = //{ - "ref" : "git ref/branch/tag to trigger workflow on" - } - cacheHelp = //{ - "name" : "name of cachix binary cache", - "jobs" : "jobs when pushing paths" - } // sharedHelp - let - osUsage = $bb("$command [[subcmd] $args\n$doc[bold]Options[/]:\n$options") + --- cache + ... "build and push store paths" + flags: + name: + * "oizys" + ? "name of binary cache" + service: + * "attic" + jobs: + * countProcessors() + ? "jobs when pushing paths" + T int + run: + nixBuildWithCache(name, args, service, jobs) - dispatchMulti( - [build, help = sharedHelp, usage = clCfg.use ], - [cache, help = cacheHelp , usage = clCfg.use ], - [ci, help = ciHelp , usage = clCfg.use ], - [dry, help = sharedHelp, usage = clCfg.use ], - [osCmd, help = sharedHelp, usage = osUsage, cmdName = "os"], - [output, help = sharedHelp, usage = clCfg.use], - [update, help = updateHelp, usage = clCfg.use], - ) + --- ci + ... "trigger GHA" + flags: + `ref`: + ? "git ref/branch/tag to trigger workflow on" + * "main" + run: + if args.len == 0: fatalQuit "expected workflow file name" + createDispatch(args[0], `ref`) + --- dry + ... "dry run build" + flags: + minimal: + T bool + run: + nixBuildHostDry(minimal, args) + + --- os + ? "[b]oizys os[/] [i]subcmd[/] [[[faint]flags[/]]" + ... "nixos-rebuild [italic]subcmd[/]" + run: + if args.len == 0: fatalQuit "please provide subcmd" + let subcmd = args[0] + if subcmd notin nixosSubcmds: + fatalQuit( + &"unknown nixos-rebuild subcmd: {subcmd}\nexpected one of: \n" & + nixosSubcmds.mapIt(" " & it).join("\n") + ) + nixosRebuild(subcmd, args[1..^1]) + + --- output + ... "nixos config attr" + flags: + yes: + T bool + ? "skip all confirmation prompts" + run: + echo nixosConfigAttrs().join(" ") + + --- update + ... "update and run nixos-rebuild" + flags: + yes: + T bool + ? "skip all confirmation prompts" + preview: + T bool + ? "show preview and exit" + run: + let hosts = getHosts() + if hosts.len > 1: fatalQuit "operation only supports one host" + let run = getLastUpdateRun() + echo fmt"run created at: {run.created_at}" + echo "nvd diff:\n", getUpdateSummary(run.id, hosts[0]) + if preview: quit 0 + if yes or confirm("Proceed with system update?"): + updateRepo() + nixosRebuild("switch") +