forked from AliyunContainerService/pouch
/
logout.go
72 lines (59 loc) · 1.65 KB
/
logout.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
package main
import (
"context"
"fmt"
"os"
"github.com/alibaba/pouch/credential"
"github.com/spf13/cobra"
)
// logoutDescription is used to describe logout command and auto generate command doc.
var logoutDescription = "\nlogout from a v1/v2 registry."
// LogoutCommand use to implement 'logout' command.
type LogoutCommand struct {
baseCommand
}
// Init initialize logout command.
func (l *LogoutCommand) Init(c *Cli) {
l.cli = c
l.cmd = &cobra.Command{
Use: "logout [SERVER]",
Short: "Logout from a registry",
Long: logoutDescription,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return l.runLogout(args)
},
Example: logoutExample(),
}
}
// runLogout is the entry of logout command.
func (l *LogoutCommand) runLogout(args []string) error {
var registry string
if len(args) > 0 {
registry = args[0]
}
if registry == "" {
ctx := context.Background()
info, err := l.cli.Client().SystemInfo(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Fail to get default registry: %s\n", err)
return err
}
registry = info.DefaultRegistry
}
if !credential.Exist(registry) {
fmt.Fprintf(os.Stdout, "Has not logged in registry: %s\n", registry)
return nil
}
if err := credential.Delete(registry); err != nil {
fmt.Fprintf(os.Stderr, "Fail to remove login credential: %s\n", err)
return err
}
fmt.Fprintf(os.Stdout, "Remove login credential for registry: %s\n", registry)
return nil
}
// logoutExample shows examples in logout command, and is used in auto-generated cli docs.
func logoutExample() string {
return `$ pouch logout $registry
Remove login credential for registry: $registry`
}