-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgen.go
176 lines (157 loc) · 4.47 KB
/
gen.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
// Copyright (c) bwplotka/mimic Authors
// Licensed under the Apache License 2.0.
package mimic
import (
"errors"
"fmt"
"os"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"gopkg.in/alecthomas/kingpin.v2"
)
// Generator manages a pool of generated files.
type Generator struct {
FilePool
out string
generated bool
}
// New returns a new Generator that parses os.Args as command line arguments.
// It allows passing closure BEFORE parsing the flags to allow defining additional flags.
//
// NOTE: Read README.md before using. This is intentionally NOT following Go library patterns like:
// * It uses panics as the main error handling way.
// * It creates CLI command inside constructor.
// * It does not allow custom loggers etc
func New(injs ...func(cmd *kingpin.CmdClause)) *Generator {
app := kingpin.New("mimic", "mimic: https://github.com/bwplotka/mimic")
app.HelpFlag.Short('h')
gen := app.Command("generate", "generates output files from all registered files via Add method.")
out := gen.Flag("output", "output directory for generated files.").Short('o').Default("gen").String()
for _, inj := range injs {
inj(gen)
}
logLevel := app.Flag("log.level", "Log filtering level.").
Default("info").Enum("error", "warn", "info", "debug")
cmd, err := app.Parse(os.Args[1:])
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, fmt.Errorf("error parsing commandline arguments: %v", err))
app.Usage(os.Args[1:])
os.Exit(2)
}
var logger log.Logger
{
var lvl level.Option
switch *logLevel {
case "error":
lvl = level.AllowError()
case "warn":
lvl = level.AllowWarn()
case "info":
lvl = level.AllowInfo()
case "debug":
lvl = level.AllowDebug()
default:
panic("unexpected log level")
}
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = level.NewFilter(logger, lvl)
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
}
a := &Generator{out: *out}
switch cmd {
case gen.FullCommand():
a.FilePool = FilePool{Logger: logger, m: map[string]string{}}
return a
}
_ = level.Error(logger).Log("err", "command not found", "command", cmd)
os.Exit(2)
return nil
}
// With behaves like linux `cd` command. It allows to "walk" & organize output files in a desired way for ease of use.
// Example:
//
// ```
//
// gen := gen.With("mycompany.com", "production", "eu1", "kubernetes", "thanos")
//
// ```
// Giving the path `mycompany.com/production/eu1/kubernetes/thanos`.
//
// With return a Generator pointing at the specified path which can be specified even further:
// Example:
// ```
//
// gen := mimic.New()
// // gen/
// ...
// gen = gen.With('foo')
// // gen/foo
// ...
// {
// gen := gen.With('bar')
// // gen/foo/bar
// }
// // gen/foo
//
// ```
func (g *Generator) With(parts ...string) *Generator {
// TODO(bwplotka): Support "..", to get back?
return &Generator{
out: g.out,
FilePool: FilePool{
Logger: g.Logger,
path: append(g.path, parts...),
m: g.m,
topLevelComments: g.topLevelComments,
},
}
}
// WithTopLevelComment enables mimic to add any string as a header comment for a genenrated
// file. Follows same usage semantics as With().
//
// Example:
//
// gen := mimic.New()
// defer gen.Generate()
//
// gen.With("config").WithTopLevelComment(mimic.GeneratedComment).Add(name+".yaml", encoding.GhodssYAML(config))
//
// Like With, you can also chain multiple WithTopLevelComment(), which will be added to the top of a file in that order.
//
// Example:
//
// gen := mimic.New()
//
// defer gen.Generate()
// gen = gen.WithTopLevelComment("Foo.")
//
// gen.With("config").WithTopLevelComment("Bar.").Add(name+".yaml", encoding.GhodssYAML(config))
//
// This will result in a YAML file like,
//
// # Foo.
// # Bar.
// config:
// - prometheus:
//
// NOTE: This option will be noop for encodings that does not support comment (e.g. encoding.JSON).
func (g *Generator) WithTopLevelComment(content string) *Generator {
return &Generator{
out: g.out,
FilePool: FilePool{
Logger: g.Logger,
path: g.path,
m: g.m,
topLevelComments: append(g.topLevelComments, content),
},
}
}
// Generate generates the configuration files that have been defined and added to a generator.
func (g *Generator) Generate() {
if g.generated {
PanicErr(errors.New("generate method already invoked once"))
}
defer func() { g.generated = true }()
_ = level.Info(g.Logger).Log("msg", "generated output", "dir", g.out)
g.write(g.out)
}