Skip to content

Commit a936529

Browse files
committed
TUN-8591 login command without extra text
Also unifies `access token` and `access login` interface
1 parent 30c435f commit a936529

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

cmd/cloudflared/access/cmd.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
)
2727

2828
const (
29+
appURLFlag = "app"
2930
loginQuietFlag = "quiet"
3031
sshHostnameFlag = "hostname"
3132
sshDestinationFlag = "destination"
@@ -83,9 +84,10 @@ func Commands() []*cli.Command {
8384
applications from the command line.`,
8485
Subcommands: []*cli.Command{
8586
{
86-
Name: "login",
87-
Action: cliutil.Action(login),
88-
Usage: "login <url of access application>",
87+
Name: "login",
88+
Action: cliutil.Action(login),
89+
Usage: "login <url of access application>",
90+
ArgsUsage: "url of Access application",
8991
Description: `The login subcommand initiates an authentication flow with your identity provider.
9092
The subcommand will launch a browser. For headless systems, a url is provided.
9193
Once authenticated with your identity provider, the login command will generate a JSON Web Token (JWT)
@@ -97,6 +99,13 @@ func Commands() []*cli.Command {
9799
Aliases: []string{"q"},
98100
Usage: "do not print the jwt to the command line",
99101
},
102+
&cli.BoolFlag{
103+
Name: "no-verbose",
104+
Usage: "print only the jwt to stdout",
105+
},
106+
&cli.StringFlag{
107+
Name: appURLFlag,
108+
},
100109
},
101110
},
102111
{
@@ -111,12 +120,12 @@ func Commands() []*cli.Command {
111120
{
112121
Name: "token",
113122
Action: cliutil.Action(generateToken),
114-
Usage: "token -app=<url of access application>",
123+
Usage: "token <url of access application>",
115124
ArgsUsage: "url of Access application",
116125
Description: `The token subcommand produces a JWT which can be used to authenticate requests.`,
117126
Flags: []cli.Flag{
118127
&cli.StringFlag{
119-
Name: "app",
128+
Name: appURLFlag,
120129
},
121130
},
122131
},
@@ -232,9 +241,8 @@ func login(c *cli.Context) error {
232241

233242
log := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog)
234243

235-
args := c.Args()
236-
appURL, err := parseURL(args.First())
237-
if args.Len() < 1 || err != nil {
244+
appURL, err := getAppURLFromArgs(c)
245+
if err != nil {
238246
log.Error().Msg("Please provide the url of the Access application")
239247
return err
240248
}
@@ -261,7 +269,14 @@ func login(c *cli.Context) error {
261269
if c.Bool(loginQuietFlag) {
262270
return nil
263271
}
264-
fmt.Fprintf(os.Stdout, "Successfully fetched your token:\n\n%s\n\n", cfdToken)
272+
273+
// Chatty by default for backward compat. The new --app flag
274+
// is an implicit opt-out of the backwards-compatible chatty output.
275+
if c.Bool("no-verbose") || c.IsSet(appURLFlag) {
276+
fmt.Fprint(os.Stdout, cfdToken)
277+
} else {
278+
fmt.Fprintf(os.Stdout, "Successfully fetched your token:\n\n%s\n\n", cfdToken)
279+
}
265280

266281
return nil
267282
}
@@ -340,6 +355,17 @@ func run(cmd string, args ...string) error {
340355
return c.Run()
341356
}
342357

358+
func getAppURLFromArgs(c *cli.Context) (*url.URL, error) {
359+
var appURLStr string
360+
args := c.Args()
361+
if args.Len() < 1 {
362+
appURLStr = c.String(appURLFlag)
363+
} else {
364+
appURLStr = args.First()
365+
}
366+
return parseURL(appURLStr)
367+
}
368+
343369
// token dumps provided token to stdout
344370
func generateToken(c *cli.Context) error {
345371
err := sentry.Init(sentry.ClientOptions{
@@ -349,8 +375,8 @@ func generateToken(c *cli.Context) error {
349375
if err != nil {
350376
return err
351377
}
352-
appURL, err := parseURL(c.String("app"))
353-
if err != nil || c.NumFlags() < 1 {
378+
appURL, err := getAppURLFromArgs(c)
379+
if err != nil {
354380
fmt.Fprintln(os.Stderr, "Please provide a url.")
355381
return err
356382
}

0 commit comments

Comments
 (0)