diff --git a/cmd/src/login.go b/cmd/src/login.go index 889994af51..b5e1862b5f 100644 --- a/cmd/src/login.go +++ b/cmd/src/login.go @@ -2,19 +2,19 @@ package main import ( "context" - "flag" "fmt" "io" "net/url" "os" "github.com/sourcegraph/src-cli/internal/api" + "github.com/sourcegraph/src-cli/internal/clicompat" "github.com/sourcegraph/src-cli/internal/cmderrors" "github.com/sourcegraph/src-cli/internal/oauth" + "github.com/urfave/cli/v3" ) -func init() { - usage := `'src login' helps you authenticate 'src' to access a Sourcegraph instance with your user credentials. +const loginExamples = `'src login' helps you authenticate 'src' to access a Sourcegraph instance with your user credentials. Usage: @@ -35,28 +35,21 @@ Examples: $ src login https://sourcegraph.com ` - flagSet := flag.NewFlagSet("login", flag.ExitOnError) - usageFunc := func() { - fmt.Fprintln(flag.CommandLine.Output(), usage) - flagSet.PrintDefaults() - } - - var ( - apiFlags = api.NewFlags(flagSet) - ) - - handler := func(args []string) error { - if err := flagSet.Parse(args); err != nil { - return err - } - +var loginCommand = clicompat.Wrap(&cli.Command{ + Name: "login", + Usage: "authenticate to a Sourcegraph instance with your user credentials", + UsageText: "src login [command options] [SOURCEGRAPH_URL]", + Description: loginExamples, + HideVersion: true, + Flags: clicompat.WithAPIFlags(), + Action: func(ctx context.Context, cmd *cli.Command) error { if cfg.configFilePath != "" { fmt.Fprintln(os.Stderr) 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) } - if flagSet.NArg() >= 1 { - arg := flagSet.Arg(0) + if cmd.Args().Present() { + arg := cmd.Args().First() loginEndpointURL, err := parseEndpoint(arg) if err != nil { return cmderrors.Usage(fmt.Sprintf("invalid endpoint URL: %s", arg)) @@ -79,6 +72,7 @@ Examples: cfg.endpointURL = loginEndpointURL } + apiFlags := clicompat.APIFlagsFromCmd(cmd) client := cfg.apiClient(apiFlags, io.Discard) return loginCmd(context.Background(), loginParams{ @@ -88,14 +82,8 @@ Examples: apiFlags: apiFlags, oauthClient: oauth.NewClient(oauth.DefaultClientID), }) - } - - commands = append(commands, &command{ - flagSet: flagSet, - handler: handler, - usageFunc: usageFunc, - }) -} + }, +}) type loginParams struct { cfg *config diff --git a/cmd/src/run_migration_compat.go b/cmd/src/run_migration_compat.go index fde21bd5ee..eada89896c 100644 --- a/cmd/src/run_migration_compat.go +++ b/cmd/src/run_migration_compat.go @@ -18,6 +18,7 @@ import ( var migratedCommands = map[string]*cli.Command{ "abc": abcCommand, "auth": authCommand, + "login": loginCommand, "version": versionCommand, }