@@ -3,6 +3,7 @@ package client
3
3
import (
4
4
"bytes"
5
5
"encoding/json"
6
+ "errors"
6
7
"fmt"
7
8
"io/ioutil"
8
9
"os"
@@ -94,7 +95,10 @@ func ContextFromIP(apiServer string) (*Cluster, *Context, error) {
94
95
return nil , nil , err
95
96
}
96
97
97
- if err := find (clusters , "cluster.server" , apiServer , & cluster , ErrorNoCluster (apiServer )); err != nil {
98
+ err = find (clusters , "cluster.server" , apiServer , & cluster )
99
+ if err == ErrorNoMatch {
100
+ return nil , nil , ErrorNoCluster (apiServer )
101
+ } else if err != nil {
98
102
return nil , nil , err
99
103
}
100
104
@@ -105,7 +109,10 @@ func ContextFromIP(apiServer string) (*Cluster, *Context, error) {
105
109
return nil , nil , err
106
110
}
107
111
108
- if err := find (contexts , "context.cluster" , cluster .Name , & context , ErrorNoContext (cluster .Name )); err != nil {
112
+ err = find (contexts , "context.cluster" , cluster .Name , & context )
113
+ if err == ErrorNoMatch {
114
+ return nil , nil , ErrorNoContext (cluster .Name )
115
+ } else if err != nil {
109
116
return nil , nil , err
110
117
}
111
118
@@ -127,7 +134,10 @@ func IPFromContext(name string) (ip string, err error) {
127
134
return "" , err
128
135
}
129
136
130
- if err := find (contexts , "name" , name , & context , ErrorNoContext (name )); err != nil {
137
+ err = find (contexts , "name" , name , & context )
138
+ if err == ErrorNoMatch {
139
+ return "" , ErrorNoContext (name )
140
+ } else if err != nil {
131
141
return "" , err
132
142
}
133
143
@@ -139,9 +149,10 @@ func IPFromContext(name string) (ip string, err error) {
139
149
}
140
150
141
151
clusterName := context .Context .Cluster
142
- if err := find (clusters , "name" , clusterName , & cluster ,
143
- fmt .Errorf ("no cluster named `%s` as required by context `%s` was found. Please check your $KUBECONFIG" , clusterName , name ),
144
- ); err != nil {
152
+ err = find (clusters , "name" , clusterName , & cluster )
153
+ if err == ErrorNoMatch {
154
+ return "" , fmt .Errorf ("no cluster named `%s` as required by context `%s` was found. Please check your $KUBECONFIG" , clusterName , name )
155
+ } else if err != nil {
145
156
return "" , err
146
157
}
147
158
@@ -160,9 +171,12 @@ func tryMSISlice(v *objx.Value, what string) ([]map[string]interface{}, error) {
160
171
return data , nil
161
172
}
162
173
174
+ // ErrorNoMatch occurs when no item matched had the expected value
175
+ var ErrorNoMatch error = errors .New ("no matches found" )
176
+
163
177
// find attempts to find an object in list whose prop equals expected.
164
178
// If found, the value is unmarshalled to ptr, otherwise errNotFound is returned.
165
- func find (list []map [string ]interface {}, prop string , expected string , ptr interface {}, errNotFound error ) error {
179
+ func find (list []map [string ]interface {}, prop string , expected string , ptr interface {}) error {
166
180
var findErr error
167
181
i := funk .Find (list , func (x map [string ]interface {}) bool {
168
182
if findErr != nil {
@@ -183,7 +197,7 @@ func find(list []map[string]interface{}, prop string, expected string, ptr inter
183
197
}
184
198
185
199
if i == nil {
186
- return errNotFound
200
+ return ErrorNoMatch
187
201
}
188
202
189
203
o := objx .New (i ).MustJSON ()
0 commit comments