mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 05:40:11 +01:00
Factor command line parsing into an internal package for reuse.
This commit is contained in:
parent
27ec66353c
commit
95bd705036
6 changed files with 281 additions and 272 deletions
|
|
@ -11,19 +11,16 @@ package main
|
|||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/csaf-poc/csaf_distribution/v2/util"
|
||||
"github.com/jessevdk/go-flags"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/csaf-poc/csaf_distribution/v2/internal/options"
|
||||
)
|
||||
|
||||
const defaultPreset = "mandatory"
|
||||
|
||||
type config struct {
|
||||
Output string `short:"o" long:"output" description:"File name of the generated report" value-name:"REPORT-FILE" toml:"output"`
|
||||
Output string `short:"o" long:"output" description:"File name of the generated report" value-name:"REPORT-FILE" toml:"output"`
|
||||
//lint:ignore SA5008 We are using choice twice: json, html.
|
||||
Format string `short:"f" long:"format" choice:"json" choice:"html" description:"Format of report" default:"json" toml:"format"`
|
||||
Insecure bool `long:"insecure" description:"Do not check TLS certificates from provider" toml:"insecure"`
|
||||
ClientCert *string `long:"client-cert" description:"TLS client certificate file (PEM encoded data)" value-name:"CERT-FILE" toml:"client_cert"`
|
||||
|
|
@ -38,80 +35,38 @@ type config struct {
|
|||
RemoteValidatorCache string `long:"validatorcache" description:"FILE to cache remote validations" value-name:"FILE" toml:"validator_cache"`
|
||||
RemoteValidatorPresets []string `long:"validatorpreset" description:"One or more presets to validate remotely" default:"mandatory" toml:"validator_preset"`
|
||||
|
||||
Config *string `short:"c" long:"config" description:"Path to config TOML file" value-name:"TOML-FILE" toml:"-"`
|
||||
Config string `short:"c" long:"config" description:"Path to config TOML file" value-name:"TOML-FILE" toml:"-"`
|
||||
|
||||
clientCerts []tls.Certificate
|
||||
}
|
||||
|
||||
// parseArgsConfig parse the command arguments and loads configuration
|
||||
// from a configuration file.
|
||||
func parseArgsConfig() ([]string, *config, error) {
|
||||
cfg := &config{
|
||||
RemoteValidatorPresets: []string{"mandatory"},
|
||||
}
|
||||
|
||||
parser := flags.NewParser(cfg, flags.Default)
|
||||
parser.Usage = "[OPTIONS] domain..."
|
||||
args, err := parser.Parse()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if cfg.Version {
|
||||
fmt.Println(util.SemVersion)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if cfg.Config != nil {
|
||||
path, err := homedir.Expand(*cfg.Config)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := cfg.load(path); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
} else if path := findConfigFile(); path != "" {
|
||||
if err := cfg.load(path); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return args, cfg, nil
|
||||
}
|
||||
|
||||
// configPaths are the potential file locations of the the config file.
|
||||
// configPaths are the potential file locations of the config file.
|
||||
var configPaths = []string{
|
||||
"~/.config/csaf/checker.toml",
|
||||
"~/.csaf_checker.toml",
|
||||
"csaf_checker.toml",
|
||||
}
|
||||
|
||||
// findConfigFile looks for a file in the pre-defined paths in "configPaths".
|
||||
// The returned value will be the name of file if found, otherwise an empty string.
|
||||
func findConfigFile() string {
|
||||
for _, f := range configPaths {
|
||||
name, err := homedir.Expand(f)
|
||||
if err != nil {
|
||||
log.Printf("warn: %v\n", err)
|
||||
continue
|
||||
}
|
||||
if _, err := os.Stat(name); err == nil {
|
||||
return name
|
||||
}
|
||||
// parseArgsConfig parse the command arguments and loads configuration
|
||||
// from a configuration file.
|
||||
func parseArgsConfig() ([]string, *config, error) {
|
||||
p := options.Parser[config]{
|
||||
DefaultConfigLocations: configPaths,
|
||||
ConfigLocation: func(cfg *config) string {
|
||||
return cfg.Config
|
||||
},
|
||||
Usage: "[OPTIONS] domain...",
|
||||
SetDefaults: func(cfg *config) {
|
||||
cfg.RemoteValidatorPresets = []string{defaultPreset}
|
||||
},
|
||||
// Re-establish default values if not set.
|
||||
EnsureDefaults: func(cfg *config) {
|
||||
if cfg.RemoteValidatorPresets == nil {
|
||||
cfg.RemoteValidatorPresets = []string{defaultPreset}
|
||||
}
|
||||
},
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// load loads a configuration from file.
|
||||
func (cfg *config) load(path string) error {
|
||||
md, err := toml.DecodeFile(path, &cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if undecoded := md.Undecoded(); len(undecoded) != 0 {
|
||||
return fmt.Errorf("could not parse %q from %q", undecoded, path)
|
||||
}
|
||||
return nil
|
||||
return p.Parse()
|
||||
}
|
||||
|
||||
// protectedAccess returns true if we have client certificates or
|
||||
|
|
@ -139,13 +94,3 @@ func (cfg *config) prepare() error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// errCheck checks if err is not nil and terminates the program if so.
|
||||
func errCheck(err error) {
|
||||
if err != nil {
|
||||
if flags.WroteHelp(err) {
|
||||
os.Exit(0)
|
||||
}
|
||||
log.Fatalf("error: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue