@@ -2,12 +2,13 @@ package main
22
33import (
44 "context"
5- "flag"
65 "fmt"
76
87 "github.com/sourcegraph/sourcegraph/lib/errors"
98
9+ "github.com/sourcegraph/src-cli/internal/clicompat"
1010 "github.com/sourcegraph/src-cli/internal/oauth"
11+ "github.com/urfave/cli/v3"
1112)
1213
1314var (
@@ -21,37 +22,52 @@ type oauthTokenRefresher interface {
2122 GetToken (ctx context.Context ) (oauth.Token , error )
2223}
2324
24- func init () {
25- flagSet := flag .NewFlagSet ("token" , flag .ExitOnError )
26- header := flagSet .Bool ("header" , false , "print the token as an Authorization header" )
27- usageFunc := func () {
28- fmt .Fprintf (flag .CommandLine .Output (), "Usage of 'src auth token':\n \n " )
29- fmt .Fprintf (flag .CommandLine .Output (), "Print the current authentication token.\n " )
30- fmt .Fprintf (flag .CommandLine .Output (), "Use --header to print a complete Authorization header instead.\n \n " )
31- flagSet .PrintDefaults ()
32- }
25+ const authTokenExamples = `
26+ Print the current authentication token.
3327
34- handler := func (args []string ) error {
35- if err := flagSet .Parse (args ); err != nil {
36- return err
37- }
28+ Use --header to print a complete Authorization header instead.
29+
30+ Examples:
31+
32+ Raw token output:
33+
34+ $ src auth token
35+ sgp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
36+
37+ Authorization header output:
3838
39- token , err := resolveAuthToken (context .Background (), cfg )
39+ $ src auth token --header
40+ Authorization: token sgp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
41+
42+ If you are authenticated with OAuth instead of SRC_ACCESS_TOKEN, the header uses the Bearer scheme:
43+
44+ $ src auth token --header
45+ Authorization: Bearer eyJhbGciOi...
46+ `
47+
48+ var authTokenCommand = clicompat .Wrap (& cli.Command {
49+ Name : "token" ,
50+ Usage : "prints the current authentication token or Authorization header" ,
51+ UsageText : "src auth token [options]" ,
52+ Description : authTokenExamples ,
53+ HideVersion : true ,
54+ Flags : []cli.Flag {
55+ & cli.BoolFlag {
56+ Name : "header" ,
57+ Usage : "print the token as an Authorization header" ,
58+ },
59+ },
60+ Action : func (ctx context.Context , cmd * cli.Command ) error {
61+ token , err := resolveAuthToken (ctx , cfg )
4062 if err != nil {
4163 return err
4264 }
4365
44- token = formatAuthTokenOutput (token , cfg .AuthMode (), * header )
45- fmt .Println (token )
46- return nil
47- }
48-
49- authCommands = append (authCommands , & command {
50- flagSet : flagSet ,
51- handler : handler ,
52- usageFunc : usageFunc ,
53- })
54- }
66+ token = formatAuthTokenOutput (token , cfg .AuthMode (), cmd .Bool ("header" ))
67+ _ , err = fmt .Fprintln (cmd .Writer , token )
68+ return err
69+ },
70+ })
5571
5672func resolveAuthToken (ctx context.Context , cfg * config ) (string , error ) {
5773 if err := cfg .requireCIAccessToken (); err != nil {
0 commit comments