forked from pinpox/base16-universal-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (88 loc) · 2.88 KB
/
main.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
package main
import (
"fmt"
"github.com/hoisie/mustache"
"gopkg.in/alecthomas/kingpin.v2"
"os"
"path/filepath"
)
// Configuration file
var configFile string
//Flags
var (
updateFlag = kingpin.Flag("update-list", "Update the list of templates and colorschemes").Bool()
clearListFlag = kingpin.Flag("clear-list", "Delete local master list caches").Bool()
clearSchemesFlag = kingpin.Flag("clear-templates", "Delete local scheme caches").Bool()
clearTemplatesFlag = kingpin.Flag("clear-schemes", "Delete local template caches").Bool()
configFileFlag = kingpin.Flag("config", "Specify configuration file to use").Default("config.yaml").String()
)
//Configuration
var appConf SetterConfig
func main() {
//Pase Flags
kingpin.Version("1.0.0")
kingpin.Parse()
appConf = NewConfig(*configFileFlag)
// appConf.Show()
//TODO delete caches, if user wants to
//Create cache paths, if missing
p1 := filepath.Join(".", appConf.SchemesCachePath)
os.MkdirAll(p1, os.ModePerm)
p2 := filepath.Join(".", appConf.TemplatesCachePath)
os.MkdirAll(p2, os.ModePerm)
schemeList := LoadBase16ColorschemeList()
templateList := LoadBase16TemplateList()
if *updateFlag {
schemeList.UpdateSchemes()
templateList.UpdateTemplates()
}
scheme := schemeList.Find(appConf.Colorscheme)
fmt.Println("[CONFIG]: Selected scheme: ", scheme.Name)
for k := range appConf.Applications {
schemeList = LoadBase16ColorschemeList()
templateList = LoadBase16TemplateList()
templ := templateList.Find(k)
Base16Render(templ, scheme)
}
}
func Base16Render(templ Base16Template, scheme Base16Colorscheme) {
fmt.Println("[RENDER]: Rendering template \"" + templ.Name + "\"")
for k, v := range templ.Files {
templFileData, err := DownloadFileToStirng(templ.RawBaseURL + "templates/" + k + ".mustache")
check(err)
renderedFile := mustache.Render(templFileData, scheme.MustacheContext())
saveBasePath := appConf.Applications[templ.Name].Files[k] + "/"
p4 := filepath.Join(".", saveBasePath)
os.MkdirAll(p4, os.ModePerm)
savePath := saveBasePath + k + v.Extension
//If DryRun is enabled, just print the output location for debugging
if appConf.DryRun {
fmt.Println(" - (dryrun) file would be written to: ", savePath)
} else {
switch appConf.Applications[templ.Name].Mode {
case "rewrite":
fmt.Println(" - writing: ", savePath)
saveFile, err := os.Create(savePath)
defer saveFile.Close()
check(err)
saveFile.Write([]byte(renderedFile))
saveFile.Close()
case "append":
fmt.Println(" - appending to: ", savePath)
case "replace":
fmt.Println(" - replacing in: ", savePath)
}
}
}
if appConf.DryRun {
fmt.Println("Not running hook, DryRun enabled: ", appConf.Applications[templ.Name].Hook)
} else {
exe_cmd(appConf.Applications[templ.Name].Hook)
}
}
//TODO proper error handling
func check(e error) {
if e != nil {
panic(e)
}
}