-
Notifications
You must be signed in to change notification settings - Fork 1
/
catalog.go
147 lines (119 loc) · 3.75 KB
/
catalog.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"github.com/rancher/go-rancher/client"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
var Catalog map[string]Template
const catalogRoot string = "./DATA/templates/"
var metadataFolder = regexp.MustCompile(`^DATA/templates/[^/]+$`)
/*var Schemas = Schemas{
Data: []Schema{
{},
{},
},
}*/
type TemplateCollection struct {
client.Collection
Data []Template `json:"data,omitempty"`
}
func Init() {
_, err := os.Stat(catalogRoot)
if err != nil {
cloneCatalog()
}else {
pullCatalog()
}
Catalog = make(map[string]Template)
filepath.Walk(catalogRoot, walkCatalog)
}
func cloneCatalog() {
fmt.Println("Cloning the catalog from github")
//git clone the github repo
e := exec.Command("git", "clone", "https://github.com/prachidamle/rancher-catalog", "./DATA")
err := e.Run()
if err != nil {
fmt.Println(err)
}
}
func pullCatalog() {
fmt.Println("Pulling the catalog from github to sync changes")
e := exec.Command("git", "-C", "./DATA", "pull", "origin", "master")
err := e.Run()
if err != nil {
fmt.Println("error in pull", err)
}
}
func walkCatalog(path string, f os.FileInfo, err error) error {
if f.IsDir() && metadataFolder.MatchString(path) {
fmt.Printf("Found a metadata folder, name:%s \n", f.Name())
newTemplate := Template{}
//read the root level config.yml
filename, _ := filepath.Abs(path + "/config.yml")
yamlFile, _ := ioutil.ReadFile(filename)
config := make(map[string]string)
_ = yaml.Unmarshal(yamlFile, &config)
newTemplate.Name = config["name"]
newTemplate.Category = config["category"]
newTemplate.Description = config["description"]
newTemplate.DefaultVersion = config["defaultVersion"]
newTemplate.Path = f.Name()
//list the folders under the root level
newTemplate.VersionLinks = make(map[string]string)
dirList, _ := ioutil.ReadDir(path)
for _, subfile := range dirList {
if subfile.IsDir() {
newTemplate.VersionLinks[subfile.Name()] = f.Name() + "/" + subfile.Name()
}else if strings.HasPrefix(subfile.Name(), "catalogIcon"){
newTemplate.IconLink = f.Name() + "/" + subfile.Name()
}
}
Catalog[f.Name()] = newTemplate
}
return nil
}
func readTemplateVersion(path string) Template{
dirList, error := ioutil.ReadDir(catalogRoot + path)
newTemplate := Template{}
if error != nil{
//return 404
}else {
for _, subfile := range dirList {
if strings.HasPrefix(subfile.Name(), "config.yml") {
filename, _ := filepath.Abs(catalogRoot + path + "/config.yml")
yamlFile, _ := ioutil.ReadFile(filename)
config := make(map[string]string)
_ = yaml.Unmarshal(yamlFile, &config)
newTemplate.Name = config["name"]
newTemplate.Category = config["category"]
newTemplate.Description = config["description"]
newTemplate.DefaultVersion = config["defaultVersion"]
newTemplate.Path = path
}else if strings.HasPrefix(subfile.Name(), "catalogIcon"){
newTemplate.IconLink = path + "/" + subfile.Name()
}else if strings.HasPrefix(subfile.Name(), "docker-compose"){
filename, _ := filepath.Abs(catalogRoot + path + "/" + subfile.Name())
composefile, _ := ioutil.ReadFile(filename)
newTemplate.DockerCompose = string(composefile)
}else if strings.HasPrefix(subfile.Name(), "rancher-compose"){
filename, _ := filepath.Abs(catalogRoot + path + "/" + subfile.Name())
composeBytes, _ := ioutil.ReadFile(filename)
newTemplate.RancherCompose = string(composeBytes)
//read the questions section
RC := make(map[string]RancherCompose)
_ = yaml.Unmarshal(composeBytes, &RC)
//newTemplate.Questions = RC["myService"].Questions
for key, _ := range RC {
newTemplate.Questions = RC[key].Questions
}
}
}
}
return newTemplate
}