Skip to content

Instantly share code, notes, and snippets.

@m-tmatma
Last active July 8, 2022 02:11
Show Gist options
  • Save m-tmatma/2804a5b6a8dfc5a6062f62c23e7c5098 to your computer and use it in GitHub Desktop.
Save m-tmatma/2804a5b6a8dfc5a6062f62c23e7c5098 to your computer and use it in GitHub Desktop.
cache.sh
#! /bin/bash -e
function usage () {
echo "usage $0 <action> [--cache-dir <cache-dir] [--remote <remote cache url>]"
echo ""
echo "<action>:"
echo " build"
echo " update-cache"
echo ""
echo "<cache-dir>:"
echo " The location of local cache"
echo " default value is '${HOME}/shared/cached'"
echo ""
echo "<remote cache url>:"
echo " The remote cache url"
echo " default value is empty"
echo ""
echo "$0 build"
echo "$0 build --cache-dir $HOME/shared-cache"
echo "$0 build --remote http://cache-server"
echo "$0 update-cache"
echo "$0 update-cache --cache-dir $HOME/shared-cache"
echo "$0 update-cache --remote http://cache-server"
echo "$0 update-cache --remote http://cache-server --cache-dir $HOME/shared-cache"
}
function abspath (){
dirname="$(cd -- "$(dirname -- "$1")" && pwd)" || exit $?
abspath="${dirname%/}/$(basename -- "$1")"
echo $abspath
}
case $1 in
build)
ACTION=build
;;
update-cache)
ACTION=update-cache
;;
help)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
shift
OPTIONS=`getopt-n $(basename $0) -o d:r: -l cache-dir:,remote: -- "$@"`
eval set -- "$OPTIONS"
UPDATE_CACHE=
DEFAULT_LOCAL_CACHE=${HOME}/shared/cached
CACHE_DIR=$DEFAULT_LOCAL_CACHE
REMOTE_URL=
while [ $# -gt 0 ]
do
case $1 in
-d | --cache-dir)
# seek to the next parameter
shift
# parameter
CACHE_DIR=$1
;;
-r | --remote)
# seek to the next parameter
shift
# parameter
REMOTE_URL=$1
;;
--)
shift;
break
;;
esac
shift
done
# while [ $# -gt 0 ]
# do
# echo "引数=$1"
# shift
# done
echo "ACTION=$ACTION"
echo "UPDATE_CACHE=$UPDATE_CACHE"
echo "CACHE_DIR=$(abspath $CACHE_DIR)"
echo "REMOTE_URL=$REMOTE_URL"
# UPDATE_CACHE Remote Local
# ------------------------------------------------------
# 1 SOURCE_MIRROR_URL DL_DIR
# 1 - DL_DIR
# "" SOURCE_MIRROR_URL -
# "" - SOURCE_MIRROR_URL
#
if [ "$ACTION" == "update-cache" ];then
DL_DIR=$CACHE_DIR
if [ -n "$REMOTE_URL" ];then
SOURCE_MIRROR_URL=$REMOTE_URL
fi
else
if [ -n "$REMOTE_URL" ];then
SOURCE_MIRROR_URL=$REMOTE_URL
else
SOURCE_MIRROR_URL="file://$CACHE_DIR"
fi
fi
echo "ACTION=$ACTION"
echo "SOURCE_MIRROR_URL=$SOURCE_MIRROR_URL"
echo "DL_DIR=$DL_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment