-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_env.fish
67 lines (56 loc) · 1.3 KB
/
create_env.fish
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/fish
# Create environment
#
# @return [None]
#
function create_env
argparse -n create_env 'h/help' 'u/user=' 'g/group=' 's/shell=' -- $argv
# show help
if set -lq _flag_h
show_help
return
end
# create user
# https://stackoverflow.com/questions/52025906/can-function-get-empty-arguments
create_user "$_flag_u" "$_flag_g" "$_flag_s"
end
# Show help information
#
# @return [String] help information
#
function show_help
string trim '
h: help
u: user you want to add
g: group of user you want to add
s: login shell
'
end
# Create user
#
# @param [String] user user name
# @param [String] group group name
# @param [String] shell login shell
# @return [None]
#
function create_user -a user group shell
set -l useradd_opts ''
set -l separator ' '
# TODO(from fish 3.0)
# https://github.com/fish-shell/fish-shell/issues/1326
if test -n $user
set useradd_opts $user
else
echo 'u/user is required.'
exit
end
if test -n $group
set useradd_opts (string join $separator -- $useradd_opts -g $group)
end
if test -n $shell
set useradd_opts (string join $separator -- $useradd_opts -s $shell)
end
useradd (string split $separator $useradd_opts)
passwd $user
end
create_env $argv