forked from pinpox/base16-universal-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
123 lines (92 loc) · 2.87 KB
/
template.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
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"strings"
)
type Base16TemplateFile struct {
Extension string `yaml:"extension"`
Output string `yaml:"output"`
}
type Base16Template struct {
//The actual template
Files map[string]Base16TemplateFile
//Name (of the application)
Name string
RepoURL string
RawBaseURL string
}
func (l *Base16TemplateList) GetBase16Template(name string) Base16Template {
// yamlURL := "https://raw.githubusercontent.com/" + parts[3] + "/" + parts[4] + "/master/templates/config.yaml"
if len(name) == 0 {
panic("Template name was empty")
}
var newTemplate Base16Template
newTemplate.RepoURL = l.templates[name]
parts := strings.Split(l.templates[name], "/")
newTemplate.RawBaseURL = "https://raw.githubusercontent.com/" + parts[3] + "/" + parts[4] + "/master/"
newTemplate.Name = name
path := appConf.TemplatesCachePath + name
// Create local template file, if not present
if _, err := os.Stat(path); os.IsNotExist(err) {
templateData, err := DownloadFileToStirng(newTemplate.RawBaseURL + "templates/config.yaml")
check(err)
saveFile, err := os.Create(path)
//TODO delete old file?
defer saveFile.Close()
saveFile.Write([]byte(templateData))
saveFile.Close()
}
template, err := ioutil.ReadFile(path)
check(err)
//TODO cache actual templates
var files map[string]Base16TemplateFile
err = yaml.Unmarshal(template, &files)
check(err)
newTemplate.Files = files
return newTemplate
}
type Base16TemplateList struct {
templates map[string]string
}
func (l *Base16TemplateList) UpdateTemplates() {
//Get all repos from master source
var templRepos map[string]string
templatesYAML, err := DownloadFileToStirng(appConf.TemplatesMasterURL)
check(err)
err = yaml.Unmarshal([]byte(templatesYAML), &templRepos)
check(err)
fmt.Println("Found template repos: ", len(templRepos))
for k, v := range templRepos {
l.templates[k] = v
}
SaveBase16TemplateList(Base16TemplateList{l.templates})
}
func LoadBase16TemplateList() Base16TemplateList {
colorschemes := LoadStringMap(appConf.TemplatesListFile)
return Base16TemplateList{colorschemes}
}
func SaveBase16TemplateList(l Base16TemplateList) {
SaveStringMap(l.templates, appConf.TemplatesListFile)
}
func (c *Base16TemplateList) Find(input string) Base16Template {
if _, err := os.Stat(appConf.TemplatesListFile); os.IsNotExist(err) {
check(err)
fmt.Println("Templates list not found, pulling new one...")
c.UpdateTemplates()
}
if len(c.templates) == 0 {
fmt.Println("No templates in list, pulling new one... ")
c.UpdateTemplates()
}
templateName := FindMatchInMap(c.templates, input)
return c.GetBase16Template(templateName)
}
func (c *Base16TemplateList) Print() {
fmt.Printf("The following %d Templates and files are available:\n",len(c.templates))
for _,templateName := range sortMapKeys(c.templates) {
fmt.Println(" -",templateName)
}
}