Skip to content

Commit 9ffcd19

Browse files
authored
Add ability to run user mod commands with sudo (#35)
1 parent 391ad6d commit 9ffcd19

File tree

1 file changed

+16
-4
lines changed
  • modules/bash-commons/src

1 file changed

+16
-4
lines changed

modules/bash-commons/src/os.sh

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,35 @@ function os_user_exists {
9191
id "$username" >/dev/null 2>&1
9292
}
9393

94-
# Create an OS user whose name is $username
94+
# Create an OS user whose name is $username. If true is passed in as the second arg, run the commands with sudo.
9595
function os_create_user {
9696
local -r username="$1"
97+
local -r with_sudo="$2"
98+
99+
local exuseradd='useradd'
100+
if [[ "$with_sudo" == 'true' ]]; then
101+
exuseradd='sudo useradd'
102+
fi
97103

98104
if os_user_exists "$username"; then
99105
log_info "User $username already exists. Will not create again."
100106
else
101107
log_info "Creating user named $username"
102-
useradd "$username"
108+
"$exuseradd" "$username"
103109
fi
104110
}
105111

106-
# Change the owner of $dir to $username
112+
# Change the owner of $dir to $username. If true is passed in as the last arg, run the command with sudo.
107113
function os_change_dir_owner {
108114
local -r dir="$1"
109115
local -r username="$2"
116+
local -r with_sudo="$3"
117+
118+
local exchown='chown'
119+
if [[ "$with_sudo" == 'true' ]]; then
120+
exchown='sudo chown'
121+
fi
110122

111123
log_info "Changing ownership of $dir to $username"
112-
chown -R "$username:$username" "$dir"
124+
"$exchown" -R "$username:$username" "$dir"
113125
}

0 commit comments

Comments
 (0)