@@ -10,36 +10,43 @@ import (
10
10
"strings"
11
11
)
12
12
13
- type credentialsGetResponse struct {
14
- Username string
15
- Secret string
16
- }
17
-
18
13
// Serve initializes the credentials helper and parses the action argument.
14
+ // This function is designed to be called from a command line interface.
15
+ // It uses os.Args[1] as the key for the action.
16
+ // It uses os.Stdin as input and os.Stdout as output.
17
+ // This function terminates the program with os.Exit(1) if there is an error.
19
18
func Serve (helper Helper ) {
20
- if err := handleCommand (helper ); err != nil {
19
+ var err error
20
+ if len (os .Args ) != 2 {
21
+ err = fmt .Errorf ("Usage: %s <store|get|erase>" , os .Args [0 ])
22
+ }
23
+
24
+ if err == nil {
25
+ err = HandleCommand (helper , os .Args [1 ], os .Stdin , os .Stdout )
26
+ }
27
+
28
+ if err != nil {
21
29
fmt .Fprintf (os .Stdout , "%v\n " , err )
22
30
os .Exit (1 )
23
31
}
24
32
}
25
33
26
- func handleCommand (helper Helper ) error {
27
- if len (os .Args ) != 2 {
28
- return fmt .Errorf ("Usage: %s <store|get|erase>" , os .Args [0 ])
29
- }
30
-
31
- switch os .Args [1 ] {
34
+ // HandleCommand uses a helper and a key to run a credential action.
35
+ func HandleCommand (helper Helper , key string , in io.Reader , out io.Writer ) error {
36
+ switch key {
32
37
case "store" :
33
- return store (helper , os . Stdin )
38
+ return Store (helper , in )
34
39
case "get" :
35
- return get (helper , os . Stdin , os . Stdout )
40
+ return Get (helper , in , out )
36
41
case "erase" :
37
- return erase (helper , os . Stdin )
42
+ return Erase (helper , in )
38
43
}
39
- return fmt .Errorf ("Usage: %s <store|get|erase> " , os . Args [ 0 ] )
44
+ return fmt .Errorf ("Unknown credential action `%s` " , key )
40
45
}
41
46
42
- func store (helper Helper , reader io.Reader ) error {
47
+ // Store uses a helper and an input reader to save credentials.
48
+ // The reader must contain the JSON serialization of a Credentials struct.
49
+ func Store (helper Helper , reader io.Reader ) error {
43
50
scanner := bufio .NewScanner (reader )
44
51
45
52
buffer := new (bytes.Buffer )
@@ -59,7 +66,10 @@ func store(helper Helper, reader io.Reader) error {
59
66
return helper .Add (& creds )
60
67
}
61
68
62
- func get (helper Helper , reader io.Reader , writer io.Writer ) error {
69
+ // Get retrieves the credentials for a given server url.
70
+ // The reader must contain the server URL to search.
71
+ // The writer is used to write the JSON serialization of the credentials.
72
+ func Get (helper Helper , reader io.Reader , writer io.Writer ) error {
63
73
scanner := bufio .NewScanner (reader )
64
74
65
75
buffer := new (bytes.Buffer )
@@ -78,7 +88,7 @@ func get(helper Helper, reader io.Reader, writer io.Writer) error {
78
88
return err
79
89
}
80
90
81
- resp := credentialsGetResponse {
91
+ resp := Credentials {
82
92
Username : username ,
83
93
Secret : secret ,
84
94
}
@@ -92,7 +102,9 @@ func get(helper Helper, reader io.Reader, writer io.Writer) error {
92
102
return nil
93
103
}
94
104
95
- func erase (helper Helper , reader io.Reader ) error {
105
+ // Erase removes credentials from the store.
106
+ // The reader must contain the server URL to remove.
107
+ func Erase (helper Helper , reader io.Reader ) error {
96
108
scanner := bufio .NewScanner (reader )
97
109
98
110
buffer := new (bytes.Buffer )
0 commit comments