mirror of
https://github.com/daylinmorgan/oizys.git
synced 2024-12-22 18:30:43 -06:00
improve command line parsing in oizys cli
This commit is contained in:
parent
b63bd12d6a
commit
8766668a1c
2 changed files with 23 additions and 15 deletions
|
@ -4,7 +4,6 @@
|
||||||
pkgs,
|
pkgs,
|
||||||
...
|
...
|
||||||
}: let
|
}: let
|
||||||
|
|
||||||
inherit (lib) mkIfIn;
|
inherit (lib) mkIfIn;
|
||||||
cfg = config.oizys.languages;
|
cfg = config.oizys.languages;
|
||||||
in {
|
in {
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
import std/[os, osproc, tables, times, strutils, terminal]
|
import std/[os, osproc, parseopt, times, strutils, terminal]
|
||||||
from std/nativesockets import getHostname
|
from std/nativesockets import getHostname
|
||||||
|
|
||||||
let summaryFile = getEnv("GITHUB_STEP_SUMMARY")
|
let summaryFile = getEnv("GITHUB_STEP_SUMMARY")
|
||||||
|
|
||||||
|
|
||||||
proc info(args: varargs[string, `$`]) =
|
proc info(args: varargs[string, `$`]) =
|
||||||
stdout.styledWriteLine(
|
stdout.styledWriteLine(
|
||||||
fgCyan, "oizys", resetStyle, "|",
|
fgCyan, "oizys", resetStyle, "|",
|
||||||
|
@ -18,16 +17,18 @@ proc error(args: varargs[string, `$`]) =
|
||||||
args.join("")
|
args.join("")
|
||||||
)
|
)
|
||||||
|
|
||||||
proc execQuit(cmd: string) =
|
|
||||||
info "exec: ", cmd
|
|
||||||
quit (execCmd cmd)
|
|
||||||
|
|
||||||
type
|
type
|
||||||
OizysContext = object
|
OizysContext = object
|
||||||
flake, host: string
|
flake, host: string
|
||||||
|
extraArgs: seq[string]
|
||||||
cache = "daylin"
|
cache = "daylin"
|
||||||
pinix: bool = true
|
pinix: bool = true
|
||||||
|
|
||||||
|
proc execQuit(c: OizysContext, args: varargs[string]) =
|
||||||
|
let cmd = (@args & c.extraArgs).join(" ")
|
||||||
|
info "exec: ", cmd
|
||||||
|
quit (execCmd cmd)
|
||||||
|
|
||||||
proc newCtx(): OizysContext =
|
proc newCtx(): OizysContext =
|
||||||
result = OizysContext()
|
result = OizysContext()
|
||||||
result.flake = getEnv("FLAKE_PATH", getEnv("HOME") / "oizys")
|
result.flake = getEnv("FLAKE_PATH", getEnv("HOME") / "oizys")
|
||||||
|
@ -49,11 +50,11 @@ proc systemFlakePath(c: OizysContext): string =
|
||||||
|
|
||||||
proc build(c: OizysContext) =
|
proc build(c: OizysContext) =
|
||||||
## build nixos
|
## build nixos
|
||||||
execQuit c.cmd & " build " & c.systemFlakePath
|
execQuit c, c.cmd, "build", c.systemFlakePath
|
||||||
|
|
||||||
proc dry(c: OizysContext) =
|
proc dry(c: OizysContext) =
|
||||||
## poor man's nix flake check
|
## poor man's nix flake check
|
||||||
execQuit c.cmd & " build " & c.systemFlakePath & " --dry-run"
|
execQuit c, c.cmd, "build", c.systemFlakePath, "--dry-run"
|
||||||
|
|
||||||
proc cache(c: OizysContext) =
|
proc cache(c: OizysContext) =
|
||||||
let start = now()
|
let start = now()
|
||||||
|
@ -78,7 +79,7 @@ proc cache(c: OizysContext) =
|
||||||
|
|
||||||
proc nixosRebuild(c: OizysContext, subcmd: string) =
|
proc nixosRebuild(c: OizysContext, subcmd: string) =
|
||||||
let cmd = if c.pinix: "pixos-rebuild" else: "nixos-rebuild"
|
let cmd = if c.pinix: "pixos-rebuild" else: "nixos-rebuild"
|
||||||
execQuit "sudo " & cmd & " " & subcmd & " " & " --flake " & c.flake
|
execQuit c, "sudo", cmd, subcmd, "--flake", c.flake
|
||||||
|
|
||||||
proc boot(c: OizysContext) =
|
proc boot(c: OizysContext) =
|
||||||
## nixos rebuild boot
|
## nixos rebuild boot
|
||||||
|
@ -120,7 +121,7 @@ proc runCmd(c: OizysContext, cmd: string) =
|
||||||
quit 1
|
quit 1
|
||||||
|
|
||||||
|
|
||||||
proc parseFlag(c: var OizysContext, key, val: string) =
|
proc parseFlag(c: var OizysContext, kind: CmdLineKind, key, val: string) =
|
||||||
case key:
|
case key:
|
||||||
of "h", "help":
|
of "h", "help":
|
||||||
echo usage; quit 0
|
echo usage; quit 0
|
||||||
|
@ -130,23 +131,31 @@ proc parseFlag(c: var OizysContext, key, val: string) =
|
||||||
c.flake = val
|
c.flake = val
|
||||||
of "no-pinix":
|
of "no-pinix":
|
||||||
c.pinix = false
|
c.pinix = false
|
||||||
|
else:
|
||||||
|
c.extraArgs.add (if kind == cmdLongOption: "--" else: "-") & key
|
||||||
|
c.extraArgs.add val
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
import std/parseopt
|
|
||||||
var
|
var
|
||||||
c = newCtx()
|
c = newCtx()
|
||||||
subcmd: string
|
subcmd: string
|
||||||
for kind, key, val in getopt(longNoVal = @["no-nom"]):
|
var p = initOptParser(
|
||||||
|
longNoVal = @["no-pinix", "help", ""], shortNoVal = {'h'}
|
||||||
|
)
|
||||||
|
for kind, key, val in p.getopt():
|
||||||
case kind
|
case kind
|
||||||
of cmdArgument:
|
of cmdArgument:
|
||||||
|
echo key
|
||||||
subcmd = key
|
subcmd = key
|
||||||
of cmdLongOption, cmdShortOption:
|
of cmdLongOption, cmdShortOption:
|
||||||
parseFlag c, key, val
|
if key == "":
|
||||||
|
break
|
||||||
|
parseFlag c, kind, key, val
|
||||||
of cmdEnd:
|
of cmdEnd:
|
||||||
discard
|
discard
|
||||||
if subcmd == "":
|
if subcmd == "":
|
||||||
echo "please specify a command"
|
echo "please specify a command"
|
||||||
echo usage; quit 1
|
echo usage; quit 1
|
||||||
|
c.extraArgs = p.remainingArgs
|
||||||
check c
|
check c
|
||||||
runCmd c, subcmd
|
runCmd c, subcmd
|
||||||
|
|
Loading…
Reference in a new issue