@@ -34,7 +34,7 @@ type Osxkeychain struct{}
34
34
35
35
// Add adds new credentials to the keychain.
36
36
func (h Osxkeychain ) Add (creds * credentials.Credentials ) error {
37
- h .Delete (creds .ServerURL )
37
+ _ = h .Delete (creds .ServerURL ) // ignore errors as existing credential may not exist.
38
38
39
39
s , err := splitServer (creds .ServerURL )
40
40
if err != nil {
@@ -66,10 +66,16 @@ func (h Osxkeychain) Delete(serverURL string) error {
66
66
}
67
67
defer freeServer (s )
68
68
69
- errMsg := C .keychain_delete (s )
70
- if errMsg != nil {
69
+ if errMsg := C .keychain_delete (s ); errMsg != nil {
71
70
defer C .free (unsafe .Pointer (errMsg ))
72
- return errors .New (C .GoString (errMsg ))
71
+ switch goMsg := C .GoString (errMsg ); goMsg {
72
+ case errCredentialsNotFound :
73
+ return credentials .NewErrCredentialsNotFound ()
74
+ case errInteractionNotAllowed :
75
+ return ErrInteractionNotAllowed
76
+ default :
77
+ return errors .New (goMsg )
78
+ }
73
79
}
74
80
75
81
return nil
@@ -93,15 +99,14 @@ func (h Osxkeychain) Get(serverURL string) (string, string, error) {
93
99
errMsg := C .keychain_get (s , & usernameLen , & username , & secretLen , & secret )
94
100
if errMsg != nil {
95
101
defer C .free (unsafe .Pointer (errMsg ))
96
- goMsg := C .GoString (errMsg )
97
- if goMsg == errCredentialsNotFound {
102
+ switch goMsg := C .GoString (errMsg ); goMsg {
103
+ case errCredentialsNotFound :
98
104
return "" , "" , credentials .NewErrCredentialsNotFound ()
99
- }
100
- if goMsg == errInteractionNotAllowed {
105
+ case errInteractionNotAllowed :
101
106
return "" , "" , ErrInteractionNotAllowed
107
+ default :
108
+ return "" , "" , errors .New (goMsg )
102
109
}
103
-
104
- return "" , "" , errors .New (goMsg )
105
110
}
106
111
107
112
user := C .GoStringN (username , C .int (usernameLen ))
@@ -124,15 +129,14 @@ func (h Osxkeychain) List() (map[string]string, error) {
124
129
defer C .freeListData (& acctsC , listLenC )
125
130
if errMsg != nil {
126
131
defer C .free (unsafe .Pointer (errMsg ))
127
- goMsg := C .GoString (errMsg )
128
- if goMsg == errCredentialsNotFound {
132
+ switch goMsg := C .GoString (errMsg ); goMsg {
133
+ case errCredentialsNotFound :
129
134
return make (map [string ]string ), nil
130
- }
131
- if goMsg == errInteractionNotAllowed {
135
+ case errInteractionNotAllowed :
132
136
return nil , ErrInteractionNotAllowed
137
+ default :
138
+ return nil , errors .New (goMsg )
133
139
}
134
-
135
- return nil , errors .New (goMsg )
136
140
}
137
141
138
142
var listLen int
0 commit comments