mirror of
https://github.com/docker/docker-credential-helpers.git
synced 2026-06-13 16:01:28 +05:30
Implement client.List, change list API
[]string, []string -> map[string]string because the other APIs assume a 1:1 correspondence Signed-off-by: Jake Sanders <jsand@google.com>
This commit is contained in:
+11
-5
@@ -68,14 +68,20 @@ func Erase(program ProgramFunc, serverURL string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// List executes a program to remove the server credentials from the native store.
|
||||
func List(program ProgramFunc) error {
|
||||
// List executes a program to list server credentials in the native store.
|
||||
func List(program ProgramFunc) (map[string]string, error) {
|
||||
cmd := program("list")
|
||||
cmd.Input(strings.NewReader("garbage"))
|
||||
cmd.Input(strings.NewReader("unused"))
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
t := strings.TrimSpace(string(out))
|
||||
return fmt.Errorf("error listing credentials - err: %v, out: `%s`", err, t)
|
||||
return nil, fmt.Errorf("error listing credentials - err: %v, out: `%s`", err, t)
|
||||
}
|
||||
return nil
|
||||
|
||||
var resp map[string]string
|
||||
if err = json.NewDecoder(bytes.NewReader(out)).Decode(&resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
const (
|
||||
validServerAddress = "https://index.docker.io/v1"
|
||||
validUsername = "linus"
|
||||
validServerAddress2 = "https://example.com:5002"
|
||||
invalidServerAddress = "https://foobar.example.com"
|
||||
missingCredsAddress = "https://missing.docker.io/v1"
|
||||
@@ -71,7 +72,7 @@ func (m *mockProgram) Output() ([]byte, error) {
|
||||
return []byte("error storing credentials"), errProgramExited
|
||||
}
|
||||
case "list":
|
||||
return []byte(`{"Path":"e237574ae22fd53ddb9490dc1f72139946fd5372d42ba54d1eeb3ae5068fd22b","Username":"http://example.com/collections\u003cnotary_key\u003eSnapshot"}`), nil
|
||||
return []byte(fmt.Sprintf(`{"%s": "%s"}`, validServerAddress, validUsername)), nil
|
||||
|
||||
}
|
||||
|
||||
@@ -195,7 +196,12 @@ func TestErase(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
if err := List(mockProgramFn); err != nil {
|
||||
auths, err := List(mockProgramFn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if username, exists := auths[validServerAddress]; !exists || username != validUsername {
|
||||
t.Fatalf("auths[%s] returned %s, %t; expected %s, %t", validServerAddress, username, exists, validUsername, true)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user