-
Notifications
You must be signed in to change notification settings - Fork 8
/
login.go
105 lines (83 loc) · 2.65 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cmd
import (
"context"
"fmt"
"github.com/morikuni/aec"
"github.com/pkg/browser"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/console/tui"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/gitpod"
)
func NewLoginCmd() *cobra.Command {
var (
kind string
)
cmd := &cobra.Command{
Use: "login",
Short: "Login to use Namespace services (DNS and SSL management, etc).",
Args: cobra.NoArgs,
RunE: fncobra.RunE(func(ctx context.Context, args []string) error {
res, err := fnapi.StartLogin(ctx, kind)
if err != nil {
return nil
}
stdout := console.Stdout(ctx)
fmt.Fprintf(stdout, "%s\n", aec.Bold.Apply("Login to Namespace"))
if openURL(res.LoginUrl) {
fmt.Fprintf(stdout, "Please complete the login flow in your browser.\n\n %s\n", res.LoginUrl)
} else {
fmt.Fprintf(stdout, "In order to login, open the following URL in your browser:\n\n %s\n", res.LoginUrl)
}
auth, err := fnapi.CompleteLogin(ctx, res.LoginId, res.Kind, fnapi.TelemetryOn(ctx).GetClientID())
if err != nil {
return err
}
username, err := fnapi.StoreUser(ctx, auth)
if err != nil {
return err
}
fmt.Fprintf(stdout, "\nHi %s, you are now logged in, have a nice day.\n", username)
return nil
}),
}
cmd.Flags().StringVar(&kind, "kind", "clerk", "Internal kind.")
_ = cmd.Flags().MarkHidden("kind")
cmd.AddCommand(NewRobotLogin("robot"))
return cmd
}
func openURL(url string) bool {
if gitpod.IsGitpod() {
// This is to avoid using www-browser (lynx) in gitpods.
// TODO is there a way to open a browser here?
return false
}
err := browser.OpenURL(url)
return err == nil
}
func NewRobotLogin(use string) *cobra.Command {
robotLogin := &cobra.Command{
Use: use,
Short: "Login as a robot.",
Args: cobra.ExactArgs(1),
Hidden: true,
RunE: fncobra.RunE(func(ctx context.Context, args []string) error {
accessToken, err := tui.AskSecret(ctx, "Which Access Token would you like to use today?", "That would be a Github access token.", "access token")
if err != nil {
return err
}
username, err := fnapi.LoginAsRobotAndStore(ctx, args[0], string(accessToken))
if err != nil {
return err
}
fmt.Fprintf(console.Stdout(ctx), "\nHi %s, you are now logged in, have a nice day.\n", username)
return nil
}),
}
return robotLogin
}