forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
120 lines (97 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"context"
"fmt"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/credential"
"github.com/alibaba/pouch/pkg/term"
"github.com/spf13/cobra"
)
// loginDescription is used to describe login command and auto generate command doc.
var loginDescription = "\nlogin to a v1/v2 registry with the provided credentials."
// LoginCommand use to implement 'login' command.
type LoginCommand struct {
baseCommand
username string
password string
}
// Init initialize login command.
func (l *LoginCommand) Init(c *Cli) {
l.cli = c
l.cmd = &cobra.Command{
Use: "login [OPTIONS] [SERVER]",
Short: "Login to a registry",
Long: loginDescription,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return l.runLogin(args)
},
Example: loginExample(),
}
l.addFlags()
}
// addFlags adds flags for specific command.
func (l *LoginCommand) addFlags() {
flagSet := l.cmd.Flags()
flagSet.StringVarP(&l.username, "username", "u", "", "username for registry")
flagSet.StringVarP(&l.password, "password", "p", "", "password for registry")
}
// runLogin is the entry of login command.
func (l *LoginCommand) runLogin(args []string) error {
auth := configureAuth(l.username, l.password)
if len(args) > 0 {
auth.ServerAddress = args[0]
}
ctx := context.Background()
apiClient := l.cli.Client()
// error will be ignored here, cause registry address can be null.
if auth.ServerAddress == "" {
if info, err := apiClient.SystemInfo(ctx); err == nil {
auth.ServerAddress = info.DefaultRegistry
}
}
resp, err := apiClient.RegistryLogin(ctx, auth)
if err != nil {
return err
}
if resp.Status != "" {
fmt.Printf("%s\n", resp.Status)
}
return credential.Save(auth)
}
// configureAuth ensures that username and password is given.
func configureAuth(username, password string) *types.AuthConfig {
if username == "" {
username = readInput("Username", true)
}
if password == "" {
password = readInput("Password", false)
}
return &types.AuthConfig{
Username: username,
Password: password,
}
}
// readInput reads from stdin.
func readInput(prompt string, echo bool) (read string) {
fmt.Printf("%s: ", prompt)
if echo {
fmt.Scanf("%s", &read)
} else {
disableEcho(echo, &read)
}
return
}
// disableEcho disables echo when read from input.
func disableEcho(echo bool, read *string) {
term.StdinEcho(false)
fmt.Scanf("%s", read)
fmt.Printf("\n")
// restore stdin terminal
term.StdinEcho(true)
}
// loginExample shows examples in login command, and is used in auto-generated cli docs.
func loginExample() string {
return `$ pouch login -u $username -p $password
Login Succeeded`
}