PortValhalla/scripts/lib/action.fish

88 lines
2.7 KiB
Fish
Raw Normal View History

2024-11-01 14:32:43 +00:00
set -l dir (status dirname)
function runActionSetup -V dir
2024-11-01 14:32:43 +00:00
source "$dir/hooks.fish"
source "$dir/nix.fish"
source "$dir/settings.fish"
2024-11-03 21:32:47 +00:00
echo "Installing dependencies..."
2024-12-05 13:59:28 +00:00
and runHook --force installValhallaDeps 'Please set up a function `installValhallaDeps` for installing `fish`, `git`, `jq`, `nix`, `ssh`, `sudo` and `tmux`.'
2024-11-03 21:32:47 +00:00
and echo "Preparing nix..."
2024-11-01 14:32:43 +00:00
and runHook prepareNix || installNixPkgs
and echo "Flagging repository as safe..."
2024-11-25 14:19:01 +00:00
and sudo git config --system --add safe.directory (realpath -m "$(status dirname)/../..")
and runHook actionPreRun || true
end
2024-11-01 14:32:43 +00:00
2024-11-25 14:19:01 +00:00
function runActionConfigure -V dir
2024-11-05 16:39:06 +00:00
and if [ -z "$CONFIG_NAME" ]
2024-11-25 14:19:01 +00:00
source "$dir/settings.fish"
2024-11-05 16:39:06 +00:00
selectProfile config
2024-11-25 14:19:01 +00:00
set -gx CONFIG_NAME "$config"
2024-11-05 16:39:06 +00:00
end
end
function disposeAction -V dir
source "$dir/hooks.fish"
2024-11-05 16:39:06 +00:00
source "$dir/nix.fish"
2024-11-04 00:34:43 +00:00
runHook actionPostRun || true
2024-11-05 16:39:06 +00:00
and uninstallNixPkgs
2024-11-28 21:12:15 +00:00
and sudo git config --remove-section --system safe || true
end
2024-11-01 14:32:43 +00:00
function runAction -V dir
runActionSetup
2024-11-05 16:39:06 +00:00
and runActionConfigure
and $argv
and disposeAction
2024-11-01 14:32:43 +00:00
end
function runSetupUserAction -V dir
2024-11-25 14:19:01 +00:00
source "$dir/settings.fish"
runActionSetup
runActionConfigure
set -l name (getOSConfig setupUser.name)
if [ "$USER" != "$name" ]
2024-12-05 15:34:24 +00:00
set -l cmdline (cat /proc/$fish_pid/cmdline | string split0 || true)
2024-11-01 14:32:43 +00:00
2024-11-25 14:19:01 +00:00
if [ (id -u) -ne 0 ]
2024-11-27 04:24:53 +00:00
sudo --set-home --preserve-env env "PATH=$PATH" $cmdline
else
2024-11-25 14:19:01 +00:00
if [ -z "$TMUX" ]
tmux new-session env (env) $cmdline
2024-11-25 14:19:01 +00:00
else
set -l sudoConfig "/etc/sudoers.d/PortValhalla"
2024-11-01 14:32:43 +00:00
2024-11-25 14:19:01 +00:00
begin
if id -u "$name" &>/dev/null
echo "Setup user already exists!"
else
echo "Creating setup user"
and useradd \
--comment "PortValhalla Setup User" \
--system \
--groups nix-users \
--create-home \
--uid (getOSConfig setupUser.id --json) \
"$name"
end
2024-11-01 14:32:43 +00:00
2024-11-25 14:19:01 +00:00
and begin
echo "$name ALL=(ALL:ALL) NOPASSWD: ALL"
end >"$sudoConfig"
2024-11-01 14:32:43 +00:00
2024-11-27 04:24:53 +00:00
and sudo --preserve-env --set-home --user "$name" env "PATH=$PATH" $cmdline
2024-12-06 12:52:26 +00:00
or read -P "An error occurred! Press enter to continue: "
2024-11-25 14:19:01 +00:00
disposeAction
rm "$sudoConfig"
userdel -rf "$name"
end
end
2024-11-01 14:32:43 +00:00
end
else
$argv
2024-11-01 14:32:43 +00:00
end
end