Skip to content

Instantly share code, notes, and snippets.

@Lucho00Cuba
Created January 1, 2023 14:55
Show Gist options
  • Select an option

  • Save Lucho00Cuba/2f01ea5fc9845be567685e70ba27c6f7 to your computer and use it in GitHub Desktop.

Select an option

Save Lucho00Cuba/2f01ea5fc9845be567685e70ba27c6f7 to your computer and use it in GitHub Desktop.
ChartMuseum CLI
#!/usr/bin/env bash
function chartmuseum {
# Funcs
function http {
FILTER=""
if [[ ! -z ${map['filter']} ]]; then
FILTER='.[] | to_entries | map(. | select (.key == "'${map['filter']}'" ) | {(.key): .value} ) | .[]'
fi
# Actions
if [[ ${map['action']} == "list" ]]; then
curl -X GET $1 2>/dev/null | /rancher/bin/jq ; #echo -e "\n"
elif [[ ${map['action']} == "push" ]]; then
curl -sS --data-binary "@${map['file']}" $1 2>/dev/null | /rancher/bin/jq ; # echo -e "\n"
elif [[ ${map['action']} == "delete" ]]; then
curl -sSX DELETE $1 2>/dev/null | /rancher/bin/jq ; # echo -e "\n"
elif [[ ${map['action']} == "pull" ]]; then
curl -sSX GET $1 2>/dev/null | /rancher/bin/jq ; # echo -e "\n"
else
curl -X ${map['action']^^} $1 2>/dev/null | /rancher/bin/jq "$FILTER" ; # echo -e "\n"
fi
}
function push {
if [[ ! -z ${map['file']} ]]; then
if [[ -f ${map['file']} ]]; then
echo "requests: http://${map['server']}/api/charts/"
http "http://${map['server']}/api/charts"
else
echo "file not found"
fi
else
echo "not found -file [VALUE]"
fi
}
function delete {
if [[ ! -z ${map['name']} ]]; then
if [[ ! -z ${map['version']} ]]; then
http "http://${map['server']}/api/charts/${map['name']}/${map['version']}"
else
echo echo "not found -version [VALUE]"
exit 1
fi
else
echo echo "not found -name [VALUE]"
fi
}
function pull {
if [[ ! -z ${map['name']} ]]; then
if [[ ! -z ${map['version']} ]]; then
if [[ ! -z ${map['output']} ]]; then
http "http://${map['server']}/charts/${map['name']}-${map['version']}.tgz --output ${map['output']}/${map['name']}-${map['version']}.tgz"
map['filedir']="${map['output']}/${map['name']}-${map['version']}.tgz"
else
http "http://${map['server']}/charts/${map['name']}-${map['version']}.tgz --output ${map['name']}-${map['version']}.tgz"
map['filedir']="${map['name']}-${map['version']}.tgz"
fi
if [[ $(echo $?) == 0 ]]; then
echo -e "state: ok\nfile: ${map['filedir']}"
else
echo "state: fail"
fi
else
echo "not found -version [VALUE]"
fi
else
echo "not found -name [VALUE]"
fi
}
function get {
if [[ ! -z ${map['name']} ]]; then
if [[ ! -z ${map['version']} ]]; then
echo "requests: http://${map['server']}/api/charts/${map['name']}/${map['version']}"
http "http://${map['server']}/api/charts/${map['name']}/${map['version']}"
else
echo "requests: http://${map['server']}/api/charts/${map['name']}"
http "http://${map['server']}/api/charts/${map['name']}"
fi
else
echo "not found -name [VALUE]"
exit 1
fi
}
function list {
if [[ ! -z ${map['server']} ]]; then
echo "requests: http://${map['server']}/api/charts"
http "http://${map['server']}/api/charts"
else
echo "not found -server [VALUE]"
exit 1
fi
}
# MAIN
#if [[ $# -eq 0 ]]; then
# echo "not found params"
# exit 1
#fi
declare -A map
while test $# -gt 0; do
key=${1/-/}
case "$1" in
*)
shift
value=$1
shift
#echo "$key: $value"
map[${key,,}]=${value,,}
;;
esac
done
if [[ ! -z ${map['action']} && ${map['action']} != " " ]]; then
if [[ ${map['action']} != "push" && ${map['action']} != "get" && ${map['action']} != "delete" && ${map['action']} != "list" && ${map['action']} != "pull" ]]; then
echo "not found action: ${map['action']}"
exit 1
fi
else
echo "not found action [push, get, delete, list, pull]"
exit 1
fi
# Print Data
for key in ${!map[@]}; do
echo "${key}: ${map[$key]}"
done
#echo "keys: ${!map[@]}"
#echo "values: ${map[@]}"
# Default Server
if [[ -z ${map['server']} ]]; then
map['server']="10.43.18.19"
fi
# Invoke
${map['action']}
}
chartmuseum $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment