mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-09-20 01:04:15 +00:00
94 lines
1.9 KiB
Go
94 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"sort"
|
|
|
|
"github.com/charmbracelet/huh"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func newLogoutCmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "logout [registry]",
|
|
Short: "Remove account credentials",
|
|
Long: "Remove stored credentials for an account.\nDefault registry: atcr.io",
|
|
Args: cobra.MaximumNArgs(1),
|
|
RunE: runLogout,
|
|
}
|
|
}
|
|
|
|
func runLogout(cmd *cobra.Command, args []string) error {
|
|
serverURL := "atcr.io"
|
|
if len(args) > 0 {
|
|
serverURL = args[0]
|
|
}
|
|
|
|
appViewURL := buildAppViewURL(serverURL)
|
|
|
|
cfg, err := loadConfig()
|
|
if err != nil {
|
|
return fmt.Errorf("loading config: %w", err)
|
|
}
|
|
|
|
reg := cfg.findRegistry(appViewURL)
|
|
if reg == nil || len(reg.Accounts) == 0 {
|
|
fmt.Fprintf(os.Stderr, "No accounts configured for %s.\n", serverURL)
|
|
return nil
|
|
}
|
|
|
|
// Determine which account to remove
|
|
var handle string
|
|
|
|
if len(reg.Accounts) == 1 {
|
|
for h := range reg.Accounts {
|
|
handle = h
|
|
}
|
|
} else {
|
|
// Multiple accounts — select which to remove
|
|
var handles []string
|
|
for h := range reg.Accounts {
|
|
handles = append(handles, h)
|
|
}
|
|
sort.Strings(handles)
|
|
|
|
var options []huh.Option[string]
|
|
for _, h := range handles {
|
|
label := h
|
|
if h == reg.Active {
|
|
label += " (active)"
|
|
}
|
|
options = append(options, huh.NewOption(label, h))
|
|
}
|
|
|
|
err := huh.NewSelect[string]().
|
|
Title("Which account to remove?").
|
|
Options(options...).
|
|
Value(&handle).
|
|
Run()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Confirm
|
|
var confirm bool
|
|
err = huh.NewConfirm().
|
|
Title(fmt.Sprintf("Remove %s from %s?", handle, serverURL)).
|
|
Value(&confirm).
|
|
Run()
|
|
if err != nil || !confirm {
|
|
fmt.Fprintf(os.Stderr, "Cancelled.\n")
|
|
return nil
|
|
}
|
|
|
|
cfg.removeAccount(appViewURL, handle)
|
|
if err := cfg.save(); err != nil {
|
|
return fmt.Errorf("saving config: %w", err)
|
|
}
|
|
|
|
fmt.Printf("Removed %s from %s\n", handle, serverURL)
|
|
return nil
|
|
}
|