if --profile is set to 'default', load configuration from script directory

pull/4/head
Didier BONNEFOI 2017-06-19 18:36:33 +02:00
parent 1a0e3f220f
commit 485cbd833e
2 changed files with 39 additions and 31 deletions

View File

@ -37,14 +37,16 @@ Options
Possible arguments are: Possible arguments are:
``` ```
--url <url> : the API URL to call, for example /domains (default is /me) --url <url> : the API URL to call, for example /domains (default is /me)
--method <method> : the HTTP method to use, for example POST (default is GET) --method <method> : the HTTP method to use, for example POST (default is GET)
--data <JSON data> : the data body to send with the request --data <JSON data> : the data body to send with the request
--target <CA|EU> : the target API (default is EU) --target <CA|EU> : the target API (default is EU)
--init : to initialize the consumer key --init : to initialize the consumer key
--initApp : to initialize the API application --initApp : to initialize the API application
--profile <dir> : load a configuration from profile/ directory, (override default location) --list-profile : list available profiles in profile/ directory
--list-profile : list available profiles in profile/ directory --profile <value>
* default : from script directory
* <dir> : from profile/<dir> directory
``` ```
Usage Usage

View File

@ -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/" API_CREATE_APP_URLS[EU]="https://api.ovh.com/createApp/"
BASE_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" BASE_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
CURRENT_PATH="${BASE_PATH}"
#PROFILES_PATH
PROFILES_PATH="${BASE_PATH}/profile" PROFILES_PATH="${BASE_PATH}/profile"
HELP_CMD="$0" HELP_CMD="$0"
@ -143,14 +141,16 @@ help()
{ {
echo echo
echo "Help: possible arguments are:" echo "Help: possible arguments are:"
echo " --url <url> : the API URL to call, for example /domains (default is /me)" echo " --url <url> : the API URL to call, for example /domains (default is /me)"
echo " --method <method> : the HTTP method to use, for example POST (default is GET)" echo " --method <method> : the HTTP method to use, for example POST (default is GET)"
echo " --data <JSON data> : the data body to send with the request" echo " --data <JSON data> : the data body to send with the request"
echo " --target <$( echo ${TARGETS[@]} | sed 's/\s/|/g' )> : the target API (default is EU)" echo " --target <$( echo ${TARGETS[@]} | sed 's/\s/|/g' )> : the target API (default is EU)"
echo " --init : to initialize the consumer key" echo " --init : to initialize the consumer key"
echo " --initApp : to initialize the API application" echo " --initApp : to initialize the API application"
echo " --profile <dir> : load a configuration from profile/ directory, (override default location)" echo " --list-profile : list available profiles in profile/ directory"
echo " --list-profile : list available profiles in profile/ directory" echo " --profile <value>"
echo " * default : from script directory"
echo " * <dir> : from profile/<dir> directory"
echo echo
} }
@ -187,10 +187,9 @@ parseArguments()
;; ;;
--profile) --profile)
shift shift
initProfile "$1" PROFILE=$1
;; ;;
--list-profile) --list-profile)
shift
listProfile listProfile
exit 0 exit 0
;; ;;
@ -233,30 +232,40 @@ getJSONFieldString()
echo ${RESULT:1:${#RESULT}-2} echo ${RESULT:1:${#RESULT}-2}
} }
# override CURRENT_PATH with profile name # set CURRENT_PATH with profile name
# usage : initProfile profile_name # usage : initProfile profile_name
initProfile() initProfile()
{ {
local profile=$1 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}" ] if [ ! -d "${PROFILES_PATH}/${profile}" ]
then then
echo "${PROFILES_PATH}/${profile} should exists" echo "${PROFILES_PATH}/${profile} should exists"
listProfile
exit 1 exit 1
fi fi
# override default configuration location # override default configuration location
CURRENT_PATH="$( cd "${PROFILES_PATH}/${profile}" && pwd )" CURRENT_PATH="$( cd "${PROFILES_PATH}/${profile}" && pwd )"
HELP_CMD="${HELP_CMD} --profile ${profile}" HELP_CMD="${HELP_CMD} --profile ${profile}"
else
echo "profile not set, choose in : "
listProfile
exit 1
fi fi
} }
listProfile() listProfile()
{ {
echo "Available profiles : "
cd ${PROFILES_PATH} && ls -1 cd ${PROFILES_PATH} && ls -1
} }
@ -274,13 +283,10 @@ hasOvhAppKey()
main() main()
{ {
if [ ! -d "${PROFILES_PATH}" ]
then
mkdir "${PROFILES_PATH}"
fi
parseArguments "$@" parseArguments "$@"
initProfile ${PROFILE}
initApplication initApplication
initConsumerKey initConsumerKey