Skip to content

Commit 129017a

Browse files
committed
credentials: define consts for supported actions (sub-commands)
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 99079ca commit 129017a

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

client/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func isValidCredsMessage(msg string) error {
2626

2727
// Store uses an external program to save credentials.
2828
func Store(program ProgramFunc, creds *credentials.Credentials) error {
29-
cmd := program("store")
29+
cmd := program(credentials.ActionStore)
3030

3131
buffer := new(bytes.Buffer)
3232
if err := json.NewEncoder(buffer).Encode(creds); err != nil {
@@ -50,7 +50,7 @@ func Store(program ProgramFunc, creds *credentials.Credentials) error {
5050

5151
// Get executes an external program to get the credentials from a native store.
5252
func Get(program ProgramFunc, serverURL string) (*credentials.Credentials, error) {
53-
cmd := program("get")
53+
cmd := program(credentials.ActionGet)
5454
cmd.Input(strings.NewReader(serverURL))
5555

5656
out, err := cmd.Output()
@@ -81,7 +81,7 @@ func Get(program ProgramFunc, serverURL string) (*credentials.Credentials, error
8181

8282
// Erase executes a program to remove the server credentials from the native store.
8383
func Erase(program ProgramFunc, serverURL string) error {
84-
cmd := program("erase")
84+
cmd := program(credentials.ActionErase)
8585
cmd.Input(strings.NewReader(serverURL))
8686
out, err := cmd.Output()
8787
if err != nil {
@@ -99,7 +99,7 @@ func Erase(program ProgramFunc, serverURL string) error {
9999

100100
// List executes a program to list server credentials in the native store.
101101
func List(program ProgramFunc) (map[string]string, error) {
102-
cmd := program("list")
102+
cmd := program(credentials.ActionList)
103103
cmd.Input(strings.NewReader("unused"))
104104
out, err := cmd.Output()
105105
if err != nil {

credentials/credentials.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ import (
1010
"strings"
1111
)
1212

13+
// Action defines the name of an action (sub-command) supported by a
14+
// credential-helper binary. It is an alias for "string", and mostly
15+
// for convenience.
16+
type Action = string
17+
18+
// List of actions (sub-commands) supported by credential-helper binaries.
19+
const (
20+
ActionStore Action = "store"
21+
ActionGet Action = "get"
22+
ActionErase Action = "erase"
23+
ActionList Action = "list"
24+
ActionVersion Action = "version"
25+
)
26+
1327
// Credentials holds the information shared between docker and the credentials store.
1428
type Credentials struct {
1529
ServerURL string
@@ -74,17 +88,17 @@ func usage() string {
7488
}
7589

7690
// HandleCommand runs a helper to execute a credential action.
77-
func HandleCommand(helper Helper, action string, in io.Reader, out io.Writer) error {
91+
func HandleCommand(helper Helper, action Action, in io.Reader, out io.Writer) error {
7892
switch action {
79-
case "store":
93+
case ActionStore:
8094
return Store(helper, in)
81-
case "get":
95+
case ActionGet:
8296
return Get(helper, in, out)
83-
case "erase":
97+
case ActionErase:
8498
return Erase(helper, in)
85-
case "list":
99+
case ActionList:
86100
return List(helper, out)
87-
case "version":
101+
case ActionVersion:
88102
return PrintVersion(out)
89103
default:
90104
return fmt.Errorf("%s: unknown action: %s", Name, action)

0 commit comments

Comments
 (0)