Skip to content

Commit dfaa4dd

Browse files
authored
chore: migrate login to urfave/cli (#1299)
* migrate login to urfave/cli * fix merge issues
1 parent fd61054 commit dfaa4dd

2 files changed

Lines changed: 17 additions & 28 deletions

File tree

cmd/src/login.go

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ package main
22

33
import (
44
"context"
5-
"flag"
65
"fmt"
76
"io"
87
"net/url"
98
"os"
109

1110
"github.com/sourcegraph/src-cli/internal/api"
11+
"github.com/sourcegraph/src-cli/internal/clicompat"
1212
"github.com/sourcegraph/src-cli/internal/cmderrors"
1313
"github.com/sourcegraph/src-cli/internal/oauth"
14+
"github.com/urfave/cli/v3"
1415
)
1516

16-
func init() {
17-
usage := `'src login' helps you authenticate 'src' to access a Sourcegraph instance with your user credentials.
17+
const loginExamples = `'src login' helps you authenticate 'src' to access a Sourcegraph instance with your user credentials.
1818
1919
Usage:
2020
@@ -35,28 +35,21 @@ Examples:
3535
$ src login https://sourcegraph.com
3636
`
3737

38-
flagSet := flag.NewFlagSet("login", flag.ExitOnError)
39-
usageFunc := func() {
40-
fmt.Fprintln(flag.CommandLine.Output(), usage)
41-
flagSet.PrintDefaults()
42-
}
43-
44-
var (
45-
apiFlags = api.NewFlags(flagSet)
46-
)
47-
48-
handler := func(args []string) error {
49-
if err := flagSet.Parse(args); err != nil {
50-
return err
51-
}
52-
38+
var loginCommand = clicompat.Wrap(&cli.Command{
39+
Name: "login",
40+
Usage: "authenticate to a Sourcegraph instance with your user credentials",
41+
UsageText: "src login [command options] [SOURCEGRAPH_URL]",
42+
Description: loginExamples,
43+
HideVersion: true,
44+
Flags: clicompat.WithAPIFlags(),
45+
Action: func(ctx context.Context, cmd *cli.Command) error {
5346
if cfg.configFilePath != "" {
5447
fmt.Fprintln(os.Stderr)
5548
fmt.Fprintf(os.Stderr, "⚠️ Warning: Configuring src with a JSON file is deprecated. Please migrate to using the env vars SRC_ENDPOINT, SRC_ACCESS_TOKEN, and SRC_PROXY instead, and then remove %s. See https://github.com/sourcegraph/src-cli#readme for more information.\n", cfg.configFilePath)
5649
}
5750

58-
if flagSet.NArg() >= 1 {
59-
arg := flagSet.Arg(0)
51+
if cmd.Args().Present() {
52+
arg := cmd.Args().First()
6053
loginEndpointURL, err := parseEndpoint(arg)
6154
if err != nil {
6255
return cmderrors.Usage(fmt.Sprintf("invalid endpoint URL: %s", arg))
@@ -79,6 +72,7 @@ Examples:
7972
cfg.endpointURL = loginEndpointURL
8073
}
8174

75+
apiFlags := clicompat.APIFlagsFromCmd(cmd)
8276
client := cfg.apiClient(apiFlags, io.Discard)
8377

8478
return loginCmd(context.Background(), loginParams{
@@ -88,14 +82,8 @@ Examples:
8882
apiFlags: apiFlags,
8983
oauthClient: oauth.NewClient(oauth.DefaultClientID),
9084
})
91-
}
92-
93-
commands = append(commands, &command{
94-
flagSet: flagSet,
95-
handler: handler,
96-
usageFunc: usageFunc,
97-
})
98-
}
85+
},
86+
})
9987

10088
type loginParams struct {
10189
cfg *config

cmd/src/run_migration_compat.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
var migratedCommands = map[string]*cli.Command{
1919
"abc": abcCommand,
2020
"auth": authCommand,
21+
"login": loginCommand,
2122
"version": versionCommand,
2223
}
2324

0 commit comments

Comments
 (0)