-
Notifications
You must be signed in to change notification settings - Fork 23
/
login.go
51 lines (42 loc) · 1.54 KB
/
login.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cmd
import (
"fmt"
"github.com/pkg/errors"
"github.com/replicatedhq/replicated/pkg/credentials"
"github.com/spf13/cobra"
)
func (r *runners) InitLoginCommand(parent *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "login",
Short: "Log in to Replicated",
Long: `This command will open your browser to ask you authentication details and create / retrieve an API token for the CLI to use.`,
SilenceUsage: true,
RunE: r.login,
}
parent.AddCommand(cmd)
cmd.Flags().StringVar(&r.args.loginEndpoint, "endpoint", "https://vendor.replicated.com", "The endpoint to use for the login process. Defaults to https://vendor.replicated.com")
cmd.Flags().MarkHidden("endpoint")
return cmd
}
func (r *runners) login(_ *cobra.Command, _ []string) error {
endpoint := r.args.loginEndpoint
// if we are already logged in, just return
currentCredentials, err := credentials.GetCurrentCredentials()
if err == nil {
if currentCredentials.IsEnv {
return errors.New("REPLICATED_API_TOKEN is set. Please unset it to login with a different account.")
}
return errors.New("There are already credentials on this machine. Please run `replicated logout` to remove them.")
}
if err != credentials.ErrCredentialsNotFound {
return err
}
// open a browser to the login page
if err := credentials.Fetch(endpoint); err != nil {
if err == credentials.ErrNoBrowser {
return fmt.Errorf("No browser could be found. Please visit %s to login and create an API token.", endpoint)
}
return err
}
return nil
}