commit f52676e97c775bc3bacc8617d92899b6fa267cae
parent 4a51fc40073313ed0f60e7b41d9e4aee3502a641
Author: Matthias Jaros <jaros@mailbox.org>
Date: Sun, 9 Nov 2025 19:36:18 +0100
Added fail helper function that gives formatted output returns nonzero
status
Diffstat:
| M | src/init | | | 39 | +++++++++++++++++++++++++-------------- |
1 file changed, 25 insertions(+), 14 deletions(-)
diff --git a/src/init b/src/init
@@ -9,6 +9,13 @@ source (status dirname)/env
#
#
+function fail
+ set_color red
+ echo "$argv"
+ set_color normal
+ return 1
+end
+
function checkUser
set -l user $argv[1]
cat /etc/passwd | grep -q -E "^$user:"
@@ -19,22 +26,26 @@ function setupUser
set -l userConfig (string split ':' $argv[1])
set -l user $userConfig[1]
set -l key $userConfig[2]
- checkUser "$user"; and return 0
+ set -l HOME "/home/$user"
echo "Setting up user: $user"
if [ "$user" = git ]
echo "Error: User git is reserved"
return 1
end
-
- set -l HOME "/home/$user"
- # user is not restricted, this is done in ssh config with forceCommand
- # we need to be able to run a fish script to check for permissions
- adduser --home $HOME -D "$user"
- # add user to sudo group
- addgroup "$user" $_GIT_SUDO_GROUP
- # unlock account to allow ssh logins
- passwd -u "$user" &>/dev/null
+
+ # setup user exist if necessary
+ if checkUser "$user"
+ # user is not restricted, this is done in ssh config with forceCommand
+ # we need to be able to run a fish script to check for permissions
+ adduser --home $HOME -D "$user"; or fail "Failed to create user $user"
+ # add user to sudo group
+ addgroup "$user" $_GIT_SUDO_GROUP; or fail "Failed to add user $user to group $_GIT_SUDO_GROUP"
+ # unlock account to allow ssh logins
+ passwd -u "$user" &>/dev/null; or fail "Failed to unlock account of $user"
+ end
+
+ # Copy/Overwrite ssh key
mkdir "$HOME/.ssh"
echo "$key" > "$HOME/.ssh/authorized_keys"
chown -R $user "$HOME"
@@ -46,10 +57,10 @@ function setupGitUser
echo "Setting up git user"
# git user has read/write access to all repos, set git-shell
# and no password login allowed -> no ssh login possible
- addgroup -g $GIT_GID git
- adduser git -u $GIT_UID -G git -D --shell "/usr/bin/git-shell"
+ addgroup -g $GIT_GID git; or fail "Failed to create git user group"
+ adduser git -u $GIT_UID -G git -D --shell "/usr/bin/git-shell"; or fail "Failed to setup git user"
# group that is allowed to sudo
- addgroup $_GIT_SUDO_GROUP
+ addgroup $_GIT_SUDO_GROUP; or fail "Failed to setup git sudo group"
end
function setupRepo
@@ -60,7 +71,7 @@ function setupRepo
# check repo name
set -l matcher (string match -r -g "^$_REPO_VALIDATION_REGEX\$" -- $repo)
if test -z $matcher[1]
- echo "Failed to setup repo, invalid config: $repoConfig"
+ fail "Failed to setup repo, invalid config: $repoConfig"
return 1
end