Skip to content

Commit 783d252

Browse files
authored
auth: print the agent setup prompt after login (PDOCS-4939) (#2087)
The signed-in page already offers the prompt with a copy button. Print the same text in the terminal, so users who don't look at the browser tab still see it.
1 parent a5b98dd commit 783d252

5 files changed

Lines changed: 40 additions & 5 deletions

File tree

‎commands/auth_login.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ func RunAuthLogin(c *CmdConfig) error {
191191
if saveScope {
192192
displayOAuthDefaultScopesSaved(c, scopes)
193193
}
194+
displayAgentSetupHint(c)
194195

195196
return nil
196197
}
@@ -357,6 +358,10 @@ func displayOAuthLoginSummary(c *CmdConfig, authContext string, token *oauth.Tok
357358
}
358359
}
359360

361+
func displayAgentSetupHint(c *CmdConfig) {
362+
template.Render(c.Out, `{{nl}}{{muted "Paste this setup prompt into your agent:"}}{{nl}} {{highlight .}}{{nl}}`, oauth.AgentSetupPrompt)
363+
}
364+
360365
func displayOAuthDefaultScopesSaved(c *CmdConfig, scopes string) {
361366
if scopes == "" {
362367
template.Render(c.Out, `{{muted "Cleared the saved default scopes."}}{{nl}}`, nil)

‎commands/auth_login_test.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,34 @@ func TestRunAuthLoginWarnsThatTheCurrentContextIsReplaced(t *testing.T) {
8383
assert.Equal(t, doctl.ArgDefaultContext, viper.GetString(doctl.ArgContext))
8484
}
8585

86+
func TestRunAuthLoginPrintsTheAgentSetupPrompt(t *testing.T) {
87+
config, _ := newOAuthTestCmdConfig(t, "https://cloud.example.com")
88+
var out bytes.Buffer
89+
config.Out = &out
90+
91+
stubOAuthLogin(t, func(_ context.Context, _ oauth.LoginOptions) (*oauth.Token, error) {
92+
return &oauth.Token{AccessToken: "doo_v1_access", RefreshToken: "dor_v1_refresh"}, nil
93+
})
94+
95+
require.NoError(t, RunAuthLogin(config))
96+
97+
assert.Contains(t, out.String(), oauth.AgentSetupPrompt, "the terminal shows the same prompt as the signed-in page")
98+
}
99+
100+
func TestRunAuthLoginFailureOmitsTheAgentSetupPrompt(t *testing.T) {
101+
config, _ := newOAuthTestCmdConfig(t, "https://cloud.example.com")
102+
var out bytes.Buffer
103+
config.Out = &out
104+
105+
stubOAuthLogin(t, func(_ context.Context, _ oauth.LoginOptions) (*oauth.Token, error) {
106+
return nil, &oauth.AuthorizationError{Code: "access_denied"}
107+
})
108+
109+
require.Error(t, RunAuthLogin(config))
110+
111+
assert.NotContains(t, out.String(), "get-started.md")
112+
}
113+
86114
func TestRunAuthLoginWithoutContextLeavesTheWorkingContextAlone(t *testing.T) {
87115
config, _ := newOAuthTestCmdConfig(t, "https://cloud.example.com")
88116
viper.Set(doctl.ArgContext, "your-team")

‎integration/auth_login_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ var _ = suite("auth/login", func(t *testing.T, when spec.G, it spec.S) {
160160
combined := output.String()
161161
expect.Contains(combined, "Signed in as")
162162
expect.Contains(combined, "sammy@example.com")
163+
expect.Contains(combined, "https://docs.digitalocean.com/get-started.md", "the terminal shows the agent setup prompt")
163164
expect.Contains(combined, "My Team")
164165

165166
challenge, kind, clientID, scope, redirectURI, tokenForm := as.snapshot()
@@ -224,6 +225,7 @@ var _ = suite("auth/login", func(t *testing.T, when spec.G, it spec.S) {
224225

225226
combined := output.String()
226227
expect.Contains(combined, "access_denied")
228+
expect.NotContains(combined, "get-started.md", "a declined authorization does not show the agent setup prompt")
227229
expect.Contains(combined, "the user declined the request")
228230

229231
if contents, err := os.ReadFile(testConfig); err == nil {

‎internal/oauth/login_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func TestSuccessPageUsesEmbeddedBackground(t *testing.T) {
169169
assert.Contains(t, page, "Authorization<br>successful")
170170
assert.Contains(t, page, "You can close this tab.")
171171
assert.Contains(t, page, "Copy setup prompt for your agent")
172-
assert.Contains(t, page, `data-prompt="`+agentSetupPrompt+`"`)
172+
assert.Contains(t, page, `data-prompt="`+AgentSetupPrompt+`"`)
173173
assert.Contains(t, page, successBackgroundDataURL, "the success page must include the embedded photograph unchanged")
174174
assert.Contains(t, page, `alt="DigitalOcean"`)
175175
assert.Equal(t, 1, strings.Count(page, "<script>"), "the copy control needs one inline script; the callback server cannot serve a file")

‎internal/oauth/pages.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,18 +292,18 @@ type pageContent struct {
292292
Prompt string
293293
}
294294

295-
// agentSetupPrompt is what "Copy setup prompt for your agent" puts on the
295+
// AgentSetupPrompt is what "Copy setup prompt for your agent" puts on the
296296
// clipboard: a one-line instruction to fetch DigitalOcean's getting-started
297-
// guide and follow it.
298-
const agentSetupPrompt = "Fetch https://docs.digitalocean.com/get-started.md and follow it."
297+
// guide and follow it. doctl auth login also prints it in the terminal.
298+
const AgentSetupPrompt = "Fetch https://docs.digitalocean.com/get-started.md and follow it."
299299

300300
var successContent = pageContent{
301301
Outcome: "successful",
302302
Status: "You can close this tab.",
303303
Ocean: true,
304304
Background: successBackgroundSrc,
305305
Logo: digitalOceanLogoSrc,
306-
Prompt: agentSetupPrompt,
306+
Prompt: AgentSetupPrompt,
307307
AgentOpenAI: pngSrc(agentOpenAIPNG),
308308
AgentCursor: pngSrc(agentCursorPNG),
309309
AgentClaude: pngSrc(agentClaudePNG),

0 commit comments

Comments
 (0)
⌂ 换网址