cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.65.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

cmd/pint/main.go

141lines · modecode

1package main
2
3import (
4 "fmt"
5 "log/slog"
6 "os"
7
8 "github.com/urfave/cli/v2"
9 "go.uber.org/automaxprocs/maxprocs"
10
11 "github.com/cloudflare/pint/internal/config"
12)
13
14const (
15 configFlag = "config"
16 logLevelFlag = "log-level"
17 enabledFlag = "enabled"
18 disabledFlag = "disabled"
19 offlineFlag = "offline"
20 noColorFlag = "no-color"
21 workersFlag = "workers"
22)
23
24var (
25 version = "unknown"
26 commit = "unknown"
27)
28
29func newApp() *cli.App {
30 return &cli.App{
31 Usage: "Prometheus rule linter/validator.",
32 Flags: []cli.Flag{
33 &cli.PathFlag{
34 Name: configFlag,
35 Aliases: []string{"c"},
36 Value: ".pint.hcl",
37 Usage: "Configuration file to use.",
38 },
39 &cli.IntFlag{
40 Name: workersFlag,
41 Aliases: []string{"w"},
42 Value: 10,
43 Usage: "Number of worker threads for running checks.",
44 },
45 &cli.StringFlag{
46 Name: logLevelFlag,
47 Aliases: []string{"l"},
48 Value: slog.LevelInfo.String(),
49 Usage: "Log level.",
50 },
51 &cli.BoolFlag{
52 Name: noColorFlag,
53 Aliases: []string{"n"},
54 Value: false,
55 Usage: "Disable output colouring.",
56 },
57 &cli.StringSliceFlag{
58 Name: disabledFlag,
59 Aliases: []string{"d"},
60 Value: cli.NewStringSlice(),
61 Usage: "List of checks to disable (example: promql/cost).",
62 },
63 &cli.StringSliceFlag{
64 Name: enabledFlag,
65 Aliases: []string{"e"},
66 Value: cli.NewStringSlice(),
67 Usage: "Only enable these checks (example: promql/cost).",
68 },
69 &cli.BoolFlag{
70 Name: offlineFlag,
71 Aliases: []string{"o"},
72 Value: false,
73 Usage: "Disable all check that send live queries to Prometheus servers.",
74 },
75 },
76 Commands: []*cli.Command{
77 versionCmd,
78 lintCmd,
79 ciCmd,
80 watchCmd,
81 configCmd,
82 parseCmd,
83 },
84 }
85}
86
87type actionMeta struct {
88 cfg config.Config
89 isOffline bool
90 workers int
91}
92
93func actionSetup(c *cli.Context) (meta actionMeta, err error) {
94 err = initLogger(c.String(logLevelFlag), c.Bool(noColorFlag))
95 if err != nil {
96 return meta, fmt.Errorf("failed to set log level: %w", err)
97 }
98
99 undo, err := maxprocs.Set()
100 defer undo()
101 if err != nil {
102 slog.Error("failed to set GOMAXPROCS", slog.Any("err", err))
103 }
104
105 meta.workers = c.Int(workersFlag)
106 if meta.workers < 1 {
107 return meta, fmt.Errorf("--%s flag must be > 0", workersFlag)
108 }
109
110 var fromFile bool
111 meta.cfg, fromFile, err = config.Load(c.Path(configFlag), c.IsSet(configFlag))
112 if err != nil {
113 return meta, fmt.Errorf("failed to load config file %q: %w", c.Path(configFlag), err)
114 }
115 if fromFile {
116 slog.Debug("Adding pint config to the parser exclude list", slog.String("path", c.Path(configFlag)))
117 meta.cfg.Parser.Exclude = append(meta.cfg.Parser.Exclude, c.Path(configFlag))
118 }
119
120 meta.cfg.SetDisabledChecks(c.StringSlice(disabledFlag))
121 enabled := c.StringSlice(enabledFlag)
122 if len(enabled) > 0 {
123 meta.cfg.Checks.Enabled = enabled
124 }
125
126 if c.Bool(offlineFlag) {
127 meta.isOffline = true
128 meta.cfg.DisableOnlineChecks()
129 }
130
131 return meta, nil
132}
133
134func main() {
135 app := newApp()
136 err := app.Run(os.Args)
137 if err != nil {
138 slog.Error("Execution completed with error(s)", slog.Any("err", err))
139 os.Exit(1)
140 }
141}