Skip to content

Commit

Permalink
lib/dpkg: now POSIX
Browse files Browse the repository at this point in the history
  • Loading branch information
icymatter committed Jul 4, 2021
1 parent 3c76450 commit b00aadb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 37 deletions.
4 changes: 4 additions & 0 deletions bin/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ _check_POSIX_files() {
| grep -Eiqe '^#!/usr/bin/env sh' ;
then
_check_file "$1" || return 1
# We only care POSIX issue for files under `lib/`
elif echo "$1" | grep -qs "lib/"; then
>&2 echo ":: WARNING: POSIX help-wanted '$1'"
fi

shift
done
}
Expand Down
36 changes: 18 additions & 18 deletions lib/dpkg.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env sh

# Purpose: Debian / Ubuntu support
# Author : Anh K. Huynh
Expand All @@ -20,11 +20,11 @@ _dpkg_init() {
# dpkg_Q may _not_implemented
# FIXME: Need to support a small list of packages
dpkg_Q() {
if [[ "$_TOPT" == "q" ]]; then
if [ "$_TOPT" = "q" ]; then
dpkg -l \
| grep -E '^[hi]i' \
| awk '{print $2}'
elif [[ "$_TOPT" == "" ]]; then
elif [ -z "$_TOPT" ]; then
dpkg -l "$@" \
| grep -E '^[hi]i'
else
Expand All @@ -45,7 +45,7 @@ dpkg_Qe() {
}

dpkg_Ql() {
if [[ -n "$*" ]]; then
if [ $# -ge 1 ]; then
dpkg-query -L "$@"
return
fi
Expand All @@ -54,7 +54,7 @@ dpkg_Ql() {
| grep -E '^[hi]i' \
| awk '{print $2}' \
| while read -r _pkg; do
if [[ "$_TOPT" == "q" ]]; then
if [ "$_TOPT" = "q" ]; then
dpkg-query -L "$_pkg"
else
dpkg-query -L "$_pkg" \
Expand Down Expand Up @@ -94,7 +94,7 @@ dpkg_Qs() {

# dpkg_Rs may _not_implemented
dpkg_Rs() {
if [[ "$_TOPT" == "" ]]; then
if [ -z "$_TOPT" ]; then
apt-get autoremove "$@"
else
_not_implemented
Expand Down Expand Up @@ -140,19 +140,18 @@ dpkg_Sy() {
apt-get update "$@"
}

# FIXME: A simple implemention for #53 and
# FIXME: https://github.com/icy/pacapt/pull/156
# FIXME: but I'm not sure there is any issue...
dpkg_Ss() {
local IFS=$'\n'
packages=($(apt-cache search "$@"))
for package in ${packages[@]:-}
do
name=${package%% - *}
desc=${package#* - }
dpkg-query -W "$name" > /dev/null 2>&1
if [[ $? -eq 1 ]]; then
echo -e "package/$name \n $desc"
else
dpkg-query -W -f='package/${binary:Package} ${Version}\t[${Status}]\n ${binary:Summary}\n' "$name"
fi
apt-cache search "${@:-.}" \
| while read -r name _dash desc; do
if ! dpkg-query -W "$name" > /dev/null 2>&1; then
printf "package/%s \n %s\n" \
"$name" "$desc"
else
dpkg-query -W -f='package/${binary:Package} ${Version}\n ${binary:Summary}\n' "$name"
fi
done
}

Expand All @@ -165,6 +164,7 @@ dpkg_Scc() {
}

dpkg_S() {
# shellcheck disable=SC2086
apt-get install $_TOPT "$@"
}

Expand Down
21 changes: 4 additions & 17 deletions lib/zz_main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,13 @@ _PACMAN_detect \
# Once we haven't switcher over `bash`, there is great chance
# the current system are missing `Bash` ; on these systems
# our library are not ready for pure-POSIX features!
# FIXME: Now this list has to be updated manually.
if [ -z "${__PACAPT_FORKED__:-}" ]; then
case "$_PACMAN" in
"apk") ;;
"conda") ;;
"dnf") ;;
"homebrew") ;;
"macports") ;;
"opkg") ;;
"pkgng") ;;
"pkg_tools") ;;
"portage") ;;
"sun_tools" ) ;;
"swupd" ) ;;
"tazpkg") ;;
"tlmgr" ) ;;
"yum" ) ;;
"zypper" ) ;;
*)
"cave")
_die "pacapt($_PACMAN) library is not ready for pure-POSIX features (or your Bash version is not >= 4)."
;;
*)
;;
esac
fi

Expand Down
7 changes: 5 additions & 2 deletions tests/dpkg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
# FIXME: debian:squeeze It's unable to run `pacman -Sy`
im ubuntu:14.04 ubuntu:16.04 ubuntu:18.04 ubuntu:20.04 ubuntu
im debian:jessie debian:stretch debian:buster debian
im debian_bash3

# Remove `docker-clean` because we need *.deb files under /var/cache
in ! rm -fv /etc/apt/apt.conf.d/docker-clean
ou ^removed .*docker-clean

in ! PACAPT_DEBUG=auto pacman
ou debug.*Switching to Bash shell
in ! if bash --version | grep -qs 'version 3'; then echo "Bash3-no-switching"; fi
ou (debug.*Switching to Bash shell)|(Bash3-no-switching)

in -P
ou available operations:.*( [Q][ ilos])+
Expand Down Expand Up @@ -114,7 +116,8 @@ ou ^ Package: screen
in -Sc
in clear
in -sS tmux
ou tmux
ou package/tmux
ou terminal multiplexer

# Strong cleaning up
in -Sccc
Expand Down

0 comments on commit b00aadb

Please sign in to comment.