forked from lbi-bot/letsblockit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (58 loc) · 1.43 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
package main
import (
"fmt"
"io"
"os"
"github.com/alecthomas/kong"
"github.com/letsblockit/letsblockit/data"
"github.com/letsblockit/letsblockit/src/filters"
"gopkg.in/yaml.v3"
)
// Alias outputs to allow capturing them in tests
var (
stdout io.Writer = os.Stdout
stderr io.Writer = os.Stderr
)
type renderCmd struct {
Strict bool `help:"validate the input data before rendering the output"`
Input string `default:"-" help:"input file to use, defaults to stdin" arg:"" type:"existingfile"`
}
type logger struct{}
func (l *logger) Warnf(format string, args ...interface{}) {
if _, err := fmt.Fprintf(stderr, "WARNING: "+format+"\n", args...); err != nil {
panic(err)
}
}
func (c *renderCmd) Run() error {
var err error
var input io.Reader
if c.Input == "-" {
input = os.Stdin
} else {
input, err = os.Open(c.Input)
if err != nil {
return fmt.Errorf("cannot open input file: %w", err)
}
}
repo, err := filters.Load(data.Templates, data.Presets)
if err != nil {
return fmt.Errorf("cannot load filter templates: %w", err)
}
var list filters.List
err = yaml.NewDecoder(input).Decode(&list)
if err != nil {
return fmt.Errorf("cannot decode input file: %w", err)
}
if c.Strict {
err = list.Validate()
if err != nil {
return fmt.Errorf("invalid input data: %w", err)
}
}
return list.Render(stdout, &logger{}, repo)
}
func main() {
cmd := &renderCmd{}
k := kong.Parse(cmd)
k.FatalIfErrorf(cmd.Run())
}