Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 16 additions & 28 deletions cmd/src/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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))
Expand All @@ -79,6 +72,7 @@ Examples:
cfg.endpointURL = loginEndpointURL
}

apiFlags := clicompat.APIFlagsFromCmd(cmd)
client := cfg.apiClient(apiFlags, io.Discard)

return loginCmd(context.Background(), loginParams{
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/src/run_migration_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
var migratedCommands = map[string]*cli.Command{
"abc": abcCommand,
"auth": authCommand,
"login": loginCommand,
"version": versionCommand,
}

Expand Down
Loading