From b53247626002e1c5faf47589d92e293f16d489f3 Mon Sep 17 00:00:00 2001 From: Didier BONNEFOI Date: Tue, 20 Feb 2018 17:12:34 +0100 Subject: [PATCH] move profiles to user home --- README.md | 8 ++++---- ovh-api-bash-client.sh | 41 +++++++++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 865f7e8..60ec14d 100644 --- a/README.md +++ b/README.md @@ -41,12 +41,12 @@ Possible arguments are: --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 + --init : to initialize the consumer key, and manage custom access rules file --initApp : to initialize the API application - --list-profile : list available profiles in profile/ directory + --list-profile : list available profiles in ~/.ovh-api-bash-client/profile directory --profile - * default : from script directory - * : from profile/ directory + * default : from ~/.ovh-api-bash-client/profile directory + * : from ~/.ovh-api-bash-client/profile/ directory ``` Usage diff --git a/ovh-api-bash-client.sh b/ovh-api-bash-client.sh index 298e583..6b871f6 100755 --- a/ovh-api-bash-client.sh +++ b/ovh-api-bash-client.sh @@ -32,7 +32,8 @@ do done BASE_PATH=$( cd -P "$( dirname "${SOURCE}" )" && pwd ) -PROFILES_PATH="${BASE_PATH}/profile" +LEGACY_PROFILES_PATH="${BASE_PATH}/profile" +PROFILES_PATH="${HOME}/.ovh-api-bash-client/profile" HELP_CMD="$0" @@ -45,6 +46,11 @@ SIGDATA="" POST_DATA="" PROFILE="" +_echoWarning() +{ + echo >&2 "[WARNING] $*" +} + isTargetValid() { VALID=0 @@ -161,10 +167,10 @@ help() echo " --target <$( echo ${TARGETS[@]} | sed 's/\s/|/g' )> : the target API (default is EU)" echo " --init : to initialize the consumer key, and manage custom access rules file" echo " --initApp : to initialize the API application" - echo " --list-profile : list available profiles in profile/ directory" + echo " --list-profile : list available profiles in ~/.ovh-api-bash-client/profile directory" echo " --profile " - echo " * default : from script directory" - echo " * : from profile/ directory" + echo " * default : from ~/.ovh-api-bash-client/profile directory" + echo " * : from ~/.ovh-api-bash-client/profile/ directory" echo } @@ -200,7 +206,7 @@ buildAccessRules() done < "${access_rules_file}" json_rules=${json_rules::-1} if [ -z "${json_rules}" ]; then - echo "no rule defined, please verify your file '${access_rules_file}'" >&2 + echoWarning "no rule defined, please verify your file '${access_rules_file}'" exit 1 fi @@ -296,14 +302,33 @@ initProfile() if [ ! -d "${PROFILES_PATH}" ] then - mkdir "${PROFILES_PATH}" || exit 1 + mkdir -pv "${PROFILES_PATH}" || exit 1 fi + # checking if some profiles remains in legacy profile path + local legacy_profiles= + local legacy_default_profile= + if [ -d "${LEGACY_PROFILES_PATH}" ]; then + # is there any profile in legacy path ? + legacy_profiles=$(ls -A "${LEGACY_PROFILES_PATH}" 2>/dev/null) + legacy_default_profile=$(ls -A "${BASE_PATH}"/.ovh* 2>/dev/null) + if [ -n "${legacy_profiles}" ] || [ -n "${legacy_default_profile}" ]; then + _echoWarning "Your profiles resides in the legacy path:" + echo "${legacy_profiles}" + echo "${legacy_default_profile}" + _echoWarning "Please move them to this new location like this:" + [ -n "${legacy_profiles}" ] && _echoWarning " mv ${LEGACY_PROFILES_PATH}/* ${PROFILES_PATH}" + [ -n "${legacy_default_profile}" ] && _echoWarning " mv ${BASE_PATH}/.ovh* access.rules ${PROFILES_PATH}" + exit 1 + fi + 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}" + # configuration stored in the profile main path + CURRENT_PATH="${PROFILES_PATH}" else # ensure profile directory exists if [ ! -d "${PROFILES_PATH}/${profile}" ]