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() =
## 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

View file

@ -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

View file

@ -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"

View file

@ -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

View file

@ -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","--"]