|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
4 | 7 | "os"
|
5 | 8 |
|
6 |
| - "github.com/quay/clair/config" |
| 9 | + "github.com/quay/zlog" |
| 10 | + "github.com/urfave/cli/v2" |
7 | 11 | "gopkg.in/yaml.v3"
|
8 | 12 | )
|
9 | 13 |
|
10 |
| -func loadConfig(n string) (*config.Config, error) { |
11 |
| - f, err := os.Open(n) |
12 |
| - if err != nil { |
13 |
| - return nil, err |
14 |
| - } |
15 |
| - defer f.Close() |
| 14 | +var CheckConfigCmd = &cli.Command{ |
| 15 | + Name: "check-config", |
| 16 | + Usage: "print a fully-resolved clair config", |
| 17 | + Description: `Check-config can be used to check that drop-in config files are being merged correctly. |
| 18 | +
|
| 19 | +The output is not currently suitable to be fed back into Clair.`, |
| 20 | + Action: checkConfigAction, |
| 21 | + ArgsUsage: "FILE[...]", |
| 22 | + Flags: []cli.Flag{ |
| 23 | + &cli.StringFlag{ |
| 24 | + Name: "out", |
| 25 | + Aliases: []string{"o"}, |
| 26 | + Usage: "output format: json, yaml", |
| 27 | + Value: "json", |
| 28 | + }, |
| 29 | + }, |
| 30 | +} |
16 | 31 |
|
17 |
| - var cfg config.Config |
18 |
| - if err := yaml.NewDecoder(f).Decode(&cfg); err != nil { |
19 |
| - return nil, err |
| 32 | +func checkConfigAction(c *cli.Context) error { |
| 33 | + done := make(map[string]struct{}) |
| 34 | + todo := c.Args().Slice() |
| 35 | + ctx := c.Context |
| 36 | + var enc interface { |
| 37 | + Encode(any) error |
| 38 | + } |
| 39 | + if len(todo) == 0 { |
| 40 | + return errors.New("missing needed arguments") |
| 41 | + } |
| 42 | +Again: |
| 43 | + switch v := c.String("out"); v { |
| 44 | + case "json": |
| 45 | + j := json.NewEncoder(os.Stdout) |
| 46 | + j.SetIndent("", "\t") |
| 47 | + enc = j |
| 48 | + case "yaml": |
| 49 | + zlog.Warn(ctx).Msg("some values do no round-trip the yaml encoder correctly -- make sure to consult the documentation") |
| 50 | + y := yaml.NewEncoder(os.Stdout) |
| 51 | + y.SetIndent(2) |
| 52 | + enc = y |
| 53 | + default: |
| 54 | + zlog.Info(ctx).Str("out", v).Msg("unknown 'out' kind, using 'json'") |
| 55 | + c.Set("out", "json") |
| 56 | + goto Again |
| 57 | + } |
| 58 | + for _, f := range todo { |
| 59 | + if _, ok := done[f]; ok { |
| 60 | + continue |
| 61 | + } |
| 62 | + done[f] = struct{}{} |
| 63 | + if len(todo) > 1 { |
| 64 | + fmt.Println("#", f) |
| 65 | + } |
| 66 | + cfg, err := loadConfig(f) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + if err := enc.Encode(cfg); err != nil { |
| 71 | + return err |
| 72 | + } |
20 | 73 | }
|
21 |
| - // Can't use validate, because we're not running in a server "mode". |
22 |
| - return &cfg, nil |
| 74 | + return nil |
23 | 75 | }
|
0 commit comments