Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add crane auth logout #1589

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.