diff --git a/pkgs/oizys-nim/src/oizys.nim b/pkgs/oizys-nim/src/oizys.nim index 0602032..4cccb73 100644 --- a/pkgs/oizys-nim/src/oizys.nim +++ b/pkgs/oizys-nim/src/oizys.nim @@ -46,24 +46,23 @@ overlay: proc osCmd() = ## nixos-rebuild - if len(rest) == 0: quit "please provide subcmd" + if len(rest) == 0: fatalQuit "please provide subcmd" let subcmd = rest[0] if subcmd notin nixosSubcmds: - error ( + fatalQuit( &"unknown nixos-rebuild subcmd: {subcmd}\nexpected one of: \n" & nixosSubcmds.mapIt(" " & it).join("\n") - ); quit QuitFailure + ) nixosRebuild(subcmd, rest[1..^1]) proc ci(`ref`: string = "main") = ## trigger GHA update flow - if rest.len == 0: - fatal "expected workflow file name"; quit QuitFailure + if rest.len == 0: fatalQuit "expected workflow file name" createDispatch(rest[0], `ref`) proc checkExes() = if findExe("nix") == "": - quit("oizys requires nix", QuitFailure) + fatalQuit "oizys requires nix" proc `//`(t1: Table[string, string], t2: Table[string, string]): Table[string, string] = # nix style shallow table merge diff --git a/pkgs/oizys-nim/src/oizys/context.nim b/pkgs/oizys-nim/src/oizys/context.nim index 240039a..7cafc7f 100644 --- a/pkgs/oizys-nim/src/oizys/context.nim +++ b/pkgs/oizys-nim/src/oizys/context.nim @@ -1,6 +1,8 @@ import std/[logging, os, strformat, strutils] from std/nativesockets import getHostname +import ./logging + type OizysContext* = object flake, host: string @@ -24,8 +26,7 @@ var oc = initContext() proc checkPath(s: string): string = ## fail if path doesn't exist if not s.dirExists: - error fmt"flake path: {s} does not exist" - quit() + errorQuit fmt"flake path: {s} does not exist" s # public api ------------------------------------- @@ -44,6 +45,7 @@ proc updateContext*( oc.flake = if flake.startsWith("github") or flake.startsWith("git+"): flake else: checkPath(flake.normalizedPath().absolutePath()) + debug oc proc getHosts*(): seq[string] = return oc.hosts proc getFlake*(): string = return oc.flake diff --git a/pkgs/oizys-nim/src/oizys/github.nim b/pkgs/oizys-nim/src/oizys/github.nim index 5cc7822..924b771 100644 --- a/pkgs/oizys-nim/src/oizys/github.nim +++ b/pkgs/oizys-nim/src/oizys/github.nim @@ -1,7 +1,11 @@ import std/[httpclient,logging, os, strformat, strutils, json] +import ./logging var ghToken = getEnv("GITHUB_TOKEN") +proc checkToken() {.inline.} = + if ghToken == "": fatalQuit "GITHUB_TOKEN not set" + #[curl -L \ -X POST \ -H "Accept: application/vnd.github+json" \ @@ -11,16 +15,17 @@ var ghToken = getEnv("GITHUB_TOKEN") -d '{"ref":"topic-branch","inputs":{"name":"Mona the Octocat","home":"San Francisco, CA"}}' ]# + proc postGhApi(url: string, body: JsonNode) = - if ghToken == "": fatal "GITHUB_TOKEN not set"; quit QuitFailure + checkToken() let client = newHttpClient() client.headers = newHttpHeaders({ "Accept" : "application/vnd.github+json", "Authorization" : fmt"Bearer {ghToken}", "X-GitHub-Api-Version": "2022-11-28", }) - let response = client.post(url, body = $body) try: + let response = client.post(url, body = $body) info fmt"Status: {response.code}" except: error "failed to get response code" diff --git a/pkgs/oizys-nim/src/oizys/logging.nim b/pkgs/oizys-nim/src/oizys/logging.nim index da05bbc..708a43a 100644 --- a/pkgs/oizys-nim/src/oizys/logging.nim +++ b/pkgs/oizys-nim/src/oizys/logging.nim @@ -106,3 +106,12 @@ method log*(logger: FancyConsoleLogger, level: Level, args: varargs[string, `$`] proc addHandlers*(handler: Logger) = handlers.add(handler) + +template errorQuit*(args: varargs[string, `$`]) = + error args + quit QuitFailure + +template fatalQuit*(args: varargs[string, `$`]) = + error args + quit QuitFailure + diff --git a/pkgs/oizys-nim/src/oizys/nix.nim b/pkgs/oizys-nim/src/oizys/nix.nim index 68a0364..9bccf33 100644 --- a/pkgs/oizys-nim/src/oizys/nix.nim +++ b/pkgs/oizys-nim/src/oizys/nix.nim @@ -4,7 +4,7 @@ import std/[ strutils, sugar, logging, tables ] import bbansi, jsony -import ./[context, exec] +import ./[context, exec, logging] proc nixCommand(cmd: string): string = @@ -24,8 +24,7 @@ const nixosSubcmds* = proc nixosRebuild*(subcmd: string, rest: seq[string] = @[]) = if getHosts().len > 1: - error "nixos-rebuild only supports one host" - quit QuitFailure + fatalQuit "nixos-rebuild only supports one host" var cmd = fmt"sudo nixos-rebuild {subcmd} --flake {getFlake()} --log-format multiline" cmd.addArgs rest quitWithCmd cmd @@ -77,8 +76,8 @@ proc parseDryRunOutput(err: string): DryRunOutput = stderr.writeLine err quit() of 0: - info "nothing to do"; - quit(QuitSuccess) + info "nothing to do" + quit QuitSuccess else: fatal "unexpected output from nix" stderr.writeLine err @@ -88,7 +87,7 @@ proc parseDryRunOutput(err: string): DryRunOutput = result.toFetch.sort(cmpDrv) proc trunc(s: string, limit: int): string = - if s.len <= limit: + if s.len <= limit: s else: s[0..(limit-4)] & "..." @@ -175,9 +174,7 @@ proc writeDervationsToStepSummary(drvs: seq[string]) = fmt"| {name} | {hash} |" ) let summaryFilePath = getEnv("GITHUB_STEP_SUMMARY") - if summaryFilePath == "": - fatal "no github step summary found" - quit QuitFailure + if summaryFilePath == "": fatalQuit "no github step summary found" let output = open(summaryFilePath,fmAppend) output.writeLine("| derivation | hash |\n|---|---|") output.writeLine(rows.join("\n")) @@ -226,8 +223,7 @@ template `bbfmt`(pattern: static string): untyped = bb(fmt(pattern)) proc nixBuildWithCache*(minimal: bool, name: string, rest:seq[string]) = - if findExe("cachix") == "": - fatal "is cachix installed?"; quit QuitFailure + if findExe("cachix") == "": fatalQuit "is cachix installed?" info bbfmt"building and pushing to cache: [b]{name}" var cmd = "cachix" cmd.addArgs ["watch-exec","--"]