Skip to content

Commit 06f5bc0

Browse files
committed
clairctl: use new cmd.LoadConfig
Signed-off-by: Hank Donnay <[email protected]>
1 parent 3ff924a commit 06f5bc0

File tree

2 files changed

+74
-12
lines changed

2 files changed

+74
-12
lines changed

cmd/clairctl/config.go

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,75 @@
11
package main
22

33
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
47
"os"
58

6-
"github.com/quay/clair/config"
9+
"github.com/quay/zlog"
10+
"github.com/urfave/cli/v2"
711
"gopkg.in/yaml.v3"
812
)
913

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+
}
1631

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+
}
2073
}
21-
// Can't use validate, because we're not running in a server "mode".
22-
return &cfg, nil
74+
return nil
2375
}

cmd/clairctl/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"time"
77

8+
"github.com/quay/clair/config"
89
_ "github.com/quay/claircore/updater/defaults"
910
"github.com/quay/zlog"
1011
"github.com/rs/zerolog"
@@ -55,6 +56,7 @@ func main() {
5556
ExportCmd,
5657
ImportCmd,
5758
DeleteCmd,
59+
CheckConfigCmd,
5860
},
5961
Flags: []cli.Flag{
6062
&cli.BoolFlag{
@@ -93,3 +95,11 @@ func main() {
9395

9496
app.RunContext(ctx, os.Args)
9597
}
98+
99+
func loadConfig(n string) (*config.Config, error) {
100+
var cfg config.Config
101+
if err := cmd.LoadConfig(&cfg, n, false); err != nil {
102+
return nil, err
103+
}
104+
return &cfg, nil
105+
}

0 commit comments

Comments
 (0)