-
Notifications
You must be signed in to change notification settings - Fork 0
/
my-scripts
executable file
·205 lines (169 loc) · 5.44 KB
/
my-scripts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/bash
#$$This is a manager tool for my scripts
scriptPath="$HOME/bin"
#function banner() {
# echo " _ _ "
# echo " (_) | | "
# echo " _ __ ___ _ _ ______ ___ ___ _ __ _ _ __ | |_ ___ "
# echo "| '_ ` _ \\| | | |______\/ __|/ __| '__| | '_ \\| __/ __|"
# echo "| | | | | | |_| | \\__ \\ (__| | | | |_) | |_\\__ \\"
# echo "|_| |_| |_|\\__, | |___/\\___|_| |_| .__/ \\__|___/"
# echo " __/ | | | "
# echo " |___/ |_| "
#}
if [ "$1" == "new" ] || [ "$1" == "n" ]; then
if [ -z $2 ]; then
echo "Please enter a name for the new script"
echo "Run 'my-scripts -h' for help"
exit 1
fi
# check if script already exists
if [ -f "$scriptPath/$2" ]; then
echo "Script already exists!"
exit 1
fi
# check if command already exists
if command -v "$2" &> /dev/null; then
echo "Command '$2' does already exist!"
exit 1
fi
# create new script
echo "Creating new script..."
touch "$scriptPath/$2"
echo "#!/bin/bash" >> "$scriptPath/$2"
echo "" >> "$scriptPath/$2"
chmod +x "$scriptPath/$2"
echo "Created new script: $2"
vi "$scriptPath/$2"
# Add to git
current_path="$PWD"
cd "$scriptPath"
git add "$2"
git commit -m "Created new script: $2"
git push
cd "$current_path"
echo "New Script added ✅"
elif [ "$1" == "alias" ] || [ "$1" == "a" ]; then
if [ -z $2 ]; then
echo "Missing arguments"
echo "Run 'my-scripts -h' for help"
exit 1
fi
if [ -z $3 ]; then
echo "Missing arguments"
echo "Run 'my-scripts -h' for help"
exit 1
fi
# check if script already exists
if [ -f "$scriptPath/$2" ]; then
echo "Script already exists!"
exit 1
fi
# check if command already exists
if command -v "$2" &> /dev/null; then
echo "Command '$2' does already exist!"
exit 1
fi
echo "Creating new alias..."
touch "$scriptPath/$2"
aliasCommand="$3"
for arg in "${@:4}"; do
aliasCommand="$aliasCommand $arg"
done
echo "#!/bin/bash" >> "$scriptPath/$2"
echo "" >> "$scriptPath/$2"
echo "#\$\$This is a alias for the '$aliasCommand' command" >> "$scriptPath/$2"
echo "" >> "$scriptPath/$2"
echo "echo \"$aliasCommand \$*\"" >> "$scriptPath/$2"
echo "" >> "$scriptPath/$2"
echo "$aliasCommand \$*" >> "$scriptPath/$2"
echo "" >> "$scriptPath/$2"
chmod +x "$scriptPath/$2"
echo "Created new alias: $2"
# Add to git
current_path="$PWD"
cd "$scriptPath"
git add "$2"
git commit -m "Created new script: $2"
git push
cd "$current_path"
echo "New Alias added ✅"
elif [ "$1" == "delete" ]|| [ "$1" == "d" ]; then
if [ -z $2 ]; then
echo "Please enter a name for the script to delete"
echo "Run 'my-scripts -h' for help"
exit 1
fi
# check if script already exists
if [ ! -f "$scriptPath/$2" ]; then
echo "Script does not exist!"
exit 1
fi
# delete script
echo "Deleting script..."
rm "$scriptPath/$2"
# Add to git
current_path="$PWD"
cd "$scriptPath"
git add "$2"
git commit -m "Deleted script: $2"
git push
cd "$current_path"
echo "Script deleted ✅"
elif [ "$1" == "list" ] || [ "$1" == "ls" ]; then
cd $scriptPath
echo "My Scripts:"
# read file .myscriptsignore
ignoreContent=""
if [ -f ".myscriptsignore" ]; then
ignoreContent=$(cat ".myscriptsignore")
fi
# split string into lines
IFS=$'\n' read -rd '' -a ignoreArray <<<"$ignoreContent"
echo ""
files=(./*)
for file in "${files[@]}"; do
fileName=$(basename "$file")
# continue if file is in ignore list
if [[ " ${ignoreArray[@]} " =~ " ${fileName} " ]]; then
continue
fi
# get content of file
content=$(cat "$file")
# find a line starting with #$$
description=$(echo "$content" | grep -m 1 -o "#\$\$.*")
# remove #$$ from description
description=${description:3}
if [ ! -z "$description" ]; then
# print the name in the console and the description in the same line but with a tab and align the description
printf "%-20s %s\n" "$fileName" "$description"
else
echo "$fileName"
fi
done
elif [ "$1" == "code" ] || [ "$1" == "c" ]; then
code "$scriptPath"
elif [ "$1" == "update" ]; then
# check internet connection
wget -q --spider https://github.com
if [ ! $? -eq 0 ]; then
exit 0
fi
cd "$scriptPath"
if [ "$2" == "q" ]; then
git pull -q &
else
echo "Updating my-scripts..."
git pull
echo "Updated ✅"
fi
else
echo "my-scripts help"
echo ""
echo "my-scripts new <script-name> : Create new script"
echo "my-scripts alias <new-alias-name> <command> : Create new alias for the command"
echo "my-scripts delete <script-name> : Delete script"
echo "my-scripts list : List all scripts"
echo "my-scripts update : Update my-scripts"
echo "my-scripts code : Open scripts folder in VS Code"
fi