-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
347 lines (307 loc) · 11 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright (c) Bartłomiej Płotka @bwplotka
// Licensed under the Apache License 2.0.
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"syscall"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/bwplotka/mdox/pkg/cache"
"github.com/bwplotka/mdox/pkg/clilog"
"github.com/bwplotka/mdox/pkg/extkingpin"
"github.com/bwplotka/mdox/pkg/mdformatter"
"github.com/bwplotka/mdox/pkg/mdformatter/linktransformer"
"github.com/bwplotka/mdox/pkg/mdformatter/mdgen"
"github.com/bwplotka/mdox/pkg/transform"
"github.com/bwplotka/mdox/pkg/version"
"github.com/charmbracelet/glamour"
"github.com/efficientgo/core/errcapture"
"github.com/efficientgo/core/logerrcapture"
extflag "github.com/efficientgo/tools/extkingpin"
"github.com/felixge/fgprof"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
)
const (
logFormatLogfmt = "logfmt"
logFormatJson = "json"
logFormatCLILog = "clilog"
cacheFile = ".mdoxcache"
)
func setupLogger(logLevel, logFormat string) 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")
}
switch logFormat {
case logFormatJson:
return level.NewFilter(log.NewJSONLogger(log.NewSyncWriter(os.Stderr)), lvl)
case logFormatLogfmt:
return level.NewFilter(log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)), lvl)
case logFormatCLILog:
fallthrough
default:
return level.NewFilter(clilog.New(log.NewSyncWriter(os.Stderr)), lvl)
}
}
func main() {
app := extkingpin.NewApp(kingpin.New(filepath.Base(os.Args[0]), `Markdown Project Documentation Toolbox.`).Version(version.Version))
logLevel := app.Flag("log.level", "Log filtering level.").
Default("info").Enum("error", "warn", "info", "debug")
logFormat := app.Flag("log.format", "Log format to use.").
Default(logFormatCLILog).Enum(logFormatLogfmt, logFormatJson, logFormatCLILog)
// Profiling and metrics.
profilesPath := app.Flag("profiles.path", "Path to directory where CPU and heap profiles will be saved; If empty, no profiling will be enabled.").ExistingDir()
metricsPath := app.Flag("metrics.path", "Path to directory where metrics are saved in OpenMetrics format; If empty, no metrics will be saved.").ExistingDir()
ctx, cancel := context.WithCancel(context.Background())
registerFmt(ctx, app, metricsPath)
registerTransform(ctx, app)
cmd, runner := app.Parse()
logger := setupLogger(*logLevel, *logFormat)
if *profilesPath != "" {
finalize, err := snapshotProfiles(*profilesPath)
if err != nil {
level.Error(logger).Log("err", fmt.Errorf("%s profiles init failed: %w", cmd, err))
os.Exit(1)
}
defer logerrcapture.Do(logger, finalize, "profiles")
}
var g run.Group
g.Add(func() error {
// TODO(bwplotka): Move to customized better setup function.
return runner(ctx, logger)
}, func(err error) {
cancel()
})
// Listen for termination signals.
{
cancel := make(chan struct{})
g.Add(func() error {
return interrupt(logger, cancel)
}, func(error) {
close(cancel)
})
}
if err := g.Run(); err != nil {
if *logLevel == "debug" {
// Use %+v to print with stack.
level.Error(logger).Log("msg", "command failed", "command", cmd, "err", fmt.Sprintf("%+v", err))
os.Exit(1)
}
level.Error(logger).Log("msg", "command failed", "command", cmd, "err", err)
os.Exit(1)
}
}
func snapshotProfiles(dir string) (func() error, error) {
now := time.Now().UTC()
if err := os.MkdirAll(filepath.Join(dir, strings.ReplaceAll(now.Format(time.UnixDate), " ", "_")), os.ModePerm); err != nil {
return nil, err
}
f, err := os.OpenFile(filepath.Join(dir, strings.ReplaceAll(now.Format(time.UnixDate), " ", "_"), "fgprof.pb.gz"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.ModePerm)
if err != nil {
return nil, err
}
m, err := os.OpenFile(filepath.Join(dir, strings.ReplaceAll(now.Format(time.UnixDate), " ", "_"), "memprof.pb.gz"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.ModePerm)
if err != nil {
return nil, err
}
runtime.GC()
if err := pprof.WriteHeapProfile(m); err != nil {
return nil, err
}
fgFunc := fgprof.Start(f, fgprof.FormatPprof)
return func() (err error) {
defer errcapture.Do(&err, f.Close, "close")
return fgFunc()
}, nil
}
// Dump metrics from registry into file in dir using OpenMetrics format.
func Dump(reg *prometheus.Registry, dir string) error {
mfs, err := reg.Gather()
if err != nil {
return err
}
now := time.Now().UTC()
if err := os.MkdirAll(filepath.Join(dir, strings.ReplaceAll(now.Format(time.UnixDate), " ", "_")), os.ModePerm); err != nil {
return err
}
f, err := os.OpenFile(filepath.Join(dir, strings.ReplaceAll(now.Format(time.UnixDate), " ", "_"), "metrics"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.ModePerm)
if err != nil {
return err
}
defer f.Close()
for _, mf := range mfs {
for _, metric := range mf.Metric {
unixTime := now.Unix()
metric.TimestampMs = &unixTime
}
if _, err := expfmt.MetricFamilyToOpenMetrics(f, mf); err != nil {
return err
}
}
if _, err = expfmt.FinalizeOpenMetrics(f); err != nil {
return err
}
return nil
}
func interrupt(logger log.Logger, cancel <-chan struct{}) error {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
select {
case s := <-c:
level.Info(logger).Log("msg", "caught signal. Exiting.", "signal", s)
return nil
case <-cancel:
return errors.New("canceled")
}
}
func registerFmt(_ context.Context, app *extkingpin.App, metricsPath *string) {
cmd := app.Command("fmt", "Formats in-place given markdown files uniformly following GFM (GitHub Flavored Markdown: https://github.github.com/gfm/). Example: mdox fmt *.md")
files := cmd.Arg("files", "Markdown file(s) to process.").Required().ExistingFiles()
checkOnly := cmd.Flag("check", "If true, fmt will not modify the given files, instead it will fail if files needs formatting").Bool()
softWraps := cmd.Flag("soft-wraps", "If true, fmt will preserve soft line breaks for given files").Bool()
codeFmt := cmd.Flag("code-fmt", "Reformat code snippets").Default("true").Bool()
disableGenCodeBlocksDirectives := cmd.Flag("code.disable-directives", `If false, fmt will parse custom fenced code directives prefixed with 'mdox-gen' to autogenerate code snippets. For example:
`+"```"+`<lang> mdox-exec="<executable + arguments>"
This directive runs executable with arguments and put its stderr and stdout output inside code block content, replacing existing one.`).Bool()
anchorDir := cmd.Flag("anchor-dir", "Anchor directory for all transformers. PWD is used if flag is not specified.").ExistingDir()
linksLocalizeForAddress := cmd.Flag("links.localize.address-regex", "If specified, all HTTP(s) links that target a domain and path matching given regexp will be transformed to relative to anchor dir path (if exists). "+
"Absolute path links will be converted to relative links to anchor dir as well.").Regexp()
linksValidateEnabled := cmd.Flag("links.validate", "If true, all links will be validated").Short('l').Bool()
linksValidateConfig := extflag.RegisterPathOrContent(cmd, "links.validate.config", "YAML file for skipping link check, with spec defined in github.com/bwplotka/mdox/pkg/linktransformer.ValidatorConfig", extflag.WithEnvSubstitution())
clearCache := cmd.Flag("cache.clear", "If true, entire cache database will be dropped and rebuilt when mdox is run. Useful in case cache needs to be cleared immediately from GitHub Actions or other CI runner cache.").Bool()
cmd.Run(func(ctx context.Context, logger log.Logger) (err error) {
var reg *prometheus.Registry
if *metricsPath != "" {
reg = prometheus.NewRegistry()
}
var opts []mdformatter.Option
if !*disableGenCodeBlocksDirectives {
opts = append(opts, mdformatter.WithCodeBlockTransformer(mdgen.NewCodeBlockTransformer()))
}
if *softWraps {
opts = append(opts, mdformatter.WithSoftWraps())
}
if *codeFmt {
opts = append(opts, mdformatter.WithCodeFmt())
}
if len(*files) == 0 {
return errors.New("no files to format")
}
for i := range *files {
(*files)[i], err = filepath.Abs((*files)[i])
if err != nil {
return err
}
}
anchorDir, err := validateAnchorDir(*anchorDir, *files)
if err != nil {
return err
}
var linkTr []mdformatter.LinkTransformer
if *linksValidateEnabled {
var storage *cache.SQLite3Storage
validateConfigContent, err := linksValidateConfig.Content()
if err != nil {
return err
}
storage = &cache.SQLite3Storage{
Filename: cacheFile,
ClearCache: *clearCache,
}
v, err := linktransformer.NewValidator(ctx, logger, validateConfigContent, anchorDir, storage, reg)
if err != nil {
return err
}
linkTr = append(linkTr, v)
}
if *linksLocalizeForAddress != nil {
linkTr = append(linkTr, linktransformer.NewLocalizer(logger, *linksLocalizeForAddress, anchorDir))
}
if len(linkTr) > 0 {
opts = append(opts, mdformatter.WithLinkTransformer(linktransformer.NewChain(linkTr...)))
}
opts = append(opts, mdformatter.WithMetrics(reg))
if *checkOnly {
diff, err := mdformatter.IsFormatted(ctx, logger, *files, opts...)
if err != nil {
return err
}
if len(diff) == 0 {
return nil
}
grender, err := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
glamour.WithWordWrap(100),
)
if err != nil {
return err
}
diffOut, err := grender.Render("\n```diff\n" + diff.String() + "\n```\n")
if err != nil {
return err
}
return fmt.Errorf("files not formatted: %v", diffOut)
}
if err := mdformatter.Format(ctx, logger, *files, opts...); err != nil {
return err
}
if reg != nil {
if err := Dump(reg, *metricsPath); err != nil {
return err
}
}
return nil
})
}
// validateAnchorDir returns validated anchor dir against files provided.
func validateAnchorDir(anchorDir string, files []string) (_ string, err error) {
if anchorDir == "" {
anchorDir, err = os.Getwd()
if err != nil {
return "", err
}
}
anchorDir, err = filepath.Abs(anchorDir)
if err != nil {
return "", err
}
// Check if provided files are within anchorDir way.
for _, f := range files {
if !strings.HasPrefix(f, anchorDir) {
return "", fmt.Errorf("anchorDir %q is not in path of provided file %q", anchorDir, f)
}
}
return anchorDir, nil
}
func registerTransform(_ context.Context, app *extkingpin.App) {
cmd := app.Command("transform", "Transform markdown files in various ways. For example pre-process markdown files to allow it for use for popular static HTML websites based on markdown source code and front matter options.")
cfg := extflag.RegisterPathOrContent(cmd, "config", "Path to the YAML file with spec defined in github.com/bwplotka/mdox/pkg/transform.Config", extflag.WithEnvSubstitution())
cmd.Run(func(ctx context.Context, logger log.Logger) error {
validateConfig, err := cfg.Content()
if err != nil {
return err
}
return transform.Dir(ctx, logger, validateConfig)
})
}