Skip to content

Commit 4b8917b

Browse files
committed
Merge pull request #12 from calavera/export_library
Export credentials functions to be able to use them as a library.
2 parents 6bbb56a + 039c315 commit 4b8917b

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

credentials/credentials.go

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

13-
type credentialsGetResponse struct {
14-
Username string
15-
Secret string
16-
}
17-
1813
// 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.
1918
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 {
2129
fmt.Fprintf(os.Stdout, "%v\n", err)
2230
os.Exit(1)
2331
}
2432
}
2533

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 {
3237
case "store":
33-
return store(helper, os.Stdin)
38+
return Store(helper, in)
3439
case "get":
35-
return get(helper, os.Stdin, os.Stdout)
40+
return Get(helper, in, out)
3641
case "erase":
37-
return erase(helper, os.Stdin)
42+
return Erase(helper, in)
3843
}
39-
return fmt.Errorf("Usage: %s <store|get|erase>", os.Args[0])
44+
return fmt.Errorf("Unknown credential action `%s`", key)
4045
}
4146

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 {
4350
scanner := bufio.NewScanner(reader)
4451

4552
buffer := new(bytes.Buffer)
@@ -59,7 +66,10 @@ func store(helper Helper, reader io.Reader) error {
5966
return helper.Add(&creds)
6067
}
6168

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 {
6373
scanner := bufio.NewScanner(reader)
6474

6575
buffer := new(bytes.Buffer)
@@ -78,7 +88,7 @@ func get(helper Helper, reader io.Reader, writer io.Writer) error {
7888
return err
7989
}
8090

81-
resp := credentialsGetResponse{
91+
resp := Credentials{
8292
Username: username,
8393
Secret: secret,
8494
}
@@ -92,7 +102,9 @@ func get(helper Helper, reader io.Reader, writer io.Writer) error {
92102
return nil
93103
}
94104

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 {
96108
scanner := bufio.NewScanner(reader)
97109

98110
buffer := new(bytes.Buffer)

credentials/credentials_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestStore(t *testing.T) {
5050
in := bytes.NewReader(b)
5151

5252
h := newMemoryStore()
53-
if err := store(h, in); err != nil {
53+
if err := Store(h, in); err != nil {
5454
t.Fatal(err)
5555
}
5656

@@ -82,21 +82,21 @@ func TestGet(t *testing.T) {
8282
in := bytes.NewReader(b)
8383

8484
h := newMemoryStore()
85-
if err := store(h, in); err != nil {
85+
if err := Store(h, in); err != nil {
8686
t.Fatal(err)
8787
}
8888

8989
buf := strings.NewReader(serverURL)
9090
w := new(bytes.Buffer)
91-
if err := get(h, buf, w); err != nil {
91+
if err := Get(h, buf, w); err != nil {
9292
t.Fatal(err)
9393
}
9494

9595
if w.Len() == 0 {
9696
t.Fatalf("expected output in the writer, got %d", w.Len())
9797
}
9898

99-
var c credentialsGetResponse
99+
var c Credentials
100100
if err := json.NewDecoder(w).Decode(&c); err != nil {
101101
t.Fatal(err)
102102
}
@@ -124,17 +124,17 @@ func TestErase(t *testing.T) {
124124
in := bytes.NewReader(b)
125125

126126
h := newMemoryStore()
127-
if err := store(h, in); err != nil {
127+
if err := Store(h, in); err != nil {
128128
t.Fatal(err)
129129
}
130130

131131
buf := strings.NewReader(serverURL)
132-
if err := erase(h, buf); err != nil {
132+
if err := Erase(h, buf); err != nil {
133133
t.Fatal(err)
134134
}
135135

136136
w := new(bytes.Buffer)
137-
if err := get(h, buf, w); err == nil {
137+
if err := Get(h, buf, w); err == nil {
138138
t.Fatal("expected error getting missing creds, got empty")
139139
}
140140
}

credentials/helper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ type Credentials struct {
1111

1212
// Helper is the interface a credentials store helper must implement.
1313
type Helper interface {
14+
// Add appends credentials to the store.
1415
Add(*Credentials) error
16+
// Delete removes credentials from the store.
1517
Delete(serverURL string) error
18+
// Get retrieves credentials from the store.
19+
// It returns username and secret as strings.
1620
Get(serverURL string) (string, string, error)
1721
}
1822

0 commit comments

Comments
 (0)