X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fxds%2Fxds-cli.git;a=blobdiff_plain;f=utils.go;h=dc5cb57a29563893751abf45d4453f07a2bbc0aa;hp=afa0942071761e8bdcbe82250b8126058e05f42d;hb=3b5e82b55433fd49cfe0cd0349756e0c2e9a9c12;hpb=5ce05ccae810f532e73350a205b979faf2603235 diff --git a/utils.go b/utils.go index afa0942..dc5cb57 100644 --- a/utils.go +++ b/utils.go @@ -21,6 +21,7 @@ package main import ( "encoding/json" "fmt" + "os" "sort" "strconv" "strings" @@ -122,10 +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 { - id := ctx.String("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, 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 @@ -139,3 +157,8 @@ func Confirm(question string) bool { ans := strings.ToLower(strings.TrimSpace(answer)) return (ans == "y" || ans == "yes") } + +// compareID Compare an ID to a reference ID +func compareID(refID, ID string) bool { + return refID != "" && ID != "" && strings.HasPrefix(refID, ID) +}