From 47a77e96a216a5f500d4c568c4bb9543adec3a8b Mon Sep 17 00:00:00 2001 From: danaelhe <42972711+danaelhe@users.noreply.github.com> Date: Mon, 24 Jul 2023 11:28:00 -0400 Subject: [PATCH 1/2] auth: init recognizes --access-token --- commands/auth.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/commands/auth.go b/commands/auth.go index b90db3bfa..95a039cb5 100644 --- a/commands/auth.go +++ b/commands/auth.go @@ -146,7 +146,14 @@ To create new contexts, see the help for `+"`"+`doctl auth init`+"`"+`.`, Writer // XDG_CONFIG_HOME is not set, use $HOME/.config. On Windows use %APPDATA%/doctl/config. func RunAuthInit(retrieveUserTokenFunc func() (string, error)) func(c *CmdConfig) error { return func(c *CmdConfig) error { - token := c.getContextAccessToken() + token := viper.GetString(doctl.ArgAccessToken) + + // if --access-token is passed, set the context. + if token != "" { + c.setContextAccessToken(token) + } + + token = c.getContextAccessToken() context := strings.ToLower(Context) if context == "" { context = strings.ToLower(viper.GetString("context")) From a97dd700416400fc3c8dbb5954bceb6fd8fc8d95 Mon Sep 17 00:00:00 2001 From: danaelhe <42972711+danaelhe@users.noreply.github.com> Date: Mon, 24 Jul 2023 16:06:26 -0400 Subject: [PATCH 2/2] add integration test --- integration/auth_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/integration/auth_test.go b/integration/auth_test.go index de22ebad0..90199c19b 100644 --- a/integration/auth_test.go +++ b/integration/auth_test.go @@ -158,6 +158,35 @@ context: default expect.Contains(string(fileBytes), "access-token: some-magic-token") }) + + it("validates and saves an existing token non-interactively to the --context specified", func() { + var testConfigBytes = []byte(`access-token: first-token +context: default +`) + + tmpDir := t.TempDir() + testConfig := filepath.Join(tmpDir, "test-config.yml") + expect.NoError(ioutil.WriteFile(testConfig, testConfigBytes, 0644)) + + cmd := exec.Command(builtBinaryPath, + "-u", server.URL, + "--config", testConfig, + "auth", + "init", + "--access-token", "some-magic-token", + "--context", "new-context", + "--token-validation-server", server.URL, + ) + + _, err := cmd.CombinedOutput() + expect.NoError(err) + + fileBytes, err := ioutil.ReadFile(testConfig) + expect.NoError(err) + + expect.Contains(string(fileBytes), "access-token: some-magic-token") + expect.Contains(string(fileBytes), "new-context: some-magic-token") + }) }) when("no custom config is provided", func() {