Skip to content

Commit 7255d89

Browse files
committed
migrate src auth to urfave/cli
1 parent 462bb96 commit 7255d89

4 files changed

Lines changed: 82 additions & 56 deletions

File tree

cmd/src/auth.go

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

33
import (
4-
"flag"
5-
"fmt"
6-
)
7-
8-
var authCommands commander
9-
10-
func init() {
11-
usage := `'src auth' provides authentication-related helper commands.
4+
"context"
125

13-
Usage:
14-
15-
src auth command [command options]
6+
"github.com/sourcegraph/src-cli/internal/clicompat"
7+
"github.com/urfave/cli/v3"
8+
)
169

17-
The commands are:
10+
const authExamples = `
11+
Authentication-related helper commands.
1812
19-
token prints the current authentication token or Authorization header
13+
Examples:
2014
21-
Use "src auth [command] -h" for more information about a command.
15+
$ src auth token
16+
$ src auth token --header
2217
`
2318

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-
}
19+
var authCommand = clicompat.Wrap(&cli.Command{
20+
Name: "auth",
21+
Usage: "authentication helper commands",
22+
UsageText: "src auth [command options]",
23+
Description: authExamples,
24+
HideVersion: true,
25+
Commands: []*cli.Command{
26+
authTokenCommand,
27+
},
28+
Action: func(ctx context.Context, cmd *cli.Command) error {
29+
return cli.ShowSubcommandHelp(cmd)
30+
},
31+
})

cmd/src/auth_token.go

Lines changed: 32 additions & 28 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,40 @@ 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-
}
33-
34-
handler := func(args []string) error {
35-
if err := flagSet.Parse(args); err != nil {
36-
return err
37-
}
38-
39-
token, err := resolveAuthToken(context.Background(), cfg)
25+
const authTokenExamples = `
26+
Print the current authentication token.
27+
28+
Use --header to print a complete Authorization header instead.
29+
30+
Examples:
31+
32+
$ src auth token
33+
$ src auth token --header
34+
`
35+
36+
var authTokenCommand = clicompat.Wrap(&cli.Command{
37+
Name: "token",
38+
Usage: "prints the current authentication token or Authorization header",
39+
UsageText: "src auth token [options]",
40+
Description: authTokenExamples,
41+
HideVersion: true,
42+
Flags: []cli.Flag{
43+
&cli.BoolFlag{
44+
Name: "header",
45+
Usage: "print the token as an Authorization header",
46+
},
47+
},
48+
Action: func(ctx context.Context, cmd *cli.Command) error {
49+
token, err := resolveAuthToken(ctx, cfg)
4050
if err != nil {
4151
return err
4252
}
4353

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-
}
54+
token = formatAuthTokenOutput(token, cfg.AuthMode(), cmd.Bool("header"))
55+
_, err = fmt.Fprintln(cmd.Writer, token)
56+
return err
57+
},
58+
})
5559

5660
func resolveAuthToken(ctx context.Context, cfg *config) (string, error) {
5761
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)