forked from genuinetools/img
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogout.go
64 lines (50 loc) · 1.92 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
package main
import (
"context"
"flag"
"fmt"
"os"
"github.com/docker/cli/cli/config"
"github.com/docker/docker/registry"
)
const logoutShortHelp = `Log out from a Docker registry.`
var logoutLongHelp = logoutShortHelp + fmt.Sprintf("\n\nIf no server is specified, the default (%s) is used.", defaultDockerRegistry)
func (cmd *logoutCommand) Name() string { return "logout" }
func (cmd *logoutCommand) Args() string { return "[SERVER]" }
func (cmd *logoutCommand) ShortHelp() string { return logoutShortHelp }
func (cmd *logoutCommand) LongHelp() string { return logoutLongHelp }
func (cmd *logoutCommand) Hidden() bool { return false }
func (cmd *logoutCommand) DoReexec() bool { return false }
func (cmd *logoutCommand) RequiresRunc() bool { return false }
func (cmd *logoutCommand) Register(fs *flag.FlagSet) {
}
type logoutCommand struct {
serverAddress string
}
func (cmd *logoutCommand) Run(ctx context.Context, args []string) error {
if len(args) > 0 {
cmd.serverAddress = args[0]
}
// Set the default registry server address.
if cmd.serverAddress == "" {
cmd.serverAddress = defaultDockerRegistry
} else {
cmd.serverAddress = registry.ConvertToHostname(cmd.serverAddress)
}
dcfg, err := config.Load(config.Dir())
if err != nil {
return fmt.Errorf("loading config file failed: %v", err)
}
// check if we're logged in based on the records in the config file
// which means it couldn't have user/pass cause they may be in the creds store
if _, loggedIn := dcfg.AuthConfigs[cmd.serverAddress]; !loggedIn {
fmt.Fprintf(os.Stdout, "Not logged in to %s\n", cmd.serverAddress)
return nil
}
fmt.Fprintf(os.Stdout, "Removing login credentials for %s\n", cmd.serverAddress)
if err := dcfg.GetCredentialsStore(cmd.serverAddress).Erase(cmd.serverAddress); err != nil {
fmt.Fprintf(os.Stdout, "WARNING: could not erase credentials: %v\n", err)
}
fmt.Println("Logout succeeded.")
return nil
}