From 933af76daf3363e82b3500abab985768b463b285 Mon Sep 17 00:00:00 2001 From: David Runge Date: Thu, 11 Apr 2019 11:48:52 +0200 Subject: bin/pkgs: Major rewrite to get rid of redundant code. Makes use of named arrays for directores, and upstream urls. Moves package list locations to ~/.config/packages/*. --- bin/pkgs | 411 +++++++++++++++++++++++++++------------------------------------ 1 file changed, 178 insertions(+), 233 deletions(-) (limited to 'bin') diff --git a/bin/pkgs b/bin/pkgs index 59f2d9c..a64f969 100755 --- a/bin/pkgs +++ b/bin/pkgs @@ -2,141 +2,138 @@ set -euo pipefail -aur_packagedir="aur-maintain" -aur_packagelist="$HOME/.config/packages-aur.txt" -community_packagedir="svn-community" -community_upstream="svn+ssh://svn-community@repos.archlinux.org/srv/repos/svn-community/svn" -community_packagelist="$HOME/.config/packages-community.txt" -packagedir="$HOME/packages/" +package_dir_base="$HOME/packages/" +repo="" +package="" +mode="" + +declare -A upstreams=( + ["aur"]="aur@aur.archlinux.org" + ["community"]="svn+ssh://svn-community@repos.archlinux.org/srv/repos/svn-community/svn" + ["core"]="svn+ssh://svn-packages@repos.archlinux.org/srv/repos/svn-packages/svn" + ["extra"]="svn+ssh://svn-packages@repos.archlinux.org/srv/repos/svn-packages/svn" +) +declare -A package_lists=( + ["aur"]="$HOME/.config/packages/aur.txt" + ["community"]="$HOME/.config/packages/community.txt" + ["core"]="$HOME/.config/packages/core.txt" + ["extra"]="$HOME/.config/packages/extra.txt" +) +declare -A package_dirs=( + ["aur"]="${package_dir_base}aur" + ["community"]="${package_dir_base}community" + ["core"]="${package_dir_base}core" + ["extra"]="${package_dir_base}extra" +) create_tmp_packagelist() { - local list_file=$(basename "$1") + local list_file="" + list_file=$(basename "$1") mktemp -p "$XDG_RUNTIME_DIR/" "$list_file.XXXX" } -add_aur_package() { - local tmp_file=$(create_tmp_packagelist "$aur_packagelist") - echo $1 | cat "$aur_packagelist" - |uniq |sort > "$tmp_file" - cat "$tmp_file" > "$aur_packagelist" - rm "$tmp_file" -} - -add_community_package() { - local tmp_file=$(create_tmp_packagelist "$community_packagelist") - echo $1 | cat "$community_packagelist" - |uniq |sort > "$tmp_file" - cat "$tmp_file" > "$community_packagelist" - rm "$tmp_file" -} - -remove_aur_package() { - local tmp_file=$(create_tmp_packagelist "$aur_packagelist") - cat "$aur_packagelist" |grep -vE "^$1$" |uniq |sort > "$tmp_file" - cat "$tmp_file" > "$aur_packagelist" +add_package() { + local name="$1" + local tmp_file="" + tmp_file=$(create_tmp_packagelist "${package_lists[$repo]}") + echo "Adding $package to list of $repo packages." + echo "$name" | cat "${package_lists[$repo]}" - |uniq |sort > "$tmp_file" + cat "$tmp_file" > "${package_lists[$repo]}" rm "$tmp_file" } -remove_community_package() { - local tmp_file=$(create_tmp_packagelist "$community_packagelist") - cat "$community_packagelist" |grep -vE "^$1$" |uniq |sort > "$tmp_file" - cat "$tmp_file" > "$community_packagelist" +remove_package() { + local name="$1" + local tmp_file="" + tmp_file=$(create_tmp_packagelist "${package_lists[$repo]}") + echo "Removing $package from list of $repo packages." + cat "${package_lists[$repo]}" |grep -vE "^$name$" |uniq |sort > "$tmp_file" + cat "$tmp_file" > "${package_lists[$repo]}" rm "$tmp_file" } -create_aur_dir() { - if [ ! -d "$packagedir$aur_packagedir" ];then - echo "Creating AUR dir." - mkdir -pv "$packagedir$aur_packagedir" +check_mode_set(){ + if [ -n "$mode" ]; then + echo "Error: A mode can only be set once." + exit 1 fi } - -create_aur_dir() { - if [ ! -d "$packagedir$aur_packagedir" ];then - echo "Creating AUR dir." - mkdir -pv "$packagedir$aur_packagedir" +check_repo_set(){ + if [ -n "$repo" ]; then + echo "Error: A repo can only be set once." + exit 1 fi } -create_community_dir() { - if [ ! -d "$packagedir$community_packagedir" ];then - echo "Checking out [community] folder the first time." - cd $packagedir - svn checkout -N $community_upstream svn-community +create_repo_dir() { + if [ ! -d "${package_dirs[$repo]}" ];then + if [ "$repo" == "aur" ]; then + echo "Creating directory for $repo." + mkdir -pv "${package_dirs[$repo]}" + else + ( + echo "Checking out $repo directory." + cd "$(dirname "${package_dirs[$repo]}")" + svn checkout -N "${upstreams[$repo]}" \ + "$(basename "${package_dirs[$repo]}")" + ) + fi fi } print_help() { - echo "Usage: pkgs -" + echo "Usage: pkgs -" } -clean_aur_packages() { - if [ -d "$packagedir$aur_packagedir" ];then - cd "$packagedir$aur_packagedir" - for package in $(cat "$aur_packagelist"); do - if [ -d $package ]; then - echo "Cleaning package '$package'." - cd $package - git clean -f - cd .. +clean_packages() { + if [ -d "${package_dirs[$repo]}" ]; then + cd "${package_dirs[$repo]}" + for package in $(cat "${package_lists[$repo]}"); do + if [ -d "$package" ]; then + ( + cd "$package" + echo "Cleaning package $repo/$package." + if [ "$repo" == "aur" ]; then + git clean -f + else + svn cleanup --remove-unversioned . + fi + ) fi done else - echo "There are no AUR packages." + echo "Package dir for $repo is empty." + exit 1 fi } -clean_community_packages() { - if [ -d "$packagedir$community_packagedir" ]; then - cd "$packagedir$community_packagedir" - for package in $(cat "$community_packagelist"); do - if [ -d $package ]; then - cd $package - svn cleanup --remove-unversioned . - cd .. +update_packages() { + create_repo_dir + cd "${package_dirs[$repo]}" + for package in $(cat "${package_lists[$repo]}"); do + if [ "$repo" == "aur" ]; then + if [ -d "$package" ]; then + ( + echo "Pulling package: $package." + cd "$package" + git pull + ) + else + echo "Cloning package '$package'." + git clone "${upstreams[$repo]}:$package" fi - done - else - echo "There are no community packages." - fi -} - -update_aur_packages() { - create_aur_dir - cd "$packagedir$aur_packagedir" - for package in $(cat "$aur_packagelist"); do - if [ -d $package ]; then - echo "Pulling package '$package'." - cd $package - git pull - cd .. else - echo "Cloning package '$package' from the AUR for the first time." - git clone aur@aur.archlinux.org:$package + svn update "$package" fi done } -update_community_packages() { - create_community_dir - cd "$packagedir$community_packagedir" - for package in $(cat "$community_packagelist"); do - svn update $package - done -} - -add_mode=0 -aur_mode=0 -clean_mode=0 -community_mode=0 -list_mode=0 -remove_mode=0 -update_mode=0 -package="" - if [ ${#@} -gt 0 ]; then - while getopts ':a:chlur:AC' flag; do + while getopts ':a:chlur:ACEP' flag; do case "${flag}" in a) - add_mode=1 + check_mode_set + mode="add" if [ -n "${OPTARG}" ]; then package="${OPTARG}" else @@ -145,16 +142,22 @@ if [ ${#@} -gt 0 ]; then fi ;; c) - clean_mode=1 + check_mode_set + mode="clean" ;; h) print_help ;; l) - list_mode=1 + check_mode_set + mode="list" ;; + # TODO: introduce -m flag for moving package (e.g. community2extra, extra2community) + # TODO: introduce -n flag for creating new package (with PKGBUILD copied + # from default) r) - remove_mode=1 + check_mode_set + mode="remove" if [ -n "${OPTARG}" ]; then package="${OPTARG}" else @@ -163,13 +166,24 @@ if [ ${#@} -gt 0 ]; then fi ;; u) - update_mode=1 + check_mode_set + mode="update" ;; A) - aur_mode=1 + check_repo_set + repo="aur" ;; C) - community_mode=1 + check_repo_set + repo="community" + ;; + E) + check_repo_set + repo="extra" + ;; + P) + check_repo_set + repo="core" ;; *) echo "Error. Unrecognized option: ${flag}." @@ -181,134 +195,65 @@ else print_help fi -# incompatible modes -if [ $add_mode -eq 1 ] && \ - [ $remove_mode -eq 1 ]; then - echo "Add and remove mode are not complementary." - print_help - exit 1 -fi - -# add_mode (works with aur_mode and community_mode) -if [ $add_mode -eq 1 ] && \ - [ $aur_mode -eq 0 ] && \ - [ $community_mode -eq 0 ];then - echo "A repository must be defined for adding a package." - print_help - exit 1 -elif [ $add_mode -eq 1 ] && \ - [ $aur_mode -eq 1 ] && \ - [ $community_mode -eq 1 ];then - echo "Only one repository can be defined for adding a package." - print_help - exit 1 -elif [ $add_mode -eq 1 ] && \ - [ $aur_mode -eq 0 ] && \ - [ $community_mode -eq 1 ];then - echo "Adding $package to list of community packages." - add_community_package $package -elif [ $add_mode -eq 1 ] && \ - [ $aur_mode -eq 1 ] && \ - [ $community_mode -eq 0 ];then - echo "Adding $package to list of AUR packages." - add_aur_package $package -fi - -# remove_mode (works with aur_mode and community_mode) -if [ $remove_mode -eq 1 ] && \ - [ $aur_mode -eq 0 ] && \ - [ $community_mode -eq 0 ];then - echo "A repository must be defined for removing a package." - print_help - exit 1 -elif [ $remove_mode -eq 1 ] && \ - [ $aur_mode -eq 1 ] && \ - [ $community_mode -eq 1 ];then - echo "Only one repository can be defined for removing a package." - print_help - exit 1 -elif [ $remove_mode -eq 1 ] && \ - [ $aur_mode -eq 0 ] && \ - [ $community_mode -eq 1 ];then - echo "Removing $package from list of community packages." - remove_community_package $package -elif [ $remove_mode -eq 1 ] && \ - [ $aur_mode -eq 1 ] && \ - [ $community_mode -eq 0 ];then - echo "Removing $package from list of AUR packages." - remove_aur_package $package -fi - -# list_mode (works with aur_mode and community_mode) -if [ $list_mode -eq 1 ] && \ - [ $aur_mode -eq 0 ] && \ - [ $community_mode -eq 0 ];then - echo "Define which repository to list." - print_help - exit 1 -elif [ $list_mode -eq 1 ] && \ - [ $aur_mode -eq 1 ] && \ - [ $community_mode -eq 0 ];then - cat "$aur_packagelist" -elif [ $list_mode -eq 1 ] && \ - [ $aur_mode -eq 0 ] && \ - [ $community_mode -eq 1 ];then - cat "$community_packagelist" -elif [ $list_mode -eq 1 ] && \ - [ $aur_mode -eq 1 ] && \ - [ $community_mode -eq 1 ];then - echo "All packages:" - cat "$aur_packagelist" "$community_packagelist" -fi - -# update_mode (works with aur_mode and community_mode) -if [ $update_mode -eq 1 ] && \ - [ $aur_mode -eq 0 ] && \ - [ $community_mode -eq 0 ];then - echo "Define which repository to update." - print_help - exit 1 -elif [ $update_mode -eq 1 ] && \ - [ $aur_mode -eq 1 ] && \ - [ $community_mode -eq 0 ];then - echo "Updating AUR packages." - update_aur_packages -elif [ $update_mode -eq 1 ] && \ - [ $aur_mode -eq 0 ] && \ - [ $community_mode -eq 1 ];then - echo "Updating community packages." - update_community_packages -elif [ $update_mode -eq 1 ] && \ - [ $aur_mode -eq 1 ] && \ - [ $community_mode -eq 1 ];then - echo "Updating all packages:" - update_aur_packages - update_community_packages -fi - -# clean_mode (works with aur_mode and community_mode) -if [ $clean_mode -eq 1 ] && \ - [ $aur_mode -eq 0 ] && \ - [ $community_mode -eq 0 ];then - echo "Define which repository to clean." - print_help - exit 1 -elif [ $clean_mode -eq 1 ] && \ - [ $aur_mode -eq 1 ] && \ - [ $community_mode -eq 0 ];then - echo "Cleaning AUR packages." - clean_aur_packages -elif [ $clean_mode -eq 1 ] && \ - [ $aur_mode -eq 0 ] && \ - [ $community_mode -eq 1 ];then - echo "Cleaning community packages." - clean_community_packages -elif [ $clean_mode -eq 1 ] && \ - [ $aur_mode -eq 1 ] && \ - [ $community_mode -eq 1 ];then - echo "Cleaning all packages:" - clean_aur_packages - clean_community_packages -fi +case "${mode}" in + add) + if [ -z "$repo" ]; then + echo "A repository must be defined for adding a package." + exit 1 + elif [ -z "$package" ]; then + echo "A package must be defined for adding a package." + exit 1 + else + add_package "$package" + fi + ;; + remove) + if [ -z "$repo" ]; then + echo "A repository must be defined for removing a package." + print_help + exit 1 + elif [ -z "$package" ]; then + echo "A package must be defined for removing a package." + exit 1 + else + remove_package "$package" + fi + ;; + update) + if [ -z "$repo" ];then + for list in aur community core extra; do + if [ -f "${package_lists[$list]}" ]; then + repo="$list" + update_packages + fi + done + else + update_packages + fi + ;; + list) + if [ -z "$repo" ]; then + for list in aur community core extra; do + if [ -f "${package_lists[$list]}" ]; then + cat "${package_lists[$list]}" + fi + done + else + if [ -f "${package_lists[$repo]}" ]; then + cat "${package_lists[$repo]}" + fi + fi + ;; + clean) + if [ -z "$repo" ]; then + for list in aur community core extra; do + repo="$list" + clean_packages + done + else + clean_packages + fi + ;; +esac exit 0 -- cgit v1.2.3-54-g00ecf