forked from rancher/kontainer-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filestore.go
46 lines (42 loc) · 1.18 KB
/
filestore.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
package store
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/rancher/kontainer-engine/cluster"
"github.com/rancher/kontainer-engine/utils"
)
// GetAllClusterFromStore retrieves all the cluster info from disk store
func GetAllClusterFromStore() (map[string]cluster.Cluster, error) {
homeDir := filepath.Join(utils.HomeDir(), "clusters")
dir, err := ioutil.ReadDir(homeDir)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
clusters := map[string]cluster.Cluster{}
// looks for config.json
for _, file := range dir {
if file.IsDir() && !strings.HasPrefix(file.Name(), ".") {
subDir, err := ioutil.ReadDir(filepath.Join(homeDir, file.Name()))
if err != nil && !os.IsNotExist(err) {
return nil, err
}
for _, subFile := range subDir {
if !subFile.IsDir() && strings.HasSuffix(subFile.Name(), "config.json") {
cls := cluster.Cluster{}
data, err := ioutil.ReadFile(filepath.Join(homeDir, file.Name(), subFile.Name()))
if err != nil {
return nil, err
}
if err := json.Unmarshal(data, &cls); err != nil {
return nil, err
}
clusters[cls.Name] = cls
}
}
}
}
return clusters, nil
}