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
pull/4/head
Didier BONNEFOI 2017-06-21 14:31:13 +02:00
parent 143ad5114b
commit 71c1d5fe6f
2 changed files with 31 additions and 13 deletions

View File

@ -1,4 +1,4 @@
ovh API Bash client orovh API Bash client
================ ================
A bash client for OVH API (https://api.ovh.com/) 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}' ./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 demo1 --init
./ovh-api-bash-client.sh --profile demo2 --init ./ovh-api-bash-client.sh --profile demo2 --init
``` ```

View File

@ -234,10 +234,13 @@ getJSONFieldString()
} }
# set CURRENT_PATH with profile name # 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() initProfile()
{ {
local profile=$1 local createProfile=$1
local profile=$2
if [ ! -d "${PROFILES_PATH}" ] if [ ! -d "${PROFILES_PATH}" ]
then then
@ -253,12 +256,23 @@ initProfile()
# ensure profile directory exists # ensure profile directory exists
if [ ! -d "${PROFILES_PATH}/${profile}" ] if [ ! -d "${PROFILES_PATH}/${profile}" ]
then then
echo "${PROFILES_PATH}/${profile} should exists" case ${createProfile} in
listProfile get)
exit 1 echo "${PROFILES_PATH}/${profile} should exists"
listProfile
exit 1
;;
set)
mkdir "${PROFILES_PATH}/${profile}" || exit 1
;;
esac
fi fi
# override default configuration location # override default configuration location
CURRENT_PATH="$( cd "${PROFILES_PATH}/${profile}" && pwd )" CURRENT_PATH="$( cd "${PROFILES_PATH}/${profile}" && pwd )"
fi
if [ -n "${profile}" ]
then
HELP_CMD="${HELP_CMD} --profile ${profile}" HELP_CMD="${HELP_CMD} --profile ${profile}"
fi fi
@ -269,10 +283,11 @@ listProfile()
local dir= local dir=
echo "Available profiles : " echo "Available profiles : "
echo "- default" echo "- default"
if [ -d "${PROFILES_PATH}" ] if [ -d "${PROFILES_PATH}" ]
then then
# only list directory # only list directory
for dir in $(cd ${PROFILES_PATH}; echo */); for dir in $(cd ${PROFILES_PATH} && ls -d */ 2>/dev/null)
do do
# display directory name without slash # display directory name without slash
echo "- ${dir%%/}" echo "- ${dir%%/}"
@ -296,8 +311,13 @@ main()
parseArguments "$@" parseArguments "$@"
# set profile location local profileAction="get"
initProfile ${PROFILE}
if [ -n "${INIT_KEY_ACTION}" ]; then
profileAction="set"
fi
initProfile ${profileAction} ${PROFILE}
# user want to add An API Key # user want to add An API Key
case ${INIT_KEY_ACTION} in case ${INIT_KEY_ACTION} in