1
0
Fork 0
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:
Sascha L. Teichmann 2023-07-24 23:25:16 +02:00
parent 27ec66353c
commit 95bd705036
6 changed files with 281 additions and 272 deletions

View file

@ -9,15 +9,9 @@
package main
import (
"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 (
@ -43,106 +37,36 @@ type config struct {
Config string `short:"c" long:"config" description:"Path to config TOML file" value-name:"TOML-FILE" toml:"-"`
}
// 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/downloader.toml",
"~/.csaf_downloader.toml",
"csaf_downloader.toml",
}
// newConfig returns a new configuration.
func newConfig() *config {
return &config{
Worker: defaultWorker,
RemoteValidatorPresets: []string{defaultPreset},
}
}
// parseArgsConfig parses the command line and if need a config file.
func parseArgsConfig() ([]string, *config, error) {
// Parse the command line first.
cmdLineCfg := newConfig()
parser := flags.NewParser(cmdLineCfg, flags.Default)
parser.Usage = "[OPTIONS] domain..."
args, err := parser.Parse()
if err != nil {
return nil, nil, err
p := options.Parser[config]{
DefaultConfigLocations: configPaths,
ConfigLocation: func(cfg *config) string {
return cfg.Config
},
Usage: "[OPTIONS] domain...",
SetDefaults: func(cfg *config) {
cfg.Worker = defaultWorker
cfg.RemoteValidatorPresets = []string{defaultPreset}
},
// Re-establish default values if not set.
EnsureDefaults: func(cfg *config) {
if cfg.Worker == 0 {
cfg.Worker = defaultWorker
}
if cfg.RemoteValidatorPresets == nil {
cfg.RemoteValidatorPresets = []string{defaultPreset}
}
},
}
// Directly quit if the version flag was set.
if cmdLineCfg.Version {
fmt.Println(util.SemVersion)
os.Exit(0)
}
var path string
// Do we have a config file explicitly given by command line?
if cmdLineCfg.Config != "" {
path = cmdLineCfg.Config
} else {
path = findConfigFile()
}
// No config file -> We are good.
if path == "" {
return args, cmdLineCfg, nil
}
if path, err = homedir.Expand(path); err != nil {
return nil, nil, err
}
// Load the config file
fileCfg := &config{}
if err := fileCfg.load(path); err != nil {
return nil, nil, err
}
// Parse command line a second time to overwrite the
// loaded config at places where explicitly command line
// options where given.
args, err = flags.NewParser(fileCfg, flags.Default).Parse()
if err != nil {
return nil, nil, err
}
// Re-establish default values.
if fileCfg.Worker == 0 {
fileCfg.Worker = defaultWorker
}
if fileCfg.RemoteValidatorPresets == nil {
fileCfg.RemoteValidatorPresets = []string{defaultPreset}
}
return args, fileCfg, nil
}
// 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
}
// findConfigFile looks for a file in the pre-defined paths in "configPath".
// 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
}
}
return ""
return p.Parse()
}
// prepare prepares internal state of a loaded configuration.
@ -150,14 +74,3 @@ func (cfg *config) prepare() error {
// TODO: Implement me!
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)
}
}