-
Notifications
You must be signed in to change notification settings - Fork 0
/
aptup
executable file
·102 lines (87 loc) · 3.19 KB
/
aptup
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
#!/bin/sh
#shellcheck shell=dash
set -ue
exec 5<&0 6>&1 7>&2 # original std{in,out,err}
# License = MIT
# Author = https://gitlab.com/almr/
# Homepage = https://gitlab.com/almr/apt-upgrade-cli/
# https://unix.stackexchange.com/questions/603514/apt-upgrade-package-without-marking-as-manually-installed
# TODO: apt options
_G_apt_conf=
usage () {
cat<<'EOMSG'
Usage: aptup [OPTION]... [PKG]...
Options:
-u, --update run apt update first
--all pre-select all packages for update
--dialog use "dialog" as front-end
--editor, -e use editor as front-end (default)
-o APT_KEY=VAL .. configure apt
*Editor selection*
aptup tries $APTUP_EDITOR, $EDITOR, Debian sensible-editor, nano, vim.
EOMSG
exit 0
}
parse_args () {
DEFSTATUS=off
BACKEND=edit
OREC='-o APT::Install-Recommends=0'
UPGRE='^[[:space:]]*\([^#/][^/]*\)/.*\[upgradable ' # apt isn't scriptable by its own confession; format: linux-doc/focal-updates 5.4.0-59.65 all [upgradable from: 5.4.0-58.64]
while test "$#" != 0; do
local arg; arg=$1; shift
case "$arg" in
(--) break ;;
(-h|--help) usage; exit 0 ;;
(-v|--verbose) set -x ;;
(--all|-a) DEFSTATUS=on ;;
(--update|-u) apt update ;;
(--editor|-e) BACKEND=edit ;;
(--dialog) ! command -v dialog >/dev/null || BACKEND=dialog ;;
(--rec|--with-install-recommends) OREC='-o APT::Install-Recommends=1' ;;
(-o) _G_apt_conf="$_G_apt_conf -o $1"; shift ;;
(-t) arg=$1; shift; set -- -o "APT::Default-Release=$arg" "$@" ;;
(-*) echo "Unknown: $arg"; exit 1 ;;
(*) set -- "$arg" "$@"; break ;;
esac
done
main "$@"
}
upgable2pkgs () {
sed -ne 's@'"$UPGRE"'.*@\1@p'
}
dialog_sel_pkgs () (
US=$(upgable2pkgs)
test -z "$US" && exit 0
for u in $US; do set -- "$@" "$u" '' "$DEFSTATUS"; done
# 2>&1 to captured stdout (dialog defaults to --stderr); then restore tty stdin/out
# simpler to use --stdout so dialog reopens the tty (but docs warn against)
dialog --separate-output --checklist 'Upgrade' 0 0 0 "$@" 2>&1 0<&5 1>&6
)
__set_editor_env () { # set $EDITOR to app-preferred, or existing $EDITOR, or detected
EDITOR=${APTUP_EDITOR:-${EDITOR:-$(command -v sensible-editor || command -v nano || command -v editor || command -v vim || command -v vi)}}
}
edit_sel_pkgs () (
USF=$(mktemp ${TMPDIR:-/tmp}/aptup_edit_pkgs.XXXXXX)
trap 'exit 1' INT QUIT TERM HUP
trap 'rm -f "$USF"' EXIT
if test 'off' = "$DEFSTATUS"; then pfx='#'; else pfx=' '; fi
sed -e "s/^/$pfx/" >"$USF"
test -s "$USF" || exit 0
__set_editor_env; $EDITOR "$USF" 0<&5 1>&6 # though we're on Debian for sure
grep -v '^#' "$USF" | upgable2pkgs
)
sel_pkgs () {
case "$-" in (*x*) ;; (*) echo 1>&2 'Running apt list --upgradeable ...' ;; esac
LC_ALL=C.UTF-8 LANG= LANGUAGE= apt list --upgradeable $OREC $_G_apt_conf "$@" 2>/dev/null | grep "$UPGRE" |
"${BACKEND}_sel_pkgs"
}
main() {
set -- $(sel_pkgs "$@")
test $# -eq 0 && { echo 'No upgrades available' 1>&2; exit; }
case "$-" in (*x*) ;; (*) echo 1>&2 'Getting `apt-mark showauto` packages ...' ;; esac
AUTOS=$(apt-mark showauto "$@")
test "$BACKEND" != 'dialog' || clear
(set -x; apt install $OREC $_G_apt_conf "$@")
test -n "$AUTOS" && apt-mark auto $AUTOS >/dev/null
}
parse_args "$@"