mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-08-28 20:06:02 +00:00
102 lines
3.4 KiB
Go
102 lines
3.4 KiB
Go
// Package credhelper implements the Docker credential helper for ATProto-backed
|
|
// container registries. It is consumed by per-brand main packages (e.g.
|
|
// cmd/credential-helper/atcr, cmd/credential-helper/seamark) that supply a
|
|
// Config and call Run.
|
|
package credhelper
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// Config holds the per-brand identity for one built binary.
|
|
type Config struct {
|
|
// BinaryName is the program name (e.g. "docker-credential-atcr"). Used in
|
|
// the root cobra Use line, help text, and the User-Agent.
|
|
BinaryName string
|
|
|
|
// DefaultRegistry is the default registry hostname used when login/logout/
|
|
// switch is invoked without a positional argument (e.g. "atcr.io").
|
|
DefaultRegistry string
|
|
|
|
// ConfigDirName is the directory under $HOME where stored credentials live
|
|
// (e.g. ".atcr"). Created with 0700 perms on first use.
|
|
ConfigDirName string
|
|
|
|
// SecretPrefix is the magic prefix the helper uses to recognise its own
|
|
// device-issued secrets when Docker calls `store` (e.g. "atcr_device_").
|
|
// Secrets not carrying this prefix are silently ignored.
|
|
SecretPrefix string
|
|
|
|
// UpdateAssetName is the goreleaser archive prefix (e.g.
|
|
// "docker-credential-atcr") used to construct download URLs in
|
|
// `update`.
|
|
UpdateAssetName string
|
|
|
|
// ReleasesBaseURL is the tangled.org base URL for fetching latest tag
|
|
// and downloading release artifacts.
|
|
ReleasesBaseURL string
|
|
|
|
// Version, Commit, Date are stamped at link time via -ldflags.
|
|
Version, Commit, Date string
|
|
}
|
|
|
|
// helperName derives the Docker credHelpers map value from the binary name.
|
|
// Docker discovers helpers by `docker-credential-<name>`; the map value must
|
|
// be exactly the suffix.
|
|
func helperName(cfg Config) string {
|
|
return strings.TrimPrefix(cfg.BinaryName, "docker-credential-")
|
|
}
|
|
|
|
// Package-level state. Run() initialises `cfg` before any cobra command
|
|
// executes; helpers (http.go, etc.) read from it.
|
|
var cfg Config
|
|
|
|
// timeNow is a variable so tests can override it.
|
|
var timeNow = time.Now
|
|
|
|
// updateCheckCacheTTL is how long an update check is cached on disk.
|
|
const updateCheckCacheTTL = 24 * time.Hour
|
|
|
|
// Run executes the credential helper CLI with the supplied per-brand Config.
|
|
// Intended to be called from a thin main package; never returns under normal
|
|
// operation (calls os.Exit on cobra error).
|
|
func Run(c Config) {
|
|
cfg = c
|
|
|
|
rootCmd := &cobra.Command{
|
|
Use: cfg.BinaryName,
|
|
Short: cfg.BinaryName + " — ATProto container registry credential helper",
|
|
Long: fmt.Sprintf(`%s manages authentication for ATProto-backed container registries.
|
|
|
|
It implements the Docker credential helper protocol and provides commands
|
|
for managing multiple accounts across multiple registries.`, cfg.BinaryName),
|
|
Version: fmt.Sprintf("%s (commit: %s, built: %s)", cfg.Version, cfg.Commit, cfg.Date),
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
}
|
|
|
|
// Docker protocol commands (hidden — called by Docker, not users)
|
|
rootCmd.AddCommand(newGetCmd())
|
|
rootCmd.AddCommand(newStoreCmd())
|
|
rootCmd.AddCommand(newEraseCmd())
|
|
rootCmd.AddCommand(newListCmd())
|
|
|
|
// User-facing commands
|
|
rootCmd.AddCommand(newLoginCmd())
|
|
rootCmd.AddCommand(newLogoutCmd())
|
|
rootCmd.AddCommand(newStatusCmd())
|
|
rootCmd.AddCommand(newSwitchCmd())
|
|
rootCmd.AddCommand(newConfigureDockerCmd())
|
|
rootCmd.AddCommand(newUpdateCmd())
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|