diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2018-10-12 12:12:20 +0200 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2018-10-12 12:12:20 +0200 |
commit | 5e7b2dec616b844dd881897727d0edceb90dfa85 (patch) | |
tree | d418ad897ef512fd811c9b5eece3e458b9f329b2 /utils.go | |
parent | 35586adb40790f6b0b15c43d4bae86d29090736a (diff) |
Make id option overwrite XDS_xxx_ID env var
The behavior must be the same when XDS_SDK_ID is defined and user
set -id option or simply give an id without setting "--id" string
option.
IOW all following commands must give the exact same result (get info
of sdk 9d69):
./bin/xds-cli sdks get -id 9d69
./bin/xds-cli sdks get 9d69
XDS_SDK_ID=9d69 ./bin/xds-cli sdks get
XDS_SDK_ID=1234 ./bin/xds-cli sdks get -id 9d69
XDS_SDK_ID=12345 ./bin/xds-cli sdks get 9d69
Change-Id: Idbd3f052df8172d56de98f827743b43d70d37393
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'utils.go')
-rw-r--r-- | utils.go | 18 |
1 files changed, 14 insertions, 4 deletions
@@ -21,6 +21,7 @@ package main import ( "encoding/json" "fmt" + "os" "sort" "strconv" "strings" @@ -122,18 +123,27 @@ func LogPost(format string, data interface{}) { } // GetID Return a string ID set with --id option or as simple parameter -func GetID(ctx *cli.Context) string { - return GetIDName(ctx, "id") +func GetID(ctx *cli.Context, idEnvVarName string) string { + return GetIDName(ctx, "id", idEnvVarName) } // GetIDName Return a string ID set with --XXX option or as simple parameter -func GetIDName(ctx *cli.Context, idName string) string { +func GetIDName(ctx *cli.Context, idName, idEnvVarName string) string { if idName == "" { return "" } + + // Get id set using option --id id := ctx.String(idName) + + // Check if id has been set using env variable + envSdkid, _ := os.LookupEnv(idEnvVarName) + + // Support as 1st arg without --id option string (for example xds-cli sdk install 123456) idArgs := ctx.Args().First() - if id == "" && idArgs != "" { + + // Set or overwrite id using first arg (knowing that 1st is overwrite env variable) + if (id == "" && idArgs != "") || (envSdkid != "" && idArgs != "") { id = idArgs } return id |