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