-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
118 lines (97 loc) · 2.58 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
package main
import (
"compress/gzip"
"embed"
"flag"
"io/fs"
"net/http"
"os"
"github.com/nlepage/go-tarfs"
"github.com/sirupsen/logrus"
)
//go:embed schemes/*/*.yaml
var schemesFS embed.FS
var (
schemesDir string
templateDir string
onlineMode bool
// Define the logger we'll be using
logVerbose bool
rawLog = logrus.New()
log = logrus.NewEntry(rawLog)
// Variables set by goreleaser
version = "dev"
commit = "unknown"
date = "unknown"
specVersion = "0.11.0"
)
func init() {
flag.StringVar(&schemesDir, "schemes-dir", "-", "Target directory for scheme data. The default value uses internal schemes.")
flag.StringVar(&templateDir, "template-dir", ".", "Target template directory to build.")
flag.BoolVar(&logVerbose, "verbose", false, "Log all debug messages")
flag.BoolVar(&onlineMode, "online", false, "Run in online mode and pull schemes directly from GitHub")
rawLog.Level = logrus.InfoLevel
if logVerbose {
rawLog.Level = logrus.DebugLevel
}
}
func getSchemesFromGithub() (fs.FS, error) {
log.Info("Attempting to load schemes from GitHub")
r, err := http.Get("https://github.com/tinted-theming/schemes/archive/refs/heads/spec-0.11.tar.gz")
if err != nil {
return nil, err
}
gzipReader, err := gzip.NewReader(r.Body)
if err != nil {
return nil, err
}
targetFS, err := tarfs.New(gzipReader)
if err != nil {
return nil, err
}
// The archive has a subfolder containing all the schemes, so we return a
// subfs of the folder.
return fs.Sub(targetFS, "schemes-spec-0.11")
}
func main() {
var err error
flag.Parse()
log.WithFields(logrus.Fields{
"version": version,
"commit": commit,
"date": date,
"spec-version": specVersion,
}).Info("base16-builder-go")
var targetFS fs.FS
if schemesDir == "-" {
// If we're in online mode, the default is to pull from GitHub,
// otherwise use the embedded schemes.
if onlineMode {
targetFS, err = getSchemesFromGithub()
if err != nil {
log.WithError(err).Fatal("Failed to load schemes from GitHub")
}
} else {
log.Info("Using internal schemes")
targetFS, _ = fs.Sub(schemesFS, "schemes")
}
} else {
log.Infof("Processing scheme dir %q", schemesDir)
targetFS = os.DirFS(schemesDir)
}
colorSchemes, ok := loadSchemes(targetFS)
if !ok {
log.Fatal("Failed to load color schemes")
}
log.Infof("Found %d color schemes", len(colorSchemes))
templates, err := templatesFromFile(templateDir)
if err != nil {
log.Panic(err)
}
for _, template := range templates {
err = template.Render(colorSchemes)
if err != nil {
log.Panic(err)
}
}
}