Skip to content

Commit af17cb0

Browse files
authored
Merge pull request #58 from Heshdude/creating-installer-generator
Creating installer generator
2 parents 3597aa5 + f1835e1 commit af17cb0

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

.github/workflows/dockerimage.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
name: Run tests
23

34
on:
@@ -18,4 +19,4 @@ jobs:
1819
- uses: actions/checkout@v2
1920
- uses: technote-space/get-diff-action@v1
2021
- name: Get the list of modified files only
21-
run: docker build -t shellspec . && docker run -v $PWD:/app shellspec bash -c "cd /app && ./test.sh ${{ env.GIT_DIFF }}"
22+
run: pip install toml && python generate.py ${{ env.GIT_DIFF }} && docker build -t shellspec . && docker run -v $PWD:/app shellspec bash -c "cd /app && ./test.sh ${{ env.GIT_DIFF }}"

generate.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import toml
2+
import sys
3+
import os
4+
import logging
5+
import errno
6+
7+
methods = {
8+
"apt": "$APT_GET",
9+
"yum": "$YUM",
10+
"dnf": "$DNF",
11+
"apk": "$APK",
12+
"pacman": "$PACMAN",
13+
"git": "$GIT"
14+
}
15+
16+
def get_method_case(method):
17+
if method in methods:
18+
return "[ ! -z "+methods[method]+"_CMD ]; then\n"
19+
else:
20+
logging.error('Unpupported method in the TOML file, method: '+method)
21+
exit(1)
22+
23+
def generate(path):
24+
25+
installer_toml_path = path+"/installer.toml"
26+
installer_sh_path = path+"/installer.sh"
27+
28+
installer_toml = open(installer_toml_path, "r")
29+
parsed_toml = toml.loads(installer_toml.read())
30+
try:
31+
with open(installer_sh_path, "w") as installer_sh:
32+
33+
installer_sh.write("""#!/bin/sh
34+
35+
YUM_CMD=$(which yum) # yum package manager for RHEL & CentOS
36+
DNF_CMD=$(which dnf) # dnf package manager for new RHEL & CentOS
37+
APT_GET_CMD=$(which apt-get) # apt package manager for Ubuntu & other Debian based distributions
38+
PACMAN_CMD=$(which pacman) # pacman package manager for ArchLinux
39+
APK_CMD=$(which apk) # apk package manager for Alpine
40+
GIT_CMD=$(which git) # to build from source pulling from git
41+
42+
""")
43+
44+
seperator = "if"
45+
46+
for section in parsed_toml:
47+
lines = parsed_toml[section]['sh']
48+
installer_sh.write(seperator+" "+get_method_case(section))
49+
for line in lines.split("\n"):
50+
installer_sh.write(" "+line+"\n")
51+
seperator = "elif"
52+
53+
installer_sh.write("""
54+
else
55+
echo "Couldn't install package"
56+
exit 1;
57+
fi
58+
""".strip())
59+
60+
installer_sh.close()
61+
62+
except IOError as x:
63+
if x.errno == errno.EACCES:
64+
logging.error('No enough permissions to write to '+installer_sh_path)
65+
exit(1)
66+
else:
67+
logging.error('Something went wrong when trying to write to '+installer_sh_path)
68+
exit(1)
69+
70+
for path in sys.argv[1:]:
71+
if os.path.exists(path+'/installer.toml'):
72+
logging.info('Generating installer.sh for '+path)
73+
generate(path)
74+
else:
75+
logging.warn('Could not find an installer.toml in '+path)

installers/hello/installer.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[apt]
2+
sh = """
3+
echo "Installing hello"
4+
echo "Installed hello"
5+
"""
6+
7+
[yum]
8+
sh = """
9+
echo "Installing hello"
10+
echo "Installed hello"
11+
"""
12+
13+
14+
[apk]
15+
sh = """
16+
echo "Installing hello"
17+
echo "Installed hello"
18+
"""

0 commit comments

Comments
 (0)