better cli
This commit is contained in:
1 parent
53cbaf603a
commit
d3b711046c
5 files changed
+173
-147
No files matched your search
+119
@@ -0,0 +1,119 @@
|
||||
use types.nu *
|
||||
use profile.nu *
|
||||
|
||||
const REPO_ROOT = path self | path dirname | path dirname | path dirname
|
||||
|
||||
export def skyg [] {
|
||||
print $REPO_ROOT
|
||||
print "Subcommands: profiles, build, switch, test, rollback, update, remote, openwrt, build-iso, hm, decrypt, encrypt"
|
||||
print "Run: skyg <subcommand> --help"
|
||||
}
|
||||
|
||||
export def "skyg profiles" [only_remote: bool = false] {
|
||||
if $only_remote {
|
||||
valid_remote_profiles | print
|
||||
} else {
|
||||
valid_profiles | print
|
||||
}
|
||||
}
|
||||
|
||||
# Build configuration
|
||||
export def "skyg build" [] {
|
||||
cd $REPO_ROOT
|
||||
nh os build | print
|
||||
}
|
||||
|
||||
# Switch configuration
|
||||
export def "skyg switch" [] {
|
||||
cd $REPO_ROOT
|
||||
nh os switch --ask | print
|
||||
}
|
||||
|
||||
# test configuration
|
||||
export def "skyg test" [] {
|
||||
cd $REPO_ROOT
|
||||
nh os test | print
|
||||
}
|
||||
|
||||
# rollback configuration
|
||||
export def "skyg rollback" [] {
|
||||
sudo nixos-rebuild switch --rollback
|
||||
}
|
||||
|
||||
# update some flakes
|
||||
export def "skyg update" [input_name: string = ""] {
|
||||
cd $REPO_ROOT
|
||||
if $input_name == "" {
|
||||
print "Updating all flakes..."
|
||||
nix flake update | print
|
||||
} else {
|
||||
print $"Updating flake: ($input_name)"
|
||||
nix flake update $input_name | print
|
||||
}
|
||||
git diff flake.lock | print
|
||||
}
|
||||
|
||||
# Deploy to remote machine
|
||||
export def "skyg remote" [
|
||||
cmd: string@remote-cmds,
|
||||
target: string@remote_targets = "",
|
||||
] {
|
||||
cd $REPO_ROOT
|
||||
mut target_host = $"root@($target).lab.internal"
|
||||
mut profile = $"hl-($target)"
|
||||
if not (is-valid-profile $profile true) {
|
||||
print $"Invalid Remote Profile '(ansi green_bold)($target)(ansi reset)', please select one of:"
|
||||
return (remote_targets)
|
||||
}
|
||||
$"Using [(ansi green_bold)($profile)(ansi reset)]" | print
|
||||
$"($cmd) on ($target_host) with ($profile)" | print
|
||||
mut runcmd = $"nh os ($cmd) --target-host ($target_host) --ask .#($profile)"
|
||||
$runcmd | print
|
||||
bash -c $runcmd
|
||||
}
|
||||
|
||||
# Deploy OpenWrt router configuration
|
||||
export def "skyg openwrt" [router: string = "glmain"] {
|
||||
cd $REPO_ROOT
|
||||
bash -c $"age -d -i ~/.ssh/id_ed25519 secrets/($router).json.age | nix run .#openwrt-($router)"
|
||||
}
|
||||
|
||||
# Build ISO image
|
||||
export def "skyg build-iso" [] {
|
||||
cd $REPO_ROOT
|
||||
nix build .#nixosConfigurations.iso.config.system.build.isoImage
|
||||
ls -l result/iso
|
||||
}
|
||||
|
||||
# Home Manager operations
|
||||
export def "skyg hm" [action: string@hm-actions, profile: string = "ari"] {
|
||||
cd $REPO_ROOT
|
||||
$"Running home-manager ($action) for profile ($profile)" | print
|
||||
home-manager $action --flake $".#($profile)"
|
||||
}
|
||||
|
||||
# Decrypt a secret from secrets/ folder
|
||||
export def "skyg decrypt" [secret: string@secret-names] {
|
||||
cd $REPO_ROOT
|
||||
let src = $"secrets/($secret).age"
|
||||
if not ($src | path exists) {
|
||||
error make { msg: $"Secret not found: ($src)" }
|
||||
}
|
||||
mkdir .tmp
|
||||
let dest = $".tmp/unencrypted-($secret)"
|
||||
age -d -i ~/.ssh/id_ed25519 $src | save -f $dest
|
||||
print $"Decrypted to ($dest)"
|
||||
}
|
||||
|
||||
# Encrypt a file to secrets/ folder (re-encrypts using agenix)
|
||||
export def "skyg encrypt" [secret: string@secret-names, source?: string] {
|
||||
cd $REPO_ROOT
|
||||
let src = $source | default $".tmp/unencrypted-($secret)"
|
||||
if not ($src | path exists) {
|
||||
error make { msg: $"Source file not found: ($src)" }
|
||||
}
|
||||
let dest = $"secrets/($secret).age"
|
||||
cp $src $dest
|
||||
agenix -r -i ~/.ssh/id_ed25519
|
||||
print $"Encrypted ($dest)"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
const REPO_ROOT = path self | path dirname | path dirname | path dirname
|
||||
|
||||
export def valid_profiles [] {
|
||||
cd $REPO_ROOT
|
||||
nix eval .#nixosConfigurations --apply 'builtins.attrNames' --json err> /dev/null
|
||||
| from json
|
||||
}
|
||||
|
||||
export def valid_remote_profiles [] {
|
||||
valid_profiles | where {|p| $p | str starts-with "hl-" }
|
||||
}
|
||||
|
||||
export def remote_targets [] {
|
||||
valid_remote_profiles | each {|p| $p | str replace "hl-" "" }
|
||||
}
|
||||
|
||||
export def is-valid-profile [name: string, on_remote: bool = false] {
|
||||
let pool = if $on_remote { valid_remote_profiles } else { valid_profiles }
|
||||
($pool | where {|p| $p == $name} | length) == 1
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
export def hm-actions [] {
|
||||
["switch", "build"]
|
||||
}
|
||||
|
||||
export def remote-cmds [] {
|
||||
["test", "dry-activate", "run", "switch", "build"]
|
||||
}
|
||||
|
||||
const REPO_ROOT = path self | path dirname | path dirname | path dirname
|
||||
|
||||
export def secret-names [] {
|
||||
cd $REPO_ROOT
|
||||
ls secrets/*.age | get name | each { |f| $f | path basename | str replace ".age" "" }
|
||||
}
|
||||
@@ -1,145 +1,4 @@
|
||||
#!/usr/bin/env nu
|
||||
|
||||
# Ensure we are located at the bin folder
|
||||
const current_folder = path self | path dirname | path dirname
|
||||
cd $current_folder
|
||||
|
||||
def valid_profiles [] {
|
||||
nix eval .#nixosConfigurations --apply 'builtins.attrNames' --json err> /dev/null |
|
||||
from json
|
||||
}
|
||||
|
||||
def valid_remote_profiles [] {
|
||||
(valid_profiles) | where {|$it | str starts-with "hl" }
|
||||
}
|
||||
|
||||
def is-valid-profile [name: string, on_remote: bool = false] {
|
||||
if $on_remote {
|
||||
(valid_remote_profiles | where {|$it| $it == $name} | length) == 1
|
||||
} else {
|
||||
(valid_profiles | where {|$it| $it == $name} | length) == 1
|
||||
}
|
||||
}
|
||||
|
||||
def main [] {
|
||||
print $current_folder
|
||||
help main
|
||||
}
|
||||
|
||||
def "main profiles" [only_remote: bool = false] {
|
||||
if $only_remote {
|
||||
valid_remote_profiles | print
|
||||
} else {
|
||||
valid_profiles | print
|
||||
}
|
||||
}
|
||||
|
||||
# Build configuration
|
||||
def "main build" [] {
|
||||
nh os build | print
|
||||
}
|
||||
|
||||
# Switch configuration
|
||||
def "main switch" [] {
|
||||
nh os switch --ask | print
|
||||
}
|
||||
|
||||
# test configuration
|
||||
def "main test" [] {
|
||||
nh os test | print
|
||||
}
|
||||
|
||||
# rollback configuration
|
||||
def "main rollback" [] {
|
||||
sudo nixos-rebuild switch --rollback
|
||||
}
|
||||
|
||||
# update some flakes
|
||||
def "main update" [input_name:string = ""] {
|
||||
if $input_name == "" {
|
||||
print "Updating all flakes..."
|
||||
nix flake update | print
|
||||
} else {
|
||||
print "Updating flake: $input_name"
|
||||
nix flake update $input_name | print
|
||||
}
|
||||
git diff flake.lock | print
|
||||
}
|
||||
|
||||
# Deploy to remote machine
|
||||
def "main remote" [cmd:string, target:string = ""] {
|
||||
mut target_host = $"root@($target).lab.internal"
|
||||
mut profile = $"hl-($target)"
|
||||
if not (is-valid-profile $profile true) {
|
||||
print $"Invalid Remote Profile '(ansi green_bold)($target)(ansi reset)', please select one of:"
|
||||
return (valid_remote_profiles | each {|p| $p | str replace "hl-" ""})
|
||||
}
|
||||
$"Using [(ansi green_bold)($profile)(ansi reset)]" | print
|
||||
const valid_cmds = [
|
||||
"test" , "dry-activate" , "run" , "switch" , "build"
|
||||
]
|
||||
if not ($cmd in $valid_cmds) {
|
||||
print $"Invalid command ($cmd), please select one of"
|
||||
return $valid_cmds
|
||||
}
|
||||
$"($cmd) on ($target_host) with ($profile)" | print
|
||||
mut runcmd = $"nh os ($cmd) --target-host ($target_host) --ask .#($profile)"
|
||||
$runcmd | print
|
||||
bash -c $runcmd
|
||||
}
|
||||
|
||||
# Deploy OpenWrt router configuration
|
||||
def "main openwrt" [router: string = "glmain"] {
|
||||
bash -c $"age -d -i ~/.ssh/id_ed25519 secrets/($router).json.age | nix run .#openwrt-($router)"
|
||||
}
|
||||
|
||||
# Build ISO image
|
||||
def "main build-iso" [] {
|
||||
nix build .#nixosConfigurations.iso.config.system.build.isoImage
|
||||
ls -l result/iso
|
||||
}
|
||||
|
||||
# Home Manager operations
|
||||
def "main hm" [action: string@hm-actions, profile: string = "ari"] {
|
||||
let valid_actions = (hm-actions)
|
||||
if not ($action in $valid_actions) {
|
||||
error make {
|
||||
msg: $"Invalid action '($action)'",
|
||||
help: $"Valid actions are: ($valid_actions | str join ', ')"
|
||||
}
|
||||
}
|
||||
$"Running home-manager ($action) for profile ($profile)" | print
|
||||
home-manager $action --flake $".#($profile)"
|
||||
}
|
||||
|
||||
def hm-actions [] {
|
||||
["switch", "build"]
|
||||
}
|
||||
|
||||
def secret-names [] {
|
||||
ls secrets/*.age | get name | each { |f| $f | path basename | str replace ".age" "" }
|
||||
}
|
||||
|
||||
# Decrypt a secret from secrets/ folder
|
||||
def "main decrypt" [secret: string@secret-names] {
|
||||
let src = $"secrets/($secret).age"
|
||||
if not ($src | path exists) {
|
||||
error make { msg: $"Secret not found: ($src)" }
|
||||
}
|
||||
mkdir .tmp
|
||||
let dest = $".tmp/unencrypted-($secret)"
|
||||
age -d -i ~/.ssh/id_ed25519 $src | save -f $dest
|
||||
print $"Decrypted to ($dest)"
|
||||
}
|
||||
|
||||
# Encrypt a file to secrets/ folder (re-encrypts using agenix)
|
||||
def "main encrypt" [secret: string@secret-names, source?: string] {
|
||||
let src = $source | default $".tmp/unencrypted-($secret)"
|
||||
if not ($src | path exists) {
|
||||
error make { msg: $"Source file not found: ($src)" }
|
||||
}
|
||||
let dest = $"secrets/($secret).age"
|
||||
cp $src $dest
|
||||
agenix -r -i ~/.ssh/id_ed25519
|
||||
print $"Encrypted ($dest)"
|
||||
}
|
||||
#!/usr/bin/env bash
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
LIB="$SCRIPT_DIR/lib/cmds.nu"
|
||||
exec nu --commands "use \"$LIB\" *; skyg $*"
|
||||
@@ -130,10 +130,24 @@
|
||||
rustc
|
||||
cargo
|
||||
rust-analyzer
|
||||
nushell
|
||||
];
|
||||
shellHook = ''
|
||||
export PATH=$PATH:$(pwd)/bin
|
||||
${pre-commit-check.shellHook}
|
||||
export PATH=$PATH:$(pwd)/bin
|
||||
export SKYG_LIB="$(pwd)/bin/lib/cmds.nu"
|
||||
${pre-commit-check.shellHook}
|
||||
|
||||
if [ -z "$NU_VERSION" ] && [ -t 0 ] && command -v nu >/dev/null; then
|
||||
cat <<'BANNER'
|
||||
____ _ ____ ____ _ _ _
|
||||
/ ___|| | ___ _/ ___| / ___|| |__ ___| | |
|
||||
\___ \| |/ / | | | | _ \___ \| '_ \ / _ \ | |
|
||||
___) | <| |_| | |_| | ___) | | | | __/ | |
|
||||
|____/|_|\_\\__, |\____| |____/|_| |_|\___|_|_|
|
||||
|___/
|
||||
BANNER
|
||||
exec nu --execute "use \"$SKYG_LIB\" *"
|
||||
fi
|
||||
'';
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user