-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
229 lines (188 loc) · 4.38 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
package main
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strings"
"time"
_ "embed"
"github.com/bmatcuk/doublestar/v4"
"github.com/hay-kot/flint/flint"
"github.com/urfave/cli/v2"
)
//go:embed example.yml
var example []byte
var (
version = "dev"
commit = "HEAD"
date = "unknown"
pattern = "flint.{yml,yaml,toml,json}"
)
// confResolver uses doublestar to find a config file in the current working directory
// should only be called if the provided path does not exist
//
// Example:
//
// p, _ := confResolver('/path/to/flint.yml')
func confResolver(absPath string) (path string, err error) {
fsys := os.DirFS(filepath.Dir(absPath))
r, err := doublestar.Glob(fsys, pattern)
if err != nil {
return "", err
}
if len(r) == 0 {
return "", fmt.Errorf("no config file found")
}
return r[0], nil
}
// pathResolver resolves the absolute path to the config file
func pathResolver(cwd string, path string) (string, error) {
if !filepath.IsAbs(path) {
path = filepath.Join(cwd, path)
}
if _, err := os.Stat(path); os.IsNotExist(err) {
match, err := confResolver(path)
if err != nil {
return "", err
}
return match, nil
}
return path, nil
}
func getCommit() string {
if commit == "" {
return "HEAD"
}
if len(commit) > 7 {
return commit[:7]
}
return commit
}
func main() {
app := &cli.App{
Name: "flint",
Version: fmt.Sprintf("%s (%s), built at %s", version, getCommit(), date),
Usage: "extensible frontmatter linter",
Flags: []cli.Flag{
&cli.PathFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "path to config file",
Value: "flint.yml",
},
&cli.BoolFlag{
Name: "debug",
Usage: "dumps debug information to stdout during run",
Hidden: true,
},
&cli.BoolFlag{
Name: "color",
Usage: "enable/disable color output",
Value: true,
},
},
Action: run,
Commands: []*cli.Command{
{
Name: "init",
Usage: "create a flint.yml file in the current working directory",
Action: initialize,
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func run(c *cli.Context) error {
var err error
var (
start = time.Now()
color = c.Bool("color")
debug = c.Bool("debug")
)
cwd := c.Args().Get(0)
if cwd == "" {
return errors.New("no path provided")
}
if err != nil {
return fmt.Errorf("failed to get current working directory: %w", err)
}
confpath, err := pathResolver(cwd, c.String("config"))
if err != nil {
return fmt.Errorf("failed to resolve config file: %w", err)
}
conffile, err := os.OpenFile(confpath, os.O_RDONLY, 0)
if err != nil {
return fmt.Errorf("failed to find config file: %w", err)
}
ext := filepath.Ext(confpath)
var format flint.ConfigFormat
switch ext {
case ".json":
format = flint.JSON
case ".toml":
format = flint.TOML
case ".yml", ".yaml":
format = flint.YAML
default:
return fmt.Errorf("unsupported config file format: %s", ext)
}
conf, err := flint.ReadConfig(conffile, format)
conffile.Close()
if err != nil {
return fmt.Errorf("failed to find config file: %w", err)
}
checked, err := conf.Run(cwd)
if err != nil {
switch {
case errors.As(err, &flint.FlintErrors{}):
errs := err.(flint.FlintErrors)
sorted := make([]string, len(errs))
i := 0
for k := range errs {
sorted[i] = k
i++
}
sort.Strings(sorted)
bldr := strings.Builder{}
for _, fp := range sorted {
bldr.WriteString(flint.FmtFileErrors(fp, errs[fp], flint.WithColor(color)))
}
fmt.Println(bldr.String())
default:
return fmt.Errorf("failed to run flint: %w", err)
}
} else {
if color {
fmt.Println(flint.StyleSuccess.Render("\n✓ No errors found"))
} else {
fmt.Println("\n✓ No errors found")
}
}
if color {
fmt.Println(flint.StyleLightGray.Render((fmt.Sprintf("✨ flint took %s\n", time.Since(start)))))
} else {
fmt.Printf("flint took %s\n", time.Since(start))
}
if debug {
fmt.Println("debug information:")
fmt.Printf(" config file: %s\n", confpath)
fmt.Printf(" files: %d\n", checked)
}
return nil
}
func initialize(c *cli.Context) error {
target := c.String("config")
if _, err := os.Stat(target); !os.IsNotExist(err) {
return fmt.Errorf("flint.yml already exists in current working directory")
}
err := os.WriteFile(target, example, 0x644)
if err != nil {
return fmt.Errorf("failed to create flint.yml: %w", err)
}
return nil
}