Remove case statement

by using functions instead of case statement

- remove level of indentation
- allow use of local variables via "local" keyword
This commit is contained in:
Steven Penny 2014-05-24 20:35:41 -05:00
parent ec36e46faf
commit fbf531e638
1 changed files with 310 additions and 311 deletions

225
apt-cyg
View File

@ -124,104 +124,12 @@ function checkpackages()
fi fi
} }
# process options apt-update () {
dofile=0
command=''
file=''
filepackages=''
packages=''
while (( $# ))
do
case "$1" in
--mirror | -m)
echo "$2" > /etc/setup/last-mirror
shift 2
;;
--cache | -c)
cygpath -aw "$2" > /etc/setup/last-cache
shift 2
;;
--noscripts)
noscripts=1
shift
;;
--help)
usage
exit 0
;;
--version)
version
exit 0
;;
--file | -f)
if [[ $2 ]]
then
file=$2
dofile=1
shift
else
echo No file name provided, ignoring $1 >&2
fi
shift
;;
update)
command=$1
shift
;;
install \
| list \
| listfiles \
| depends \
| rdepends \
| search \
| searchall \
| remove \
| show)
if [[ $command ]]
then
packages+=" $1"
else
command=$1
fi
shift
;;
*)
packages+=" $1"
shift
;;
esac
done
if (( dofile ))
then
if [ -f "$file" ]
then
filepackages+=$(awk '{printf " %s", $0}' "$file")
else
echo File $file not found, skipping
fi
packages+=" $filepackages"
fi
case "$command" in
update)
findworkspace findworkspace
getsetup getsetup
;; }
list) apt-list () {
if checkpackages if checkpackages
then then
findworkspace findworkspace
@ -238,9 +146,9 @@ case "$command" in
echo The following packages are installed: >&2 echo The following packages are installed: >&2
awk 'NR>1 && $0=$1' /etc/setup/installed.db awk 'NR>1 && $0=$1' /etc/setup/installed.db
fi fi
;; }
listfiles) apt-listfiles () {
checkpackages checkpackages
for pkg in $packages for pkg in $packages
do do
@ -253,9 +161,9 @@ case "$command" in
echo echo
done | done |
head -c-1 head -c-1
;; }
show) apt-show () {
findworkspace findworkspace
checkpackages checkpackages
for pkg in $packages for pkg in $packages
@ -273,9 +181,9 @@ case "$command" in
' RS='\n\n@ ' FS='\n' query="$pkg" setup.ini ' RS='\n\n@ ' FS='\n' query="$pkg" setup.ini
done | done |
head -c-1 head -c-1
;; }
depends) apt-depends () {
findworkspace findworkspace
checkpackages checkpackages
for pkg in $packages for pkg in $packages
@ -302,9 +210,9 @@ case "$command" in
} }
' query="$pkg" setup.ini ' query="$pkg" setup.ini
done done
;; }
rdepends) apt-rdepends () {
findworkspace findworkspace
for pkg in $packages for pkg in $packages
do do
@ -317,9 +225,9 @@ case "$command" in
} }
' query="$pkg" setup.ini ' query="$pkg" setup.ini
done done
;; }
search) apt-search () {
checkpackages checkpackages
for pkg in $packages for pkg in $packages
do do
@ -338,9 +246,9 @@ case "$command" in
fi fi
done done
done done
;; }
searchall) apt-searchall () {
for pkg in $packages for pkg in $packages
do do
printf -v qs 'text=1&arch=%s&grep=%s' $ARCH "$pkg" printf -v qs 'text=1&arch=%s&grep=%s' $ARCH "$pkg"
@ -356,9 +264,9 @@ case "$command" in
$0 = $2 $0 = $2
' FS=/ matches ' FS=/ matches
done done
;; }
install) apt-install () {
findworkspace findworkspace
checkpackages checkpackages
for pkg in $packages for pkg in $packages
@ -467,9 +375,9 @@ case "$command" in
echo Package $pkg installed echo Package $pkg installed
done done
;; }
remove) apt-remove () {
checkpackages checkpackages
for pkg in $packages for pkg in $packages
do do
@ -518,10 +426,101 @@ case "$command" in
echo Package $pkg removed echo Package $pkg removed
done done
}
# process options
dofile=0
command=''
file=''
filepackages=''
packages=''
while (( $# ))
do
case "$1" in
--mirror | -m)
echo "$2" > /etc/setup/last-mirror
shift 2
;;
--cache | -c)
cygpath -aw "$2" > /etc/setup/last-cache
shift 2
;;
--noscripts)
noscripts=1
shift
;;
--help)
usage
exit 0
;;
--version)
version
exit 0
;;
--file | -f)
if [[ $2 ]]
then
file=$2
dofile=1
shift
else
echo No file name provided, ignoring $1 >&2
fi
shift
;;
update)
command=$1
shift
;;
install \
| list \
| listfiles \
| depends \
| rdepends \
| search \
| searchall \
| remove \
| show)
if [[ $command ]]
then
packages+=" $1"
else
command=$1
fi
shift
;; ;;
*) *)
usage packages+=" $1"
shift
;; ;;
esac esac
done
if (( dofile ))
then
if [ -f "$file" ]
then
filepackages+=$(awk '{printf " %s", $0}' "$file")
else
echo File $file not found, skipping
fi
packages+=" $filepackages"
fi
if type -t apt-$command | grep -q function
then
apt-$command
else
usage
fi