aboutsummaryrefslogtreecommitdiff
path: root/bin/zypper-purgerepos
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <msabate@suse.com>2018-03-06 10:30:44 +0100
committerMiquel Sabaté Solà <msabate@suse.com>2018-03-06 10:30:44 +0100
commit89ee6b66b5a5c277f61198896bdd611f349fced2 (patch)
tree31bf3dad908a63677cc3d2504429721eb5903da4 /bin/zypper-purgerepos
parent83a5a8a9ebb33b857aae9b036174063712a2a86c (diff)
downloaddotfiles-89ee6b66b5a5c277f61198896bdd611f349fced2.tar.gz
dotfiles-89ee6b66b5a5c277f61198896bdd611f349fced2.zip
bin: added zypper-purgerepos
This zypper subcommand will check which repositories are either disabled or have no packages installed. After that, it will then ask the user whether to purge them or not (default to no). Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
Diffstat (limited to 'bin/zypper-purgerepos')
-rwxr-xr-xbin/zypper-purgerepos66
1 files changed, 66 insertions, 0 deletions
diff --git a/bin/zypper-purgerepos b/bin/zypper-purgerepos
new file mode 100755
index 0000000..a4a84f9
--- /dev/null
+++ b/bin/zypper-purgerepos
@@ -0,0 +1,66 @@
+#!/bin/bash
+# Copyright (C) 2018 Miquel Sabaté Solà <mikisabate@gmail.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+set -e
+
+if [ "$EUID" -ne 0 ]; then
+ echo "You must be root in order to run this command."
+ exit 1
+fi
+
+function uri_for() {
+ uri=$(zypper lr $1 | grep -E "(http://|https://)" | awk '{ print $3; }')
+ echo $uri
+}
+
+declare -a ary
+
+repositories=$(zypper lr -d | grep http | awk '{ print $3; }')
+for repo in $repositories; do
+ # Check if it's disabled.
+ if [[ $(zypper lr -E $repo | grep Enabled | grep No) ]]; then
+ uri=$(uri_for "$repo")
+ echo "You should remove repo $repo (disabled) with URI: $uri"
+ ary=("${ary[@]}" "$repo")
+ continue
+ fi
+
+ # Check if there are no packages installed from this repo.
+ num=$(zypper pa -ir $repo | grep $repo | wc -l)
+ if [[ $num -eq 0 ]]; then
+ uri=$(uri_for "$repo")
+ ary=("${ary[@]}" "$repo")
+ echo "You should remove repo $repo with URI: $uri"
+ fi
+done
+
+len=${#ary[@]}
+if [ $len -eq 0 ]; then
+ echo "No repositories to be purged!"
+ exit 0
+fi
+
+echo ""
+read -p "Do you want to purge these repositories? [y/N] " choice
+case "$choice" in
+ y|Y)
+ for repo in ${ary[@]}; do
+ zypper -n rr "$repo"
+ done
+ ;;
+ * )
+ ;;
+esac