@@ -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.
1428type Credentials struct {
1529 ServerURL string
@@ -43,42 +57,52 @@ func SetCredsLabel(label string) {
4357 CredsLabel = label
4458}
4559
46- // Serve initializes the credentials helper and parses the action argument.
60+ // Serve initializes the credentials- helper and parses the action argument.
4761// This function is designed to be called from a command line interface.
4862// It uses os.Args[1] as the key for the action.
4963// It uses os.Stdin as input and os.Stdout as output.
5064// This function terminates the program with os.Exit(1) if there is an error.
5165func Serve (helper Helper ) {
52- var err error
5366 if len (os .Args ) != 2 {
54- err = fmt .Errorf ("Usage: %s <store|get|erase|list|version>" , os .Args [0 ])
67+ _ , _ = fmt .Fprintln (os .Stdout , usage ())
68+ os .Exit (1 )
5569 }
5670
57- if err == nil {
58- err = HandleCommand (helper , os .Args [1 ], os .Stdin , os .Stdout )
71+ switch os .Args [1 ] {
72+ case "--version" , "-v" :
73+ _ = PrintVersion (os .Stdout )
74+ os .Exit (0 )
75+ case "--help" , "-h" :
76+ _ , _ = fmt .Fprintln (os .Stdout , usage ())
77+ os .Exit (0 )
5978 }
6079
61- if err != nil {
62- fmt .Fprintf (os .Stdout , "%v \n " , err )
80+ if err := HandleCommand ( helper , os . Args [ 1 ], os . Stdin , os . Stdout ); err != nil {
81+ _ , _ = fmt .Fprintln (os .Stdout , err )
6382 os .Exit (1 )
6483 }
6584}
6685
67- // HandleCommand uses a helper and a key to run a credential action.
68- func HandleCommand (helper Helper , key string , in io.Reader , out io.Writer ) error {
69- switch key {
70- case "store" :
86+ func usage () string {
87+ return fmt .Sprintf ("Usage: %s <store|get|erase|list|version>" , Name )
88+ }
89+
90+ // HandleCommand runs a helper to execute a credential action.
91+ func HandleCommand (helper Helper , action Action , in io.Reader , out io.Writer ) error {
92+ switch action {
93+ case ActionStore :
7194 return Store (helper , in )
72- case "get" :
95+ case ActionGet :
7396 return Get (helper , in , out )
74- case "erase" :
97+ case ActionErase :
7598 return Erase (helper , in )
76- case "list" :
99+ case ActionList :
77100 return List (helper , out )
78- case "version" :
101+ case ActionVersion :
79102 return PrintVersion (out )
103+ default :
104+ return fmt .Errorf ("%s: unknown action: %s" , Name , action )
80105 }
81- return fmt .Errorf ("Unknown credential action `%s`" , key )
82106}
83107
84108// Store uses a helper and an input reader to save credentials.
@@ -132,18 +156,17 @@ func Get(helper Helper, reader io.Reader, writer io.Writer) error {
132156 return err
133157 }
134158
135- resp := Credentials {
159+ buffer .Reset ()
160+ err = json .NewEncoder (buffer ).Encode (Credentials {
136161 ServerURL : serverURL ,
137162 Username : username ,
138163 Secret : secret ,
139- }
140-
141- buffer .Reset ()
142- if err := json .NewEncoder (buffer ).Encode (resp ); err != nil {
164+ })
165+ if err != nil {
143166 return err
144167 }
145168
146- fmt .Fprint (writer , buffer .String ())
169+ _ , _ = fmt .Fprint (writer , buffer .String ())
147170 return nil
148171}
149172
@@ -181,6 +204,6 @@ func List(helper Helper, writer io.Writer) error {
181204
182205// PrintVersion outputs the current version.
183206func PrintVersion (writer io.Writer ) error {
184- fmt .Fprintf (writer , "%s (%s) %s\n " , Name , Package , Version )
207+ _ , _ = fmt .Fprintf (writer , "%s (%s) %s\n " , Name , Package , Version )
185208 return nil
186209}
0 commit comments