#!/bin/bash declare -f kstore > /dev/null if [ $? -ne 0 ]; then echo "kcontext depends on kstore" return 1 fi complete -W "k8s docker s3-arch" kcontext function kcontext { case $1 in k8s) shift; kcontext-k8s "$@" ;; docker) shift; kcontext-docker "$@" ;; s3-arch) shift; kcontext-s3-arch "$@" ;; *) __func_help "k8s ..." __func_head "s3-arch ..." __func_help "docker ..." return 1 ;; esac return $? } function kcontext-s3-arch { local _NAME _CONFIRM _URL _AUTH case $1 in list) kstore query -list "SELECT SUBSTR( prop, 16 ) FROM store WHERE key = 'kcontext' AND prop LIKE 's3-arch.bucket.%'" ;; save) _NAME=$2 if [ -z "$_NAME" ]; then echo "Please specify a context name" >&2 return 1 fi if [ -n "$ARCH_S3_BUCKET_URL" ] && [ -n "$ARCH_S3_AUTH" ]; then kstore get "kcontext" "s3-arch.bucket.$_NAME" 2> /dev/null > /dev/null if [ $? -eq 0 ]; then read -p "Replace existing config for \"$_NAME\"? (y/n): " _CONFIRM if [ "$_CONFIRM" != "y" ]; then return 0 fi kstore update "kcontext" "$ARCH_S3_BUCKET_URL" "s3-arch.bucket.$_NAME" kstore update "kcontext" "$ARCH_S3_AUTH" "s3-arch.auth.$_NAME" return $? else kstore add "kcontext" "$ARCH_S3_BUCKET_URL" "s3-arch.bucket.$_NAME" kstore add "kcontext" "$ARCH_S3_AUTH" "s3-arch.auth.$_NAME" return $? fi else echo "No s3-arch settings can be found in env" >&2 fi ;; load) _NAME=$2 if [ -z "$_NAME" ]; then echo "Please specify a context name" >&2 return 1 fi _URL=$( kstore get "kcontext" "s3-arch.bucket.$_NAME" 2> /dev/null ) if [ $? -ne 0 ]; then echo "No such context: $_NAME" >&2 return 1 fi _AUTH=$( kstore get "kcontext" "s3-arch.auth.$_NAME" 2> /dev/null ) if [ $? -ne 0 ]; then echo "No such context: $_NAME" >&2 return 1 fi export ARCH_S3_BUCKET_URL=$_URL export ARCH_S3_AUTH=$_AUTH ;; del) _NAME=$2 if [ -z "$_NAME" ]; then echo "Please specify a context name" >&2 return 1 fi kstore get "kcontext" "s3-arch.bucket.$_NAME" 2> /dev/null > /dev/null if [ $? -eq 0 ]; then read -p "Delete context \"$_NAME\"? (y/n): " _CONFIRM if [ "$_CONFIRM" != "y" ]; then return 0 fi kstore query "DELETE FROM store WHERE key = 'kcontext' AND prop = 's3-arch.bucket.$_NAME'" kstore query "DELETE FROM store WHERE key = 'kcontext' AND prop = 's3-arch.auth.$_NAME'" return $? else echo "No such context: $_NAME" >&2 fi ;; *) __func_head "list" __func_help "save NAME" __func_help "load NAME" __func_help "del NAME" ;; esac return 1 }