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