From 485cbd833ef5643f97f8217f712f347436023580 Mon Sep 17 00:00:00 2001 From: Didier BONNEFOI Date: Mon, 19 Jun 2017 18:36:33 +0200 Subject: [PATCH] if --profile is set to 'default', load configuration from script directory --- README.md | 18 ++++++++------- ovh-api-bash-client.sh | 52 +++++++++++++++++++++++------------------- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index ce39407..bb9afd0 100644 --- a/README.md +++ b/README.md @@ -37,14 +37,16 @@ Options Possible arguments are: ``` - --url : the API URL to call, for example /domains (default is /me) - --method : the HTTP method to use, for example POST (default is GET) - --data : the data body to send with the request - --target : the target API (default is EU) - --init : to initialize the consumer key - --initApp : to initialize the API application - --profile : load a configuration from profile/ directory, (override default location) - --list-profile : list available profiles in profile/ directory + --url : the API URL to call, for example /domains (default is /me) + --method : the HTTP method to use, for example POST (default is GET) + --data : the data body to send with the request + --target : the target API (default is EU) + --init : to initialize the consumer key + --initApp : to initialize the API application + --list-profile : list available profiles in profile/ directory + --profile + * default : from script directory + * : from profile/ directory ``` Usage diff --git a/ovh-api-bash-client.sh b/ovh-api-bash-client.sh index 5139f32..c1f351d 100755 --- a/ovh-api-bash-client.sh +++ b/ovh-api-bash-client.sh @@ -20,8 +20,6 @@ API_CREATE_APP_URLS[CA]="https://ca.api.ovh.com/createApp/" API_CREATE_APP_URLS[EU]="https://api.ovh.com/createApp/" BASE_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -CURRENT_PATH="${BASE_PATH}" -#PROFILES_PATH PROFILES_PATH="${BASE_PATH}/profile" HELP_CMD="$0" @@ -143,14 +141,16 @@ help() { echo echo "Help: possible arguments are:" - echo " --url : the API URL to call, for example /domains (default is /me)" - echo " --method : the HTTP method to use, for example POST (default is GET)" - echo " --data : the data body to send with the request" - echo " --target <$( echo ${TARGETS[@]} | sed 's/\s/|/g' )> : the target API (default is EU)" - echo " --init : to initialize the consumer key" - echo " --initApp : to initialize the API application" - echo " --profile : load a configuration from profile/ directory, (override default location)" - echo " --list-profile : list available profiles in profile/ directory" + echo " --url : the API URL to call, for example /domains (default is /me)" + echo " --method : the HTTP method to use, for example POST (default is GET)" + echo " --data : the data body to send with the request" + echo " --target <$( echo ${TARGETS[@]} | sed 's/\s/|/g' )> : the target API (default is EU)" + echo " --init : to initialize the consumer key" + echo " --initApp : to initialize the API application" + echo " --list-profile : list available profiles in profile/ directory" + echo " --profile " + echo " * default : from script directory" + echo " * : from profile/ directory" echo } @@ -187,10 +187,9 @@ parseArguments() ;; --profile) shift - initProfile "$1" + PROFILE=$1 ;; --list-profile) - shift listProfile exit 0 ;; @@ -233,30 +232,40 @@ getJSONFieldString() echo ${RESULT:1:${#RESULT}-2} } -# override CURRENT_PATH with profile name +# set CURRENT_PATH with profile name # usage : initProfile profile_name initProfile() { local profile=$1 - if [ -n "${profile}" ]; then + if [ ! -d "${PROFILES_PATH}" ] + then + mkdir "${PROFILES_PATH}" || exit 1 + fi + + # if profile is not set, or with value 'default' + if [[ -z "${profile}" ]] || [[ "${profile}" == "default" ]] + then + # configuration stored in the script path + CURRENT_PATH="${BASE_PATH}" + else + # ensure profile directory exists if [ ! -d "${PROFILES_PATH}/${profile}" ] then echo "${PROFILES_PATH}/${profile} should exists" + listProfile exit 1 fi # override default configuration location CURRENT_PATH="$( cd "${PROFILES_PATH}/${profile}" && pwd )" HELP_CMD="${HELP_CMD} --profile ${profile}" - else - echo "profile not set, choose in : " - listProfile - exit 1 fi + } listProfile() { + echo "Available profiles : " cd ${PROFILES_PATH} && ls -1 } @@ -274,13 +283,10 @@ hasOvhAppKey() main() { - if [ ! -d "${PROFILES_PATH}" ] - then - mkdir "${PROFILES_PATH}" - fi - parseArguments "$@" + initProfile ${PROFILE} + initApplication initConsumerKey