Skip to content

Commit d1c90ae

Browse files
authored
better error handling, i'm terrible at this
1 parent 21ffc3a commit d1c90ae

1 file changed

Lines changed: 145 additions & 81 deletions

File tree

fpkg

Lines changed: 145 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -6,101 +6,165 @@ error () {
66
exit 1
77
}
88

9-
# hide the keystrokes, save current directory to go to
10-
# the fpkg one
11-
stty -echo
12-
pushd . > /dev/null
13-
cd /usr/local/fpkg/
9+
write_check () {
10+
touch $FPKGDIR > /dev/null 2>&1
1411

15-
# requested an update of all packages?
16-
if [[ $1 = "update" ]]; then
1712
if [[ $? != 0 ]]; then
18-
echo "fpkg directory does not exist!"
13+
echo "ERROR: you cannot write to $FPKGDIR! Please run as root"
1914
error
2015
fi
16+
}
2117

22-
entries=$(cat pkg.list | wc -l)
18+
FPKGDIR="/usr/local/fpkg/" # where all packages are located at
19+
EDIOR="vim" # change this to your favorite editor
2320

24-
for ((i = 1; i <= $entries; i++)); do
25-
pkg_dir=$(head -n $i pkg.list | tail -1)
26-
cd $pkg_dir
27-
echo "$pkg_dir:"
28-
echo $(git pull)
29-
cd - > /dev/null
30-
done
21+
# hide the keystrokes, save current directory to go to
22+
# the fpkg one
23+
stty -echo
24+
pushd . > /dev/null
25+
cd $FPKGDIR
3126

32-
# requested the installation of a package?
33-
elif [[ $1 = "install" ]]; then
34-
cd ii/
27+
if [[ $? != 0 ]]; then
28+
echo "ERROR: $FPKGDIR does not exist! Create it now? [Y/n]"
29+
read choice
3530

36-
if [[ $? != 0 ]]; then
37-
echo "No instruction files..."
38-
error
31+
if [[ choice != "n" ]]; then
32+
mkdir -v $FPKGDIR
3933
fi
4034

41-
./$2.ii
42-
43-
# adding an entry to the list?
44-
elif [[ $1 = "add" ]]; then
45-
if [[ $(whoami) != "root" ]]; then
46-
echo "oop, root privileges required!"
47-
error
48-
fi
35+
error
36+
fi
4937

50-
stty echo
51-
echo "Package name:"
52-
read pkg_name
53-
echo $pkg_name >> pkg.list
54-
echo "# Writing installation instructions for package, you can ignore this line" > ii/$pkg_name.ii
55-
echo "cd ../<package_dir_here>/" >> ii/$pkg_name.ii
56-
$EDITOR ii/$pkg_name.ii
57-
chmod +x ii/$pkg_name.ii
58-
59-
# delete an entry from the list?
60-
elif [[ $1 = "delete" ]]; then
61-
if [[ $(whoami) != "root" ]]; then
62-
echo "oop, root privileges required!"
63-
error
64-
fi
38+
case $1 in
39+
# refresh every package using git pull
40+
"update" | "u")
41+
entries=$(cat pkg.list | wc -l)
42+
43+
for ((i = 1; i <= $entries; i++)); do
44+
pkg_dir=$(head -n $i pkg.list | tail -1)
45+
cd $pkg_dir > /dev/null 2>&1
46+
47+
if [[ $? != 0 ]]; then
48+
echo "ERROR: $pkg_dir does not exist!"
49+
echo "Remove its entry with 'fpkg delete' and try again. Aborting..."
50+
error
51+
fi
52+
53+
echo "$pkg_dir:"
54+
git pull
55+
cd - > /dev/null
56+
done
57+
;;
58+
59+
# requested the installation of a package?
60+
"install" | "i")
61+
if [[ -z $2 ]]; then
62+
echo "ERROR: package name was not provided!"
63+
error
64+
fi
65+
66+
cd ii/
67+
68+
if [[ $? != 0 ]]; then
69+
echo "ERROR: $FPKGDIR/ii/ does not exist! Create it now? [Y/n]"
70+
read choice
71+
72+
if [[ choice != "n" ]]; then
73+
mkdir -v ii/
74+
fi
75+
76+
error
77+
fi
78+
79+
./$2.ii
80+
;;
81+
82+
"list" | "l")
83+
cat pkg.list
84+
echo ""
85+
echo "Total: $(cat pkg.list | wc -l)"
86+
;;
87+
88+
# adding an entry to the list?
89+
"add" | "a")
90+
write_check
6591

66-
stty echo
67-
echo "Removing:"
68-
read pkg_name
69-
entry=$(grep -w "^$pkg_name" pkg.list -n | awk -F: '{print $1}')
70-
sed -i "$entry"d pkg.list
71-
rm ii/$pkg_name.ii
72-
echo "$pkg_name removed, would you like to remove its files as well? [Y/n]"
73-
read file_removal
74-
if [[ $file_removal = 'y' ]]; then
75-
echo "Package directory (relative to /usr/local/fpkg):"
76-
read dir
77-
rm -r $dir
78-
else
79-
popd > /dev/null
8092
stty echo
81-
fi
93+
echo "Package name:"
94+
read pkg_name
95+
echo $pkg_name >> pkg.list
96+
echo "# Writing installation instructions for package, you can ignore this line" > ii/$pkg_name.ii
97+
echo "cd ../<package_dir_here>/" > ii/$pkg_name.ii
98+
$EDITOR ii/$pkg_name.ii
99+
chmod +x ii/$pkg_name.ii
100+
;;
101+
102+
# delete an entry from the list?
103+
"delete" | "d")
104+
write_check
82105

83-
# requested the message of last git commit?
84-
elif [[ $1 = "message" ]]; then
85-
cd $2
86-
87-
if [[ $? != 0 ]]; then
88-
echo "Package does not exist! Was it a typo?"
89-
error
90-
fi
91-
92-
echo $(git log -1 --pretty=%B)
93-
94-
# didn't even input a valid option?
95-
else
96-
echo "Options:"
97-
echo "update - git pull every package on pkg.list"
98-
echo "install <pkg> - install <pkg> with instruction file"
99-
echo "add - add a package to the list"
100-
echo "delete - remove a package from the list"
101-
echo "message <pkg> - get comment from last git commit of <pkg>"
102-
error
103-
fi
106+
stty echo
107+
echo "Removing:"
108+
read pkg_name
109+
entry=$(grep -w "^$pkg_name" pkg.list -n | awk -F: '{ print $1 }')
110+
sed -i "$entry"d pkg.list
111+
rm ii/$pkg_name.ii
112+
echo "$pkg_name removed, would you like to remove its files as well? [y/N]"
113+
read file_removal
114+
115+
if [[ $file_removal = 'y' ]]; then
116+
echo "Package directory (relative to /usr/local/fpkg/):"
117+
read dir
118+
rm -rf $dir
119+
else
120+
popd > /dev/null
121+
stty echo
122+
fi
123+
;;
124+
125+
# requested the message of last git commit?
126+
"message" | "m")
127+
if [[ -z $2 ]]; then
128+
echo "ERROR: package name was not provided!"
129+
error
130+
fi
131+
132+
if [[ $2 = "-d" ]]; then
133+
cd $3
134+
else
135+
cd $2
136+
fi
137+
138+
if [[ $? != 0 ]]; then
139+
echo "Package does not exist! Was it a typo?"
140+
error
141+
fi
142+
143+
if [[ $2 = "-d" ]]; then
144+
git show
145+
else
146+
echo $(git log -1 --pretty=%B)
147+
fi
148+
;;
149+
150+
# didn't even input a valid option?
151+
"help" | *)
152+
echo "Usage: fpkg [action] [package]"
153+
echo "Actions:"
154+
echo "help | h - shows this help message"
155+
echo "update | u - git pull every package listed in pkg.list"
156+
echo "install | i <pkg> - install <pkg> using instruction file"
157+
echo "list | l - list all registered packages"
158+
echo "add | a - add a package to the list"
159+
echo "delete | d - remove a package from the list"
160+
echo "message [-d] <pkg> - get comment from last git commit of <pkg>. Takes an"
161+
echo " optional -d for getting the diff"
162+
163+
if [[ $1 != "help" ]]; then
164+
error
165+
fi
166+
;;
167+
esac
104168

105169
# restore the directory, and unhide
106170
# keystrokes

0 commit comments

Comments
 (0)