From b53247626002e1c5faf47589d92e293f16d489f3 Mon Sep 17 00:00:00 2001 From: Didier BONNEFOI Date: Tue, 20 Feb 2018 17:12:34 +0100 Subject: [PATCH 1/3] 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}" ] From 3cb29bc968a83d32c1152cff0811a8d31a73e79e Mon Sep 17 00:00:00 2001 From: Didier BONNEFOI Date: Mon, 26 Feb 2018 14:18:18 +0100 Subject: [PATCH 2/3] Automaticly move profiles from legacy path to user's home, and notify about it --- ovh-api-bash-client.sh | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/ovh-api-bash-client.sh b/ovh-api-bash-client.sh index 6b871f6..e310e64 100755 --- a/ovh-api-bash-client.sh +++ b/ovh-api-bash-client.sh @@ -311,19 +311,27 @@ initProfile() 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) + legacy_default_profile=$(cd "${BASE_PATH}" && ls .ovh* access.rules 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 + # notify about migration to new location: + _echoWarning "Your profiles were in the legacy path, migrating to ${PROFILES_PATH} :" + + if [ -n "${legacy_default_profile}" ]; then + _echoWarning "> migrating default profile:" + echo "${legacy_default_profile}" + mv ${BASE_PATH}/.ovh* access.rules "${PROFILES_PATH}" + fi + + if [ -n "${legacy_profiles}" ]; then + _echoWarning "> migrating custom profiles:" + echo "${legacy_profiles}" + mv ${LEGACY_PROFILES_PATH}/* "${PROFILES_PATH}" + fi + fi fi - # if profile is not set, or with value 'default' if [[ -z "${profile}" ]] || [[ "${profile}" == "default" ]] then From c0eb83d771e90dffbfbce9421b6afe04f49d4b0f Mon Sep 17 00:00:00 2001 From: Didier BONNEFOI Date: Mon, 26 Feb 2018 14:23:00 +0100 Subject: [PATCH 3/3] fix default profile move to new location --- ovh-api-bash-client.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ovh-api-bash-client.sh b/ovh-api-bash-client.sh index e310e64..b187c1c 100755 --- a/ovh-api-bash-client.sh +++ b/ovh-api-bash-client.sh @@ -320,7 +320,7 @@ initProfile() if [ -n "${legacy_default_profile}" ]; then _echoWarning "> migrating default profile:" echo "${legacy_default_profile}" - mv ${BASE_PATH}/.ovh* access.rules "${PROFILES_PATH}" + mv ${BASE_PATH}/{.ovh*,access.rules} "${PROFILES_PATH}" fi if [ -n "${legacy_profiles}" ]; then