-
Notifications
You must be signed in to change notification settings - Fork 3
/
snap
executable file
·315 lines (290 loc) · 11.9 KB
/
snap
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env bash
# Define some color variables
SILVER='\e[1;38;2;192;192;192m'
PEACH='\e[1;38;2;255;204;153m'
GREEN='\033[1m\033[38;2;0;255;0m'
RED='\033[1m\033[38;5;196m'
AQUA='\e[1;38;2;0;255;255m'
NC='\033[0m' # No Color
# Function to display a menu with options
display_menu() {
# Show heading in middle
clear
echo ""
COLUMNS=$(tput cols)
t1="=================="
t2="Snap App Manager"
printf "${SILVER}%*s\n${NC}" $(((${#t1} + $COLUMNS) / 2)) "$t1"
printf "${SILVER}%*s\n${NC}" $(((${#t2} + $COLUMNS) / 2)) "$t2"
printf "${SILVER}%*s\n${NC}" $(((${#t1} + $COLUMNS) / 2)) "$t1"
echo ""
echo -e "${PEACH}Select Your Choice:${NC}"
echo " 1. Snap Installer/Uninstaller"
echo " 2. List All Apps Including Core Components"
echo " 3. List Installed Apps Excluding Core Components"
echo " 4. Update All Apps"
echo " 5. Search & Install App"
echo " 6. Uninstall App"
echo " 7. Downgrade App"
echo " 8. Manage Permissions"
echo " 9. Go Back To Main Menu"
echo ""
echo -n "Enter your choice: "
}
# Function to install or uninstall snap system in a distro
setup_snap() {
echo ""
# Check if snap is already installed using command -v
if command -v snap >/dev/null; then
echo -e "${AQUA}Snap is already installed.${NC}"
sleep 1
# Ask the user if they want to uninstall snap
read -rp "Do you want to uninstall snap including all snap packages/apps? (Y/n): " choice
case $choice in
[yY]*)
# If yes, then use the appropriate command for the package manager
echo ""
echo -e "${AQUA}NOTE: After completing the uninstallation process, don't forget to restart your machine.${NC}"
echo ""
sleep 3
echo -e "${RED}Uninstalling snap...${NC}"
sleep 1
if command -v apt >/dev/null; then
sudo apt remove --purge snapd -y
elif command -v dnf >/dev/null; then
sudo dnf remove snapd -y
elif command -v yum >/dev/null; then
sudo yum remove snapd -y
elif command -v zypper >/dev/null; then
sudo zypper remove snapd
elif command -v pacman >/dev/null; then
sudo pacman -Rns snapd
else
# For other package managers, show an error message
echo -e "${AQUA}Sorry, I don't know how to uninstall snap on your system.${NC}"
fi
;;
[nN]*)
# If no, then do nothing and exit the function
echo -e "${AQUA}OK, keeping snap.${NC}"
;;
*)
# If invalid input, show an error message and exit the function
echo -e "${RED}Invalid input. Please enter y or n.${NC}"
;;
esac
else
# If snap is not installed
echo -e "${AQUA}Snap is not installed.${NC}"
sleep 1
# Ask the user if they want to install snap
read -rp "Do you want to install snap? (Y/n): " choice
case $choice in
[yY]*)
# If yes, then use the appropriate command to install snap
echo -e "${GREEN}Installing snap...${NC}"
sleep 1
echo -e "${AQUA}NOTE: Installing snap on your system may take some time. We appreciate your patience during this process.${NC}"
sleep 3
if command -v apt >/dev/null; then
sudo apt update && sudo apt install snapd -y
elif command -v dnf >/dev/null; then
sudo dnf install snapd -y
elif command -v yum >/dev/null; then
sudo yum install epel-release -y && sudo yum install snapd -y
elif command -v zypper >/dev/null; then
sudo zypper addrepo --refresh https://download.opensuse.org/repositories/system:/snappy/openSUSE_Leap_15.2 snappy && sudo zypper --gpg-auto-import-keys refresh && sudo zypper dup --from snappy && sudo zypper install snapd
sudo systemctl enable --now snapd
sudo systemctl enable --now snapd.apparmor
elif command -v pacman >/dev/null; then
# Install base-devel and git if they are not already installed
sudo pacman -S --needed base-devel git
# Clone the AUR package for snapd
git clone https://aur.archlinux.org/snapd.git
# Change to the newly created snapd directory
cd snapd
# Build and install the package using makepkg
makepkg -si
# Enable and start the snapd.socket systemd unit
sudo systemctl enable --now snapd.socket
# Create a symbolic link between /var/lib/snapd/snap and /snap
sudo ln -s /var/lib/snapd/snap /snap
else
# For other package managers, show an error message and exit the function
echo -e "${AQUA}Sorry, I don't know how to install snap on your system.${NC}"
fi
echo -e "${GREEN}Activating snap...${NC}"
sleep 1
sudo snap install core
sudo snap install snap-store
sudo systemctl enable --now snapd.socket
sudo ln -s /var/lib/snapd/snap /snap
echo ""
echo -e "${AQUA}NOTE: To complete the installation, restart your machine.${NC}"
echo ""
sleep 3
;;
[nN]*)
# If no, then do nothing and exit the function
echo -e "${AQUA}OK, not installing snap.${NC}"
;;
*)
# If invalid input, show an error message and exit the function
echo -e "${RED}Invalid input. Please enter y or n.${NC}"
;;
esac
fi
sleep 1
read -rp "Press Enter to continue..."
}
# Function to list all installed Snap apps
list_all_apps() {
echo ""
echo -e "${GREEN}Listing All Apps Including Core Components:${NC}"
sleep 1
echo -e "${GREEN}-----------------------${NC}"
snap list # Use snap command to list apps
sleep 1
read -rp "Press Enter to continue..."
}
# Function to list installed apps excluding core components
list_user_apps() {
echo ""
echo -e "${GREEN}Listing Installed Apps Excluding Core Component:${NC}"
sleep 1
echo -e "${GREEN}-----------------------${NC}"
snap list | grep -v "^core" # Use snap command to list apps
sleep 1
read -rp "Press Enter to continue..."
}
# Function to update all installed Snap apps
update_apps() {
echo ""
echo -e "${GREEN}Updating All Apps...${NC}"
sleep 1
echo -e "${GREEN}-----------------------${NC}"
sudo snap refresh # Use snap command to update all apps
sleep 1
read -rp "Press Enter to continue..."
}
# Function to search and install a specific Snap app
install_app() {
echo ""
read -rp "Enter the app name to search: " app_name
echo -e "${GREEN}Searching for $app_name...${NC}"
sleep 1
echo -e "${GREEN}-----------------------${NC}"
snap find "$app_name" # Use snap command to search for app name
sleep 1
echo -e "${GREEN}-----------------------${NC}"
read -rp "Enter the exact app name from above shown list to install: " app_install
sudo snap install "$app_install" # Use snap command to install app
sleep 1
read -rp "Press Enter to continue..."
}
# Function to uninstall a specific Snap app
uninstall_app() {
echo ""
read -rp "Enter the app name to uninstall: " app_name
sudo snap remove --purge "$app_name" # Use snap command to uninstall app
sleep 1
read -rp "Press Enter to continue..."
}
# Function to downgrade a specific Snap app
downgrade_app() {
echo ""
read -rp "Enter the app name to downgrade: " app_name
snap list "$app_name" --all # List the installed versions of the app
read -rp "Enter the revision number to downgrade to: " rev_num
sudo snap revert "$app_name" --revision "$rev_num" # Revert to the specified revision
sleep 1
read -rp "Press Enter to continue..."
}
# Function to manage permissions of Snap apps
manage_permissions() {
echo ""
echo -e "${GREEN}Managing Permissions of Snap Apps...${NC}"
sleep 1
echo -e "${GREEN}-----------------------${NC}"
# List all the installed Snap apps
snap list | grep -v "^core"
# Ask the user to enter the name of the app they want to manage
read -rp "Enter the name of the app you want to manage: " app
echo ""
sleep 1
# Check if the app is a valid Snap app
if snap list | grep -q "^$app"; then
# List all the interfaces used by the app
snap connections $app
# Ask the user to enter the name of the interface they want to manage
read -rp "Enter the name of the interface you want to manage: " interface
echo ""
sleep 1
# Check if the interface is a valid interface for the app
if snap connections $app | grep -q "$interface"; then
# Ask the user to choose an action: connect, disconnect, or cancel
read -rp "Choose an action: connect, disconnect, or cancel: " action
echo ""
sleep 1
# Perform the action according to the user's choice
case $action in
connect)
# Connect the app to the interface
snap connect $app:$interface
echo -e "${AQUA}$app is connected to $interface.${NC}"
;;
disconnect)
# Disconnect the app from the interface
snap disconnect $app:$interface
echo -e "${AQUA}$app is disconnected from $interface.${NC}"
;;
cancel)
# Cancel the operation and exit the function
echo -e "${AQUA}Operation cancelled!${NC}"
sleep 3
return
;;
*)
# Invalid action, display an error message and exit the function
echo -e "${RED}Invalid action, please choose connect, disconnect, or cancel.${NC}"
sleep 3
return
;;
esac
else
# Invalid interface, display an error message and exit the function
echo -e "${RED}$interface is not a valid interface for $app.${NC}"
sleep 3
return
fi
else
# Invalid app, display an error message and exit the function
echo -e "${RED}$app is not a valid Snap app.${NC}"
sleep 3
return
fi
sleep 1
read -rp "Press Enter to continue..."
}
# Go back to main menu
main_menu() {
chmod +x manager
./manager
}
# Main loop
while true; do
display_menu # Display the menu
read -r choice
case $choice in # Handle the choice
1) setup_snap ;; # Install or uninstall snap system in a distro
2) list_all_apps ;; # List apps
3) list_user_apps ;; # List installed apps excluding core apps
4) update_apps ;; # Update all apps
5) install_app ;; # Search and install app
6) uninstall_app ;; # Uninstall app
7) downgrade_app ;; # Downgrade app
8) manage_permissions ;; # Manage permissions of snap apps
9) main_menu ;; # Exit to main menu
*) echo "" && echo -e "${RED}Invalid choice. Please try again.${NC}" && sleep 3 ;; # Invalid choice
esac
done