-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
211 lines (180 loc) · 5.65 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
package main
import (
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/OpenPeeDeeP/xdg"
"github.com/hoisie/mustache"
"gopkg.in/alecthomas/kingpin.v2"
)
// Configuration file
var configFile string
var xdgDirs = xdg.New("base16-universal-manager", "")
// Flags
var (
updateFlag = kingpin.Flag("update-list", "Update the list of templates and colorschemes").Bool()
clearListFlag = kingpin.Flag("clear-list", "Delete local template and colorscheme list caches").Bool()
clearTemplatesFlag = kingpin.Flag("clear-templates", "Delete local template caches").Bool()
clearSchemesFlag = kingpin.Flag("clear-schemes", "Delete local schemes caches").Bool()
configFileFlag = kingpin.Flag("config", "Specify configuration file to use").Default(xdgDirs.QueryConfig("config.yaml")).String()
printConfigFlag = kingpin.Flag("print-config", "Print current configuration").Bool()
schemeFlag = kingpin.Flag("scheme", "Specify scheme to use (Overrides config)").String()
)
// Configuration
var appConf SetterConfig
func main() {
//Parse Flags
kingpin.Version("1.0.0")
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()
}
var scheme Base16Colorscheme
if *schemeFlag == "" {
// Scheme from config
scheme = schemeList.Find(appConf.Colorscheme)
} else {
// Scheme from flag
scheme = schemeList.Find(*schemeFlag)
}
fmt.Println("[CONFIG]: Selected scheme: ", scheme.Name)
templateEnabled := false
for app, appConfig := range appConf.Applications {
if appConfig.Template == "" {
appConfig.Template = app
}
if appConfig.Enabled {
err := Base16Render(templateList.Find(appConfig.Template), scheme, app)
if err != nil {
fmt.Fprintf(os.Stderr, "Error when rendering file: %v\n", err)
}
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, app string) error {
fmt.Println("[RENDER]: Rendering template \"" + templ.Name + "\"")
for k, v := range templ.Files {
templFileData, err := DownloadFileToString(templ.RawBaseURL + "templates/" + k + ".mustache")
if err != nil {
return fmt.Errorf("could not download template file: %w", err)
}
renderedFile := mustache.Render(templFileData, scheme.MustacheContext(v.Extension))
savePath, err := getSavePath(appConf.Applications[app].Files[k].Path, k+v.Extension)
if err != nil {
return fmt.Errorf("could not get location for save path: %w", err)
}
if savePath == "" {
continue
}
//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[app].Files[k].Mode {
case "rewrite":
fmt.Println(" - writing: ", savePath)
if err = WriteFile(savePath, []byte(renderedFile)); err != nil {
return err
}
case "replace":
fmt.Println(" - replacing in: ", savePath)
startMarker := appConf.Applications[app].Files[k].StartMarker
endMarker := appConf.Applications[app].Files[k].EndMarker
if err = ReplaceMultiline(savePath, renderedFile, startMarker, endMarker); err != nil {
return err
}
}
}
}
if appConf.DryRun {
fmt.Println("Not running hook, DryRun enabled: ", appConf.Applications[app].Hook)
} else {
exe_cmd(appConf.Applications[app].Hook)
}
return nil
}
func getSavePath(path, defaultFilename string) (string, error) {
if path == "" {
return "", nil
}
var homeDir string
if path[0] == '~' {
usr, err := user.Current()
if err != nil {
return "", fmt.Errorf("could not get user home directory: %w", err)
}
homeDir = usr.HomeDir
}
var savePath string
if path == "~" {
savePath = homeDir
} else if strings.HasPrefix(path, "~/") {
savePath = filepath.Join(homeDir, path[2:])
} else if path[0] != '/' {
savePath = filepath.Join(".", path)
} else {
savePath = path
}
if strings.HasSuffix(path, "/") {
err := os.MkdirAll(savePath, os.ModePerm)
if err != nil {
return "", fmt.Errorf("could not create folder: %w", err)
}
savePath = filepath.Join(savePath, defaultFilename)
}
return savePath, nil
}
// TODO proper error handling
func check(e error) {
if e != nil {
panic(e)
}
}