This repository has been archived by the owner on Apr 7, 2021. It is now read-only.
/
userLogin.go
65 lines (55 loc) · 1.6 KB
/
userLogin.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
package cmd
import (
"context"
"time"
api "github.com/32leaves/ruruku/pkg/api/v1"
"github.com/32leaves/ruruku/pkg/cli"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// userAuthCmd represents the sessionClose command
var userAuthCmd = &cobra.Command{
Use: "login <username>",
Short: "Authenticates a user against an API server",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cfg, err := GetConfigFromViper()
if err != nil {
log.Fatalf("Error while loading the configuration: %v", err)
}
conn, err := cfg.Connect()
if err != nil {
log.Fatalf("fail to dial: %v", err)
}
defer conn.Close()
client := api.NewUserServiceClient(conn)
password, err := cli.GetPassword(cmd)
if err != nil {
log.WithError(err).Fatal()
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(cfg.CLI.Timeout)*time.Second)
defer cancel()
resp, err := client.AuthenticateCredentials(ctx, &api.AuthenticationRequest{Username: args[0], Password: password})
if err != nil {
log.WithError(err).Fatal()
}
tpl := `export RURUKU_TOKEN={{ .Token }}
export RURUKU_USER={{ .User }}
`
result := struct {
Token string
User string
}{
Token: resp.Token,
User: args[0],
}
ctnt := remoteCmdValues.GetOutputFormat(result, tpl, `{.token}`)
if err := ctnt.Print(); err != nil {
log.WithError(err).Fatal()
}
},
}
func init() {
userCmd.AddCommand(userAuthCmd)
userAuthCmd.Flags().String("password", "", "The user's password. Passwords on the command line are unsafe: use the interactive mode or RURUKU_PASSWORD env var instead")
}