1
0
mirror of https://github.com/docker/docker-credential-helpers.git synced 2026-06-13 16:01:28 +05:30

credentials: define consts for supported actions (sub-commands)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2023-05-28 13:47:57 +02:00
parent 99079cafd2
commit 129017a3cd
2 changed files with 24 additions and 10 deletions
+4 -4
View File
@@ -26,7 +26,7 @@ func isValidCredsMessage(msg string) error {
// Store uses an external program to save credentials. // Store uses an external program to save credentials.
func Store(program ProgramFunc, creds *credentials.Credentials) error { func Store(program ProgramFunc, creds *credentials.Credentials) error {
cmd := program("store") cmd := program(credentials.ActionStore)
buffer := new(bytes.Buffer) buffer := new(bytes.Buffer)
if err := json.NewEncoder(buffer).Encode(creds); err != nil { if err := json.NewEncoder(buffer).Encode(creds); err != nil {
@@ -50,7 +50,7 @@ func Store(program ProgramFunc, creds *credentials.Credentials) error {
// Get executes an external program to get the credentials from a native store. // Get executes an external program to get the credentials from a native store.
func Get(program ProgramFunc, serverURL string) (*credentials.Credentials, error) { func Get(program ProgramFunc, serverURL string) (*credentials.Credentials, error) {
cmd := program("get") cmd := program(credentials.ActionGet)
cmd.Input(strings.NewReader(serverURL)) cmd.Input(strings.NewReader(serverURL))
out, err := cmd.Output() out, err := cmd.Output()
@@ -81,7 +81,7 @@ func Get(program ProgramFunc, serverURL string) (*credentials.Credentials, error
// Erase executes a program to remove the server credentials from the native store. // Erase executes a program to remove the server credentials from the native store.
func Erase(program ProgramFunc, serverURL string) error { func Erase(program ProgramFunc, serverURL string) error {
cmd := program("erase") cmd := program(credentials.ActionErase)
cmd.Input(strings.NewReader(serverURL)) cmd.Input(strings.NewReader(serverURL))
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {
@@ -99,7 +99,7 @@ func Erase(program ProgramFunc, serverURL string) error {
// List executes a program to list server credentials in the native store. // List executes a program to list server credentials in the native store.
func List(program ProgramFunc) (map[string]string, error) { func List(program ProgramFunc) (map[string]string, error) {
cmd := program("list") cmd := program(credentials.ActionList)
cmd.Input(strings.NewReader("unused")) cmd.Input(strings.NewReader("unused"))
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {
+20 -6
View File
@@ -10,6 +10,20 @@ import (
"strings" "strings"
) )
// Action defines the name of an action (sub-command) supported by a
// credential-helper binary. It is an alias for "string", and mostly
// for convenience.
type Action = string
// List of actions (sub-commands) supported by credential-helper binaries.
const (
ActionStore Action = "store"
ActionGet Action = "get"
ActionErase Action = "erase"
ActionList Action = "list"
ActionVersion Action = "version"
)
// Credentials holds the information shared between docker and the credentials store. // Credentials holds the information shared between docker and the credentials store.
type Credentials struct { type Credentials struct {
ServerURL string ServerURL string
@@ -74,17 +88,17 @@ func usage() string {
} }
// HandleCommand runs a helper to execute a credential action. // HandleCommand runs a helper to execute a credential action.
func HandleCommand(helper Helper, action string, in io.Reader, out io.Writer) error { func HandleCommand(helper Helper, action Action, in io.Reader, out io.Writer) error {
switch action { switch action {
case "store": case ActionStore:
return Store(helper, in) return Store(helper, in)
case "get": case ActionGet:
return Get(helper, in, out) return Get(helper, in, out)
case "erase": case ActionErase:
return Erase(helper, in) return Erase(helper, in)
case "list": case ActionList:
return List(helper, out) return List(helper, out)
case "version": case ActionVersion:
return PrintVersion(out) return PrintVersion(out)
default: default:
return fmt.Errorf("%s: unknown action: %s", Name, action) return fmt.Errorf("%s: unknown action: %s", Name, action)