-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkube.go
49 lines (41 loc) · 1.08 KB
/
kube.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"io/ioutil"
"path/filepath"
"regexp"
"github.com/pkg/errors"
)
type Candidate struct {
Name string
FullPath string
}
// KubeconfigFilenamePattern defines the name pattern of kubeconfig files
var KubeconfigFilenamePattern = regexp.MustCompile("^(.*)\\.(kubeconfig|config)$")
// ListKubeconfigCandidatesInDir lists all files in dir that matches KubeconfigFilenamePattern
func ListKubeconfigCandidatesInDir(dir string) ([]Candidate, error) {
fileInfo, err := ioutil.ReadDir(dir)
if err != nil {
return nil, errors.Wrap(err, "ioutil.ReadDir error")
}
var files []Candidate
for _, file := range fileInfo {
if file.IsDir() || IsSymlink(file) {
continue
}
if file.Name() == "config" {
files = append(files, Candidate{
Name: file.Name(),
FullPath: filepath.Join(dir, file.Name()),
})
continue
}
matches := KubeconfigFilenamePattern.FindStringSubmatch(file.Name())
if len(matches) >= 2 {
files = append(files, Candidate{
Name: matches[1],
FullPath: filepath.Join(dir, file.Name()),
})
}
}
return files, nil
}