better quitting

This commit is contained in:
Daylin Morgan 2024-09-09 10:45:44 -05:00
parent 8347ad4a55
commit 908ab5189a
Signed by: daylin
GPG key ID: 950D13E9719334AD
5 changed files with 32 additions and 21 deletions

View file

@ -46,24 +46,23 @@ overlay:
proc osCmd() = proc osCmd() =
## nixos-rebuild ## nixos-rebuild
if len(rest) == 0: quit "please provide subcmd" if len(rest) == 0: fatalQuit "please provide subcmd"
let subcmd = rest[0] let subcmd = rest[0]
if subcmd notin nixosSubcmds: if subcmd notin nixosSubcmds:
error ( fatalQuit(
&"unknown nixos-rebuild subcmd: {subcmd}\nexpected one of: \n" & &"unknown nixos-rebuild subcmd: {subcmd}\nexpected one of: \n" &
nixosSubcmds.mapIt(" " & it).join("\n") nixosSubcmds.mapIt(" " & it).join("\n")
); quit QuitFailure )
nixosRebuild(subcmd, rest[1..^1]) nixosRebuild(subcmd, rest[1..^1])
proc ci(`ref`: string = "main") = proc ci(`ref`: string = "main") =
## trigger GHA update flow ## trigger GHA update flow
if rest.len == 0: if rest.len == 0: fatalQuit "expected workflow file name"
fatal "expected workflow file name"; quit QuitFailure
createDispatch(rest[0], `ref`) createDispatch(rest[0], `ref`)
proc checkExes() = proc checkExes() =
if findExe("nix") == "": if findExe("nix") == "":
quit("oizys requires nix", QuitFailure) fatalQuit "oizys requires nix"
proc `//`(t1: Table[string, string], t2: Table[string, string]): Table[string, string] = proc `//`(t1: Table[string, string], t2: Table[string, string]): Table[string, string] =
# nix style shallow table merge # nix style shallow table merge

View file

@ -1,6 +1,8 @@
import std/[logging, os, strformat, strutils] import std/[logging, os, strformat, strutils]
from std/nativesockets import getHostname from std/nativesockets import getHostname
import ./logging
type type
OizysContext* = object OizysContext* = object
flake, host: string flake, host: string
@ -24,8 +26,7 @@ var oc = initContext()
proc checkPath(s: string): string = proc checkPath(s: string): string =
## fail if path doesn't exist ## fail if path doesn't exist
if not s.dirExists: if not s.dirExists:
error fmt"flake path: {s} does not exist" errorQuit fmt"flake path: {s} does not exist"
quit()
s s
# public api ------------------------------------- # public api -------------------------------------
@ -44,6 +45,7 @@ proc updateContext*(
oc.flake = oc.flake =
if flake.startsWith("github") or flake.startsWith("git+"): flake if flake.startsWith("github") or flake.startsWith("git+"): flake
else: checkPath(flake.normalizedPath().absolutePath()) else: checkPath(flake.normalizedPath().absolutePath())
debug oc
proc getHosts*(): seq[string] = return oc.hosts proc getHosts*(): seq[string] = return oc.hosts
proc getFlake*(): string = return oc.flake proc getFlake*(): string = return oc.flake

View file

@ -1,7 +1,11 @@
import std/[httpclient,logging, os, strformat, strutils, json] import std/[httpclient,logging, os, strformat, strutils, json]
import ./logging
var ghToken = getEnv("GITHUB_TOKEN") var ghToken = getEnv("GITHUB_TOKEN")
proc checkToken() {.inline.} =
if ghToken == "": fatalQuit "GITHUB_TOKEN not set"
#[curl -L \ #[curl -L \
-X POST \ -X POST \
-H "Accept: application/vnd.github+json" \ -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"}}' -d '{"ref":"topic-branch","inputs":{"name":"Mona the Octocat","home":"San Francisco, CA"}}'
]# ]#
proc postGhApi(url: string, body: JsonNode) = proc postGhApi(url: string, body: JsonNode) =
if ghToken == "": fatal "GITHUB_TOKEN not set"; quit QuitFailure checkToken()
let client = newHttpClient() let client = newHttpClient()
client.headers = newHttpHeaders({ client.headers = newHttpHeaders({
"Accept" : "application/vnd.github+json", "Accept" : "application/vnd.github+json",
"Authorization" : fmt"Bearer {ghToken}", "Authorization" : fmt"Bearer {ghToken}",
"X-GitHub-Api-Version": "2022-11-28", "X-GitHub-Api-Version": "2022-11-28",
}) })
let response = client.post(url, body = $body)
try: try:
let response = client.post(url, body = $body)
info fmt"Status: {response.code}" info fmt"Status: {response.code}"
except: except:
error "failed to get response code" error "failed to get response code"

View file

@ -106,3 +106,12 @@ method log*(logger: FancyConsoleLogger, level: Level, args: varargs[string, `$`]
proc addHandlers*(handler: Logger) = proc addHandlers*(handler: Logger) =
handlers.add(handler) handlers.add(handler)
template errorQuit*(args: varargs[string, `$`]) =
error args
quit QuitFailure
template fatalQuit*(args: varargs[string, `$`]) =
error args
quit QuitFailure

View file

@ -4,7 +4,7 @@ import std/[
strutils, sugar, logging, tables strutils, sugar, logging, tables
] ]
import bbansi, jsony import bbansi, jsony
import ./[context, exec] import ./[context, exec, logging]
proc nixCommand(cmd: string): string = proc nixCommand(cmd: string): string =
@ -24,8 +24,7 @@ const nixosSubcmds* =
proc nixosRebuild*(subcmd: string, rest: seq[string] = @[]) = proc nixosRebuild*(subcmd: string, rest: seq[string] = @[]) =
if getHosts().len > 1: if getHosts().len > 1:
error "nixos-rebuild only supports one host" fatalQuit "nixos-rebuild only supports one host"
quit QuitFailure
var cmd = fmt"sudo nixos-rebuild {subcmd} --flake {getFlake()} --log-format multiline" var cmd = fmt"sudo nixos-rebuild {subcmd} --flake {getFlake()} --log-format multiline"
cmd.addArgs rest cmd.addArgs rest
quitWithCmd cmd quitWithCmd cmd
@ -77,8 +76,8 @@ proc parseDryRunOutput(err: string): DryRunOutput =
stderr.writeLine err stderr.writeLine err
quit() quit()
of 0: of 0:
info "nothing to do"; info "nothing to do"
quit(QuitSuccess) quit QuitSuccess
else: else:
fatal "unexpected output from nix" fatal "unexpected output from nix"
stderr.writeLine err stderr.writeLine err
@ -175,9 +174,7 @@ proc writeDervationsToStepSummary(drvs: seq[string]) =
fmt"| {name} | {hash} |" fmt"| {name} | {hash} |"
) )
let summaryFilePath = getEnv("GITHUB_STEP_SUMMARY") let summaryFilePath = getEnv("GITHUB_STEP_SUMMARY")
if summaryFilePath == "": if summaryFilePath == "": fatalQuit "no github step summary found"
fatal "no github step summary found"
quit QuitFailure
let output = open(summaryFilePath,fmAppend) let output = open(summaryFilePath,fmAppend)
output.writeLine("| derivation | hash |\n|---|---|") output.writeLine("| derivation | hash |\n|---|---|")
output.writeLine(rows.join("\n")) output.writeLine(rows.join("\n"))
@ -226,8 +223,7 @@ template `bbfmt`(pattern: static string): untyped =
bb(fmt(pattern)) bb(fmt(pattern))
proc nixBuildWithCache*(minimal: bool, name: string, rest:seq[string]) = proc nixBuildWithCache*(minimal: bool, name: string, rest:seq[string]) =
if findExe("cachix") == "": if findExe("cachix") == "": fatalQuit "is cachix installed?"
fatal "is cachix installed?"; quit QuitFailure
info bbfmt"building and pushing to cache: [b]{name}" info bbfmt"building and pushing to cache: [b]{name}"
var cmd = "cachix" var cmd = "cachix"
cmd.addArgs ["watch-exec","--"] cmd.addArgs ["watch-exec","--"]