Skip to content

Commit

Permalink
clair: better argument error messages
Browse files Browse the repository at this point in the history
This replaces the custom flag values with just visiting the flags and
processing the string argument at that time.

Closes: quay#1605
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Jul 22, 2022
1 parent f4db261 commit ab6eadf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 78 deletions.
56 changes: 38 additions & 18 deletions cmd/clair/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,52 @@ const (

func main() {
// parse conf from cli
var (
confFile ConfValue
conf config.Config
runMode ConfMode
)
confFile.Set(os.Getenv(envConfig))
runMode.Set(os.Getenv(envMode))
flag.Var(&confFile, "conf", "The file system path to Clair's config file.")
flag.Var(&runMode, "mode", "The operation mode for this server.")
var conf config.Config
flag.String("conf", "", "The file system path to Clair's config file.")
flag.String("mode", "combo", "The operation mode for this server.")
flag.Parse()
if confFile.String() == "" {
golog.Fatalf("must provide a -conf flag or set %q in the environment", envConfig)
}
flag.VisitAll(func(f *flag.Flag) {
fv := f.Value.(flag.Getter).Get().(string)
var key string
switch f.Name {
case "conf":
key = envConfig
case "mode":
key = envMode
}
v, ok := os.LookupEnv(key)
if fv == "" && !ok {
golog.Fatalf("must provide a -%s value or set %q in the environment", f.Name, key)
}
if fv == "" && ok {
fv = v
}
switch f.Name {
case "conf":
var err error
cf, err := os.Open(fv)
if err != nil {
golog.Fatalf("failed to open config file: %v", err)
}
defer cf.Close()
if err := yaml.NewDecoder(cf).Decode(&conf); err != nil {
golog.Fatalf("failed to decode yaml config: %v", err)
}
case "mode":
m, err := config.ParseMode(fv)
if err != nil {
golog.Fatalf("bad mode %q: %v", fv, err)
}
conf.Mode = m
}
})
fail := false
defer func() {
if fail {
os.Exit(1)
}
}()

// validate config
err := yaml.NewDecoder(confFile.file).Decode(&conf)
if err != nil {
golog.Fatalf("failed to decode yaml config: %v", err)
}
conf.Mode = runMode.Mode
// Grab the warnings to print after the logger is configured.
ws, err := config.Validate(&conf)
if err != nil {
Expand Down
60 changes: 0 additions & 60 deletions cmd/clair/values.go

This file was deleted.

0 comments on commit ab6eadf

Please sign in to comment.