From 5e7b2dec616b844dd881897727d0edceb90dfa85 Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Fri, 12 Oct 2018 12:12:20 +0200 Subject: [PATCH] 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 --- .vscode/settings.json | 99 +++++++++++++++++++++++++-------------------------- cmd-projects.go | 6 ++-- cmd-sdks.go | 8 ++--- cmd-target.go | 6 ++-- utils.go | 18 +++++++--- 5 files changed, 72 insertions(+), 65 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index ec95b48..2dfc5d6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,53 +1,50 @@ // Place your settings in this file to overwrite default and user settings. { - // Specify GOPATH here to override the one that is set as environment variable. The inferred GOPATH from workspace root overrides this, if go.inferGopath is set to true. - "go.gopath": "${workspaceRoot}/../../../../../..", - - // The number of spaces a tab is equal to. This setting is overriden based on the file contents when `editor.detectIndentation` is on. - "editor.tabSize": 4, - // Insert spaces when pressing Tab. This setting is overriden based on the file contents when `editor.detectIndentation` is on. - "editor.insertSpaces": true, - - // When opening a file, `editor.tabSize` and `editor.insertSpaces` will be detected based on the file contents. - "editor.detectIndentation": true, - - // Configure glob patterns for excluding files and folders. - "files.exclude": { - "**/.tmp": true, - ".git": true, - "glide.lock": true, - "vendor": true, - "debug": true, - "bin": true, - "tools": true - }, - // Words to add to dictionary for a workspace. - "cSpell.words": [ - "apiv", - "iosk", - "zhouhui", - "ldflags", - "socketio", - "xdsconfig", - "sdkid", - "godotenv", - "crosssdk", - "prjs", - "xaapiv", - "urfave", - "sebd", - "golib", - "joho", - "XDSAGENT", - "reflectme", - "franciscocpg", - "gerrit", - "EVTSDK", - "tgts", - "sigs", - "rdfs", - "goselect", - "creack", - "Sillyf" - ] -} + // Specify GOPATH here to override the one that is set as environment variable. The inferred GOPATH from workspace root overrides this, if go.inferGopath is set to true. + "go.gopath": "${workspaceRoot}/../../../../../..", + // The number of spaces a tab is equal to. This setting is overriden based on the file contents when `editor.detectIndentation` is on. + "editor.tabSize": 4, + // Insert spaces when pressing Tab. This setting is overriden based on the file contents when `editor.detectIndentation` is on. + "editor.insertSpaces": true, + // When opening a file, `editor.tabSize` and `editor.insertSpaces` will be detected based on the file contents. + "editor.detectIndentation": true, + // Configure glob patterns for excluding files and folders. + "files.exclude": { + "**/.tmp": true, + ".git": true, + "glide.lock": true, + "vendor": true, + "debug": true, + "bin": true, + "tools": true + }, + // Words to add to dictionary for a workspace. + "cSpell.words": [ + "apiv", + "iosk", + "zhouhui", + "ldflags", + "socketio", + "xdsconfig", + "sdkid", + "godotenv", + "crosssdk", + "prjs", + "xaapiv", + "urfave", + "sebd", + "golib", + "joho", + "XDSAGENT", + "reflectme", + "franciscocpg", + "gerrit", + "EVTSDK", + "tgts", + "sigs", + "rdfs", + "goselect", + "creack", + "Sillyf" + ] +} \ No newline at end of file diff --git a/cmd-projects.go b/cmd-projects.go index e056563..fd46d9a 100644 --- a/cmd-projects.go +++ b/cmd-projects.go @@ -130,7 +130,7 @@ func projectsList(ctx *cli.Context) error { } func projectsGet(ctx *cli.Context) error { - id := GetID(ctx) + id := GetID(ctx, "XDS_PROJECT_ID") if id == "" { return cli.NewExitError("id parameter or option must be set", 1) } @@ -216,7 +216,7 @@ func projectsAdd(ctx *cli.Context) error { func projectsRemove(ctx *cli.Context) error { var res xaapiv1.ProjectConfig - id := GetID(ctx) + id := GetID(ctx, "XDS_PROJECT_ID") if id == "" { return cli.NewExitError("id parameter or option must be set", 1) } @@ -236,7 +236,7 @@ func projectsRemove(ctx *cli.Context) error { } func projectsSync(ctx *cli.Context) error { - id := GetID(ctx) + id := GetID(ctx, "XDS_PROJECT_ID") if id == "" { return cli.NewExitError("id parameter or option must be set", 1) } diff --git a/cmd-sdks.go b/cmd-sdks.go index e4012e7..35b053e 100644 --- a/cmd-sdks.go +++ b/cmd-sdks.go @@ -142,7 +142,7 @@ func sdksList(ctx *cli.Context) error { } func sdksGet(ctx *cli.Context) error { - id := GetID(ctx) + id := GetID(ctx, "XDS_SDK_ID") if id == "" { return cli.NewExitError("id parameter or option must be set", 1) } @@ -226,7 +226,7 @@ func _sdksListGet(sdks *[]xaapiv1.SDK) error { } func sdksInstall(ctx *cli.Context) error { - id := GetID(ctx) + id := GetID(ctx, "XDS_SDK_ID") file := ctx.String("file") force := ctx.Bool("force") shortOut := ctx.Bool("short") @@ -326,7 +326,7 @@ func sdksInstall(ctx *cli.Context) error { } func sdksUnInstall(ctx *cli.Context) error { - id := GetID(ctx) + id := GetID(ctx, "XDS_SDK_ID") if id == "" { return cli.NewExitError("id parameter or option must be set", 1) } @@ -350,7 +350,7 @@ func sdksUnInstall(ctx *cli.Context) error { } func sdksAbort(ctx *cli.Context) error { - id := GetID(ctx) + id := GetID(ctx, "XDS_SDK_ID") if id == "" { return cli.NewExitError("id parameter or option must be set", 1) } diff --git a/cmd-target.go b/cmd-target.go index 33b757d..6140755 100644 --- a/cmd-target.go +++ b/cmd-target.go @@ -165,7 +165,7 @@ func targetsList(ctx *cli.Context) error { } func targetsGet(ctx *cli.Context) error { - id := GetID(ctx) + id := GetID(ctx, "XDS_TARGET_ID") if id == "" { return cli.NewExitError("id parameter or option must be set", 1) } @@ -254,7 +254,7 @@ func targetsAdd(ctx *cli.Context) error { func targetsRemove(ctx *cli.Context) error { var res xaapiv1.TargetConfig - id := GetID(ctx) + id := GetID(ctx, "XDS_TARGET_ID") if id == "" { return cli.NewExitError("id parameter or option must be set", 1) } @@ -543,7 +543,7 @@ func GetTargetAndTerminalIDs(ctx *cli.Context, useFirstFree bool) (*xaapiv1.Targ } // Allow to create a new terminal when only target id is set - idArg = GetIDName(ctx, "id") + idArg = GetIDName(ctx, "id", "XDS_TARGET_ID") if idArg == "" { return nil, nil, fmt.Errorf("id or termId argument must be set") } diff --git a/utils.go b/utils.go index 393ee2a..dc5cb57 100644 --- a/utils.go +++ b/utils.go @@ -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 -- 2.16.6