Skip to content

Commit bdd92dd

Browse files
authored
Merge pull request docker#284 from thaJeztah/various_cleanups
Assorted improvements, and add "--version, -v", and "--help, -h" flags
2 parents 0b91805 + 129017a commit bdd92dd

File tree

4 files changed

+68
-47
lines changed

4 files changed

+68
-47
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 {

client/client_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ const (
2121
var errProgramExited = fmt.Errorf("exited 1")
2222

2323
// mockProgram simulates interactions between the docker client and a remote
24-
// credentials helper.
24+
// credentials-helper.
2525
// Unit tests inject this mocked command into the remote to control execution.
2626
type mockProgram struct {
2727
arg string
2828
input io.Reader
2929
}
3030

31-
// Output returns responses from the remote credentials helper.
31+
// Output returns responses from the remote credentials-helper.
3232
// It mocks those responses based in the input in the mock.
3333
func (m *mockProgram) Output() ([]byte, error) {
3434
in, err := io.ReadAll(m.input)
@@ -80,7 +80,7 @@ func (m *mockProgram) Output() ([]byte, error) {
8080
return []byte(fmt.Sprintf("unknown argument %q with %q", m.arg, inS)), errProgramExited
8181
}
8282

83-
// Input sets the input to send to a remote credentials helper.
83+
// Input sets the input to send to a remote credentials-helper.
8484
func (m *mockProgram) Input(in io.Reader) {
8585
m.input = in
8686
}
@@ -92,16 +92,16 @@ func mockProgramFn(args ...string) Program {
9292
}
9393

9494
func ExampleStore() {
95-
p := NewShellProgramFunc("docker-credential-secretservice")
95+
p := NewShellProgramFunc("docker-credential-pass")
9696

9797
c := &credentials.Credentials{
98-
ServerURL: "https://example.com",
99-
Username: "calavera",
98+
ServerURL: "https://registry.example.com",
99+
Username: "exampleuser",
100100
Secret: "my super secret token",
101101
}
102102

103103
if err := Store(p, c); err != nil {
104-
fmt.Println(err)
104+
_, _ = fmt.Println(err)
105105
}
106106
}
107107

@@ -129,14 +129,14 @@ func TestStore(t *testing.T) {
129129
}
130130

131131
func ExampleGet() {
132-
p := NewShellProgramFunc("docker-credential-secretservice")
132+
p := NewShellProgramFunc("docker-credential-pass")
133133

134-
creds, err := Get(p, "https://example.com")
134+
creds, err := Get(p, "https://registry.example.com")
135135
if err != nil {
136-
fmt.Println(err)
136+
_, _ = fmt.Println(err)
137137
}
138138

139-
fmt.Printf("Got credentials for user `%s` in `%s`\n", creds.Username, creds.ServerURL)
139+
_, _ = fmt.Printf("Got credentials for user `%s` in `%s`\n", creds.Username, creds.ServerURL)
140140
}
141141

142142
func TestGet(t *testing.T) {
@@ -190,10 +190,10 @@ func TestGet(t *testing.T) {
190190
}
191191

192192
func ExampleErase() {
193-
p := NewShellProgramFunc("docker-credential-secretservice")
193+
p := NewShellProgramFunc("docker-credential-pass")
194194

195-
if err := Erase(p, "https://example.com"); err != nil {
196-
fmt.Println(err)
195+
if err := Erase(p, "https://registry.example.com"); err != nil {
196+
_, _ = fmt.Println(err)
197197
}
198198
}
199199

client/command.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package client
22

33
import (
4-
"fmt"
54
"io"
65
"os"
76
"os/exec"
@@ -30,27 +29,26 @@ func NewShellProgramFuncWithEnv(name string, env *map[string]string) ProgramFunc
3029

3130
func createProgramCmdRedirectErr(commandName string, args []string, env *map[string]string) *exec.Cmd {
3231
programCmd := exec.Command(commandName, args...)
33-
programCmd.Env = os.Environ()
3432
if env != nil {
3533
for k, v := range *env {
36-
programCmd.Env = append(programCmd.Env, fmt.Sprintf("%s=%s", k, v))
34+
programCmd.Env = append(programCmd.Environ(), k+"="+v)
3735
}
3836
}
3937
programCmd.Stderr = os.Stderr
4038
return programCmd
4139
}
4240

43-
// Shell invokes shell commands to talk with a remote credentials helper.
41+
// Shell invokes shell commands to talk with a remote credentials-helper.
4442
type Shell struct {
4543
cmd *exec.Cmd
4644
}
4745

48-
// Output returns responses from the remote credentials helper.
46+
// Output returns responses from the remote credentials-helper.
4947
func (s *Shell) Output() ([]byte, error) {
5048
return s.cmd.Output()
5149
}
5250

53-
// Input sets the input to send to a remote credentials helper.
51+
// Input sets the input to send to a remote credentials-helper.
5452
func (s *Shell) Input(in io.Reader) {
5553
s.cmd.Stdin = in
5654
}

credentials/credentials.go

Lines changed: 46 additions & 23 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
@@ -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.
5165
func 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.
183206
func 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

Comments
 (0)