From 71c1d5fe6f031bf93091489074800d4d83fc2ee6 Mon Sep 17 00:00:00 2001 From: Didier BONNEFOI Date: Wed, 21 Jun 2017 14:31:13 +0200 Subject: [PATCH] profiles management enhancements - when launching --init/--initApp, create the defined profile if missing - fix profiles listing (bad directory) - help command : always add --profile argument if profile name is defined --- README.md | 6 ++---- ovh-api-bash-client.sh | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index bb9afd0..5c87b24 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -ovh API Bash client + orovh API Bash client ================ A bash client for OVH API (https://api.ovh.com/) @@ -69,10 +69,8 @@ To activate the monitoring on your dedicated server, run: ./ovh-api-bash-client.sh --method PUT --url "/dedicated/server/ns00000.ovh.net" --data '{"monitoring": true}' ``` -create a Consumer key for different account or usage +To create a Consumer key for different account or usage (profile is created if missing) ``` - mkdir profile/demo1 - mkdir profile/demo2 ./ovh-api-bash-client.sh --profile demo1 --init ./ovh-api-bash-client.sh --profile demo2 --init ``` diff --git a/ovh-api-bash-client.sh b/ovh-api-bash-client.sh index 20990b3..85c0a3d 100755 --- a/ovh-api-bash-client.sh +++ b/ovh-api-bash-client.sh @@ -234,10 +234,13 @@ getJSONFieldString() } # set CURRENT_PATH with profile name -# usage : initProfile profile_name +# usage : initProfile |set|get] profile_name +# set : create the profile if missing +# get : raise an error if no profile with that name initProfile() { - local profile=$1 + local createProfile=$1 + local profile=$2 if [ ! -d "${PROFILES_PATH}" ] then @@ -253,12 +256,23 @@ initProfile() # ensure profile directory exists if [ ! -d "${PROFILES_PATH}/${profile}" ] then - echo "${PROFILES_PATH}/${profile} should exists" - listProfile - exit 1 + case ${createProfile} in + get) + echo "${PROFILES_PATH}/${profile} should exists" + listProfile + exit 1 + ;; + set) + mkdir "${PROFILES_PATH}/${profile}" || exit 1 + ;; + esac fi # override default configuration location CURRENT_PATH="$( cd "${PROFILES_PATH}/${profile}" && pwd )" + fi + + if [ -n "${profile}" ] + then HELP_CMD="${HELP_CMD} --profile ${profile}" fi @@ -269,10 +283,11 @@ listProfile() local dir= echo "Available profiles : " echo "- default" + if [ -d "${PROFILES_PATH}" ] then - # only list directory - for dir in $(cd ${PROFILES_PATH}; echo */); + # only list directory + for dir in $(cd ${PROFILES_PATH} && ls -d */ 2>/dev/null) do # display directory name without slash echo "- ${dir%%/}" @@ -296,8 +311,13 @@ main() parseArguments "$@" - # set profile location - initProfile ${PROFILE} + local profileAction="get" + + if [ -n "${INIT_KEY_ACTION}" ]; then + profileAction="set" + fi + + initProfile ${profileAction} ${PROFILE} # user want to add An API Key case ${INIT_KEY_ACTION} in