Skip to content

Commit

Permalink
add crane auth logout (#1589)
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh committed Apr 20, 2023
1 parent 52d59d1 commit df518f9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
41 changes: 40 additions & 1 deletion cmd/crane/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewCmdAuth(options []crane.Option, argv ...string) *cobra.Command {
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error { return cmd.Usage() },
}
cmd.AddCommand(NewCmdAuthGet(options, argv...), NewCmdAuthLogin(argv...))
cmd.AddCommand(NewCmdAuthGet(options, argv...), NewCmdAuthLogin(argv...), NewCmdAuthLogout(argv...))
return cmd
}

Expand Down Expand Up @@ -203,3 +203,42 @@ func login(opts loginOptions) error {
log.Printf("logged in via %s", cf.Filename)
return nil
}

// NewCmdAuthLogout creates a new `crane auth logout` command.
func NewCmdAuthLogout(argv ...string) *cobra.Command {
eg := fmt.Sprintf(` # Log out of reg.example.com
%s logout reg.example.com`, strings.Join(argv, " "))

cmd := &cobra.Command{
Use: "logout [SERVER]",
Short: "Log out of a registry",
Example: eg,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
reg, err := name.NewRegistry(args[0])
if err != nil {
return err
}
serverAddress := reg.Name()

cf, err := config.Load(os.Getenv("DOCKER_CONFIG"))
if err != nil {
return err
}
creds := cf.GetCredentialsStore(serverAddress)
if serverAddress == name.DefaultRegistry {
serverAddress = authn.DefaultAuthKey
}
if err := creds.Erase(serverAddress); err != nil {
return err
}

if err := cf.Save(); err != nil {
return err
}
log.Printf("logged out via %s", cf.Filename)
return nil
},
}
return cmd
}
1 change: 1 addition & 0 deletions cmd/crane/doc/crane_auth.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions cmd/crane/doc/crane_auth_logout.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit df518f9

Please sign in to comment.