Skip to content

Commit

Permalink
clairctl: use new cmd.LoadConfig
Browse files Browse the repository at this point in the history
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Jun 15, 2023
1 parent 3ff924a commit 06f5bc0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 12 deletions.
76 changes: 64 additions & 12 deletions cmd/clairctl/config.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,75 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"os"

"github.com/quay/clair/config"
"github.com/quay/zlog"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)

func loadConfig(n string) (*config.Config, error) {
f, err := os.Open(n)
if err != nil {
return nil, err
}
defer f.Close()
var CheckConfigCmd = &cli.Command{
Name: "check-config",
Usage: "print a fully-resolved clair config",
Description: `Check-config can be used to check that drop-in config files are being merged correctly.
The output is not currently suitable to be fed back into Clair.`,
Action: checkConfigAction,
ArgsUsage: "FILE[...]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "out",
Aliases: []string{"o"},
Usage: "output format: json, yaml",
Value: "json",
},
},
}

var cfg config.Config
if err := yaml.NewDecoder(f).Decode(&cfg); err != nil {
return nil, err
func checkConfigAction(c *cli.Context) error {
done := make(map[string]struct{})
todo := c.Args().Slice()
ctx := c.Context
var enc interface {
Encode(any) error
}
if len(todo) == 0 {
return errors.New("missing needed arguments")
}
Again:
switch v := c.String("out"); v {
case "json":
j := json.NewEncoder(os.Stdout)
j.SetIndent("", "\t")
enc = j
case "yaml":
zlog.Warn(ctx).Msg("some values do no round-trip the yaml encoder correctly -- make sure to consult the documentation")
y := yaml.NewEncoder(os.Stdout)
y.SetIndent(2)
enc = y
default:
zlog.Info(ctx).Str("out", v).Msg("unknown 'out' kind, using 'json'")
c.Set("out", "json")
goto Again
}
for _, f := range todo {
if _, ok := done[f]; ok {
continue
}
done[f] = struct{}{}
if len(todo) > 1 {
fmt.Println("#", f)
}
cfg, err := loadConfig(f)
if err != nil {
return err
}
if err := enc.Encode(cfg); err != nil {
return err
}
}
// Can't use validate, because we're not running in a server "mode".
return &cfg, nil
return nil
}
10 changes: 10 additions & 0 deletions cmd/clairctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"time"

"github.com/quay/clair/config"
_ "github.com/quay/claircore/updater/defaults"
"github.com/quay/zlog"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -55,6 +56,7 @@ func main() {
ExportCmd,
ImportCmd,
DeleteCmd,
CheckConfigCmd,
},
Flags: []cli.Flag{
&cli.BoolFlag{
Expand Down Expand Up @@ -93,3 +95,11 @@ func main() {

app.RunContext(ctx, os.Args)
}

func loadConfig(n string) (*config.Config, error) {
var cfg config.Config
if err := cmd.LoadConfig(&cfg, n, false); err != nil {
return nil, err
}
return &cfg, nil
}

0 comments on commit 06f5bc0

Please sign in to comment.