-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathlogin.go
83 lines (70 loc) · 2.17 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
package login
import (
"encoding/base64"
"fmt"
"log"
"net/http"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
clientutils "github.com/spacecloud-io/space-cloud/utils/client"
)
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "login",
PreRun: func(cmd *cobra.Command, args []string) {
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
_ = viper.BindPFlag("url", cmd.Flags().Lookup("url"))
_ = viper.BindPFlag("username", cmd.Flags().Lookup("username"))
_ = viper.BindPFlag("password", cmd.Flags().Lookup("password"))
},
RunE: func(cmd *cobra.Command, args []string) error {
baseUrl := viper.GetString("url")
username := viper.GetString("username")
password := viper.GetString("password")
if !cmd.Flags().Changed("url") {
prompt := &survey.Input{
Message: "URL of SpaceCloud?",
Default: "http://localhost:4122",
}
survey.AskOne(prompt, &baseUrl)
}
if !cmd.Flags().Changed("username") {
prompt := &survey.Input{
Message: "Username?",
Default: "admin",
}
survey.AskOne(prompt, &username)
}
if !cmd.Flags().Changed("password") {
prompt := &survey.Input{
Message: "Password?",
Default: "admin",
}
survey.AskOne(prompt, &password)
}
httpClient := &http.Client{}
creds := clientutils.Credentials{
Username: base64.StdEncoding.EncodeToString([]byte(username)),
Password: base64.StdEncoding.EncodeToString([]byte(password)),
BaseUrl: baseUrl,
}
_, err := clientutils.Login(httpClient, creds)
if err != nil {
log.Fatal("Failed to authenticate with SpaceCloud", err)
}
location, err := clientutils.UpdateSpaceCloudCredsFile(creds)
if err != nil {
log.Fatal("Could not create creds.json file: ", err)
}
fmt.Printf("Successfully log into SpaceCloud. Credentials saved at %s\n", location)
return nil
},
}
cmd.Flags().StringP("url", "", "", "Base URL where SpaceCloud is running")
cmd.Flags().StringP("username", "", "", "Username to log into SpaceCloud")
cmd.Flags().StringP("password", "", "", "Password to log into SpaceCloud")
return cmd
}