forked from pinpox/base16-universal-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
264 lines (222 loc) · 7.44 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"fmt"
"go4.org/xdgdir"
"os"
"bufio"
"path/filepath"
"strings"
"github.com/hoisie/mustache"
"gopkg.in/alecthomas/kingpin.v2"
)
// 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()
listSchemesFlag = kingpin.Flag("list-schemes", "List templates and exit").Bool()
listTemplatesFlag = kingpin.Flag("list-templates", "List schemes and exit").Bool()
listTemplateFilesFlag = kingpin.Flag("list-templatefiles", "List files for template and exit").Default("").String()
clearSchemesFlag = kingpin.Flag("clear-schemes", "Delete local scheme caches").Bool()
clearTemplatesFlag = kingpin.Flag("clear-template", "Delete local template caches").Bool()
configFileFlag = kingpin.Flag("config", "Specify configuration file to use").Default(filepath.Join(xdgdir.Config.Path(),"base16-universal-manager/config.yaml")).String()
schemeFlag = kingpin.Flag("scheme", "Override scheme from config file").Default("").String()
printConfigFlag = kingpin.Flag("print-config", "Print current configuration").Bool()
)
//Configuration
var appConf SetterConfig
func main() {
//Parse Flags
kingpin.Version("0.2.1")
kingpin.Parse()
appConf = NewConfig(*configFileFlag)
if *printConfigFlag {
appConf.Show()
}
if *clearListFlag {
err := os.Remove(appConf.SchemesListFile)
if err == nil {
fmt.Printf("Deleted cached colorscheme list %s\n", appConf.SchemesListFile)
} else {
fmt.Fprintf(os.Stderr, "Error deleting cached colorscheme list: %v\n", err)
}
err = os.Remove(appConf.TemplatesListFile)
if err == nil {
fmt.Printf("Deleted cached template list %s\n", appConf.TemplatesListFile)
} else {
fmt.Fprintf(os.Stderr, "Error deleting cached template list: %v\n", err)
}
}
if *clearSchemesFlag {
err := os.RemoveAll(appConf.SchemesCachePath)
if err == nil {
fmt.Printf("Deleted cached colorscheme list %s\n", appConf.SchemesCachePath)
} else {
fmt.Fprintf(os.Stderr, "Error deleting cached colorschemes: %v\n", err)
}
}
if *clearTemplatesFlag {
err := os.RemoveAll(appConf.TemplatesCachePath)
if err == nil {
fmt.Printf("Deleted cached templates %s\n", appConf.TemplatesCachePath)
} else {
fmt.Fprintf(os.Stderr, "Error deleting cached templates: %v\n", err)
}
}
// Create cache paths, if missing
os.MkdirAll(appConf.SchemesCachePath, os.ModePerm)
os.MkdirAll(appConf.TemplatesCachePath, os.ModePerm)
schemeList := LoadBase16ColorschemeList()
templateList := LoadBase16TemplateList()
if *updateFlag {
schemeList.UpdateSchemes()
templateList.UpdateTemplates()
}
listing := false
if *listSchemesFlag {
schemeList.Print()
listing = true
}
if *listTemplatesFlag {
templateList.Print()
listing = true
}
if *listTemplateFilesFlag != "" {
templ := templateList.Find(*listTemplateFilesFlag)
fmt.Printf("Files for %v:\n",templ.Name)
for fk := range templ.Files {
fmt.Println(" - ",fk)
}
listing = true
}
if listing {
//exit after listing
os.Exit(0)
}
schemename := appConf.Colorscheme
if *schemeFlag != "" {
schemename = *schemeFlag
}
scheme := schemeList.Find(schemename)
fmt.Println("[CONFIG]: Selected scheme: ", scheme.Name)
templateEnabled := false
for app, appConfig := range appConf.Applications {
if appConfig.Enabled {
Base16Render(templateList.Find(app), scheme)
templateEnabled = true
}
}
if !templateEnabled {
fmt.Println("No templates enabled")
}
}
// Base16Render takes an application-specific template and renders a config file
// implementing the provided colorscheme.
func Base16Render(templ Base16Template, scheme Base16Colorscheme) {
fmt.Println("[RENDER]: Rendering template \"" + templ.Name + "\"")
for k, v := range templ.Files {
var before, after string
templFileData, err := DownloadFileToStirng(templ.RawBaseURL + "templates/" + k + ".mustache")
check(err)
configPath := appConf.Applications[templ.Name].Files[k]
if configPath == "" {
fmt.Println(" - skipping file because it is not configured: ", k)
continue
}
renderedFile := mustache.Render(templFileData, scheme.MustacheContext())
savePath := filepath.Join(configPath, k + v.Extension)
if stat, err := os.Stat(configPath); err == nil && ! stat.IsDir() {
//if the file exists and is a File write to it directly
savePath = configPath
}
os.MkdirAll(filepath.Dir(savePath), os.ModePerm)
mode := appConf.Applications[templ.Name].Mode
//If DryRun is enabled, just print the output location for debugging
if appConf.DryRun {
fmt.Println(" - (dryrun) file would be written to: ", savePath)
} else {
fmt.Fprintf(os.Stdout, " - %ving %v in: %v\n", mode, k, savePath)
start_line := tagline(templ, k, "start")
end_line := tagline(templ, k, "end")
//read file for append and replace mode
write_start, write_end := false, false
if mode == "append" || mode == "replace" {
write_start = true
file, err := os.Open(savePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not read file for %v mode: %v\n",mode, err)
os.Exit(1)
}
state := "before"
scanner := bufio.NewScanner(file)
for scanner.Scan() {
switch state {
case "before":
if scanner.Text() == start_line {
state = "between"
}else{
before += scanner.Text() + "\n"
}
case "between":
if scanner.Text() == end_line {
state = "after"
after = ""
} else {
after += scanner.Text() + "\n"
}
case "after":
after += scanner.Text() + "\n"
}
}
file.Close()
//consistency check
switch mode {
case "replace":
if state == "before" {
fmt.Fprintf(os.Stderr, "For replace mode you have to add the following as a single line in target file:\n%v", start_line)
os.Exit(1)
} else if after != "" {
write_end = true
}
case "append":
//discard everything after start tag
after = ""
}
}
//write file out
saveFile, err := os.Create(savePath)
defer saveFile.Close()
check(err)
saveFile.Write([]byte(before))
if write_start { saveFile.Write([]byte(start_line + "\n")) }
saveFile.Write([]byte(renderedFile))
if write_end { saveFile.Write([]byte(end_line + "\n")) }
saveFile.Write([]byte(after))
saveFile.Close()
}
}
if appConf.DryRun {
fmt.Println("Not running hook, DryRun enabled: ", appConf.Applications[templ.Name].Hook)
} else {
//allow string replacement
replacer := strings.NewReplacer(
"\\{", "{", //mark escaped curly braces
"{template}", templ.Name,
"{scheme}", scheme.Name,
)
exe_cmd(replacer.Replace(appConf.Applications[templ.Name].Hook))
for k:= range(appConf.Applications[templ.Name].Hooks) {
exe_cmd(replacer.Replace(appConf.Applications[templ.Name].Hooks[k]))
}
}
}
//TODO proper error handling
func check(e error) {
if e != nil {
panic(e)
}
}
func tagline(templ Base16Template, file string, part string) string{
return appConf.Applications[templ.Name].Comment_Prefix + templ.Name + "-" + file + "-" + part
}