Skip to content

Commit b071e59

Browse files
authored
migrate src auth to urfave/cli (#1298)
1 parent 462bb96 commit b071e59

4 files changed

Lines changed: 96 additions & 51 deletions

File tree

cmd/src/auth.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
11
package main
22

33
import (
4-
"flag"
5-
"fmt"
6-
)
4+
"context"
75

8-
var authCommands commander
6+
"github.com/sourcegraph/src-cli/internal/clicompat"
7+
"github.com/urfave/cli/v3"
8+
)
99

10-
func init() {
11-
usage := `'src auth' provides authentication-related helper commands.
10+
const authExamples = `
11+
Authentication-related helper commands.
1212
13-
Usage:
13+
Examples:
1414
15-
src auth command [command options]
15+
Print the active auth token:
1616
17-
The commands are:
17+
$ src auth token
18+
sgp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1819
19-
token prints the current authentication token or Authorization header
20+
Print the current Authorization header:
2021
21-
Use "src auth [command] -h" for more information about a command.
22+
$ src auth token --header
23+
Authorization: token sgp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2224
`
2325

24-
flagSet := flag.NewFlagSet("auth", flag.ExitOnError)
25-
handler := func(args []string) error {
26-
authCommands.run(flagSet, "src auth", usage, args)
27-
return nil
28-
}
29-
30-
commands = append(commands, &command{
31-
flagSet: flagSet,
32-
handler: handler,
33-
usageFunc: func() {
34-
fmt.Println(usage)
35-
},
36-
})
37-
}
26+
var authCommand = clicompat.Wrap(&cli.Command{
27+
Name: "auth",
28+
Usage: "authentication helper commands",
29+
UsageText: "src auth [command options]",
30+
Description: authExamples,
31+
HideVersion: true,
32+
Commands: []*cli.Command{
33+
authTokenCommand,
34+
},
35+
Action: func(ctx context.Context, cmd *cli.Command) error {
36+
return cli.ShowSubcommandHelp(cmd)
37+
},
38+
})

cmd/src/auth_token.go

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package main
22

33
import (
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

1314
var (
@@ -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

5672
func resolveAuthToken(ctx context.Context, cfg *config) (string, error) {
5773
if err := cfg.requireCIAccessToken(); err != nil {

cmd/src/auth_token_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bytes"
45
"context"
56
"fmt"
67
"net/url"
@@ -125,6 +126,32 @@ func TestResolveAuthToken(t *testing.T) {
125126
})
126127
}
127128

129+
func TestAuthTokenCommand(t *testing.T) {
130+
reset := stubAuthTokenDependencies(t)
131+
defer reset()
132+
133+
prevCfg := cfg
134+
defer func() { cfg = prevCfg }()
135+
136+
cfg = &config{
137+
accessToken: "access-token",
138+
endpointURL: mustParseURL(t, "https://example.com"),
139+
}
140+
141+
var out bytes.Buffer
142+
cmd := *authTokenCommand
143+
cmd.Writer = &out
144+
cmd.ErrWriter = &out
145+
146+
err := cmd.Run(context.Background(), []string{"token", "--header"})
147+
if err != nil {
148+
t.Fatal(err)
149+
}
150+
if out.String() != "Authorization: token access-token\n" {
151+
t.Fatalf("output = %q, want %q", out.String(), "Authorization: token access-token\n")
152+
}
153+
}
154+
128155
func TestFormatAuthTokenOutput(t *testing.T) {
129156
tests := []struct {
130157
name string

cmd/src/run_migration_compat.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
var migratedCommands = map[string]*cli.Command{
1919
"abc": abcCommand,
20+
"auth": authCommand,
2021
"version": versionCommand,
2122
}
2223

0 commit comments

Comments
 (0)