-
Notifications
You must be signed in to change notification settings - Fork 2
/
apt-wrapper.sh
149 lines (138 loc) · 2.79 KB
/
apt-wrapper.sh
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
#!/bin/bash
# Copyright (c) 2017- Josh Glendenning(https://github.com/isobit/pac), SARDONYX
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# echo "apt-wrapper read" # debug
# A simple bash wrapper for apt.
# Author: Josh Glendenning
# Usage: ap [options] <command> <args>...
function ap() {
function log_info {
echo "$(tput setaf 4)$1$(tput sgr0)" >&2
}
function log_warn {
echo "$(tput setaf 3)$1$(tput sgr0)" >&2
}
function log_error {
echo "$(tput setaf 1)ERROR:$(tput sgr0) $1" >&2
}
function display_help {
cat <<EOS
Usage: ap [options] <command> <args>...
<command>:
Install
i <package>
install <package>
Reinstall
ri <package>
reinstall <package>
Search
s <package>
search <package>
Uninstall
uni <package>
rm <package>
remove <package>
Update
update [args]...
up [args]...
upgrade [args]...
ua :Update & Upgrade packages all
Show list
ls [args]...
list [args]...
[options]:
-h | --help :Show this screen.
-v | --verbose :Display the command to be passed through.
--dry_run :Check the generated command without executing the command.
EOS
}
CMD='apt'
# Match any [options]
while :; do
case "$1" in
-h | --help)
display_help # Call your function
# no shifting needed here, we're done.
return
;;
-v | --verbose)
verbose=true
shift
;;
--dry-run)
dry_run=true
shift
;;
--) # End of all options
shift
break
;;
-*)
log_error "Unknown option: $1"
break
;;
*) # No more options
break
;;
esac
done
# Match <command> and pass though to apt
case "$1" in
'i' | 'install' | 'add')
shift
CMD_ARGS="install $* --no-install-recommends"
;;
'ri' | 'reinstall')
shift
CMD_ARGS="reinstall $* --no-install-recommends"
;;
's' | 'search')
shift
CMD_ARGS="search $*"
;;
'update')
shift
CMD_ARGS="update $*"
;;
'up' | 'upgrade')
shift
CMD_ARGS="upgrade $*"
;;
'ua')
shift
CMD_ARGS="update
sudo apt list --upgradable
sudo apt upgrade"
;;
'uni' | 'rm' | 'remove')
shift
CMD_ARGS="remove $*"
;;
'ls' | 'list')
shift
CMD_ARGS="list $*"
;;
*)
log_error "Unknown command: $1"
return 1
;;
esac
# echo the whole command if verbose is enabled
if [ "$verbose" = true ]; then
log_info "=> $(tput sgr0)$CMD $CMD_ARGS"
fi
# Call the command
if [ "$dry_run" = true ]; then
echo "$CMD $CMD_ARGS"
else
if [[ ${CMD_ARGS} =~ ^list.*$ ]] || [[ ${CMD_ARGS} =~ ^search.*$ ]]; then
eval "$CMD $CMD_ARGS"
return 0
fi
CMD="sudo $CMD"
eval "$CMD $CMD_ARGS"
return 0
fi
}