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