Skip to content

Commit 5e1e8f1

Browse files
committed
Giving sudo keyword in templates
1 parent 3511905 commit 5e1e8f1

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

generate.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def get_method_case(method):
2020
logging.error('Unpupported method in the TOML file, method: '+method)
2121
exit(1)
2222

23+
def parse_line(line):
24+
line = line.replace('@sudo', '$SUDO')
25+
return line
26+
2327
def generate(path):
2428

2529
installer_toml_path = path+"/installer.toml"
@@ -38,6 +42,21 @@ def generate(path):
3842
PACMAN_CMD=$(which pacman) # pacman package manager for ArchLinux
3943
APK_CMD=$(which apk) # apk package manager for Alpine
4044
GIT_CMD=$(which git) # to build from source pulling from git
45+
SUDO_CMD=$(which sudo) # check if sudo command is there
46+
47+
USER="$(id -un 2>/dev/null || true)"
48+
SUDO=''
49+
if [ "$USER" != 'root' ]; then
50+
if $SUDO_CMD; then
51+
SUDO='sudo'
52+
else
53+
cat >&2 <<-'EOF'
54+
Error: this installer needs the ability to run commands as root.
55+
We are unable to find "sudo". Make sure its available to make this happen
56+
EOF
57+
exit 1
58+
fi
59+
fi
4160
4261
""")
4362

@@ -47,7 +66,8 @@ def generate(path):
4766
lines = parsed_toml[section]['sh']
4867
installer_sh.write(seperator+" "+get_method_case(section))
4968
for line in lines.split("\n"):
50-
installer_sh.write(" "+line+"\n")
69+
step = parse_line(line)
70+
installer_sh.write(" "+step+"\n")
5171
seperator = "elif"
5272

5373
installer_sh.write("""

installers/git/installer.sh

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#!/bin/sh
2-
2+
33
YUM_CMD=$(which yum) # yum package manager for RHEL & CentOS
44
DNF_CMD=$(which dnf) # dnf package manager for new RHEL & CentOS
55
APT_GET_CMD=$(which apt-get) # apt package manager for Ubuntu & other Debian based distributions
66
PACMAN_CMD=$(which pacman) # pacman package manager for ArchLinux
77
APK_CMD=$(which apk) # apk package manager for Alpine
8+
GIT_CMD=$(which git) # to build from source pulling from git
9+
SUDO_CMD=$(which sudo) # check if sudo command is there
810

911
USER="$(id -un 2>/dev/null || true)"
10-
PREFIX=''
12+
SUDO=''
1113
if [ "$USER" != 'root' ]; then
12-
if command_exists sudo; then
13-
PREFIX='sudo'
14+
if [ ! -z $SUDO_CMD ]; then
15+
SUDO='sudo'
1416
else
1517
cat >&2 <<-'EOF'
1618
Error: this installer needs the ability to run commands as root.
@@ -20,20 +22,25 @@ if [ "$USER" != 'root' ]; then
2022
fi
2123
fi
2224

23-
if [ ! -z $APT_GET_CMD ]; then
24-
$PREFIX apt-get update
25-
$PREFIX apt-get install git
26-
elif [ ! -z $DNF_CMD ]; then
27-
$PREFIX dnf install git
28-
elif [ ! -z $YUM_CMD ]; then
29-
$PREFIX yum install git
30-
elif [ ! -z $PACMAN_CMD ]; then
31-
pacman -Sy git
32-
elif [ ! -z $APK_CMD ]; then
33-
$PREFIX apk add git
34-
else
35-
echo "Couldn't find an installer matching to this package"
36-
exit 1;
37-
fi
25+
echo $SUDO
3826

39-
git --version
27+
if [ ! -z $APT_GET_CMD ]; then
28+
$SUDO apt-get update
29+
$SUDO apt-get install git
30+
31+
elif [ ! -z $YUM_CMD ]; then
32+
$SUDO yum install git
33+
34+
elif [ ! -z $PACMAN_CMD ]; then
35+
pacman -Sy git
36+
37+
elif [ ! -z $DNF_CMD ]; then
38+
$SUDO dnf install git
39+
40+
elif [ ! -z $APK_CMD ]; then
41+
$SUDO apk add git
42+
43+
else
44+
echo "Couldn't install package"
45+
exit 1;
46+
fi

installers/git/installer.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[apt]
22
sh = """
3-
sudo apt-get update
4-
sudo apt-get install git
3+
@sudo apt-get update
4+
@sudo apt-get install git
55
"""
66

77
[yum]
88
sh = """
9-
sudo yum install git
9+
@sudo yum install git
1010
"""
1111

1212
[pacman]
@@ -16,10 +16,10 @@ pacman -Sy git
1616

1717
[dnf]
1818
sh = """
19-
sudo dnf install git
19+
@sudo dnf install git
2020
"""
2121

2222
[apk]
2323
sh = """
24-
sudo apk add git
24+
@sudo apk add git
2525
"""

0 commit comments

Comments
 (0)