mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 05:40:11 +01:00
Fix version config and make aggreator use new command line parser.
This commit is contained in:
parent
8aed2c034e
commit
1d9969162f
5 changed files with 38 additions and 59 deletions
|
|
@ -18,15 +18,14 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
|
||||||
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
||||||
"github.com/csaf-poc/csaf_distribution/v2/csaf"
|
"github.com/csaf-poc/csaf_distribution/v2/csaf"
|
||||||
|
"github.com/csaf-poc/csaf_distribution/v2/internal/options"
|
||||||
"github.com/csaf-poc/csaf_distribution/v2/util"
|
"github.com/csaf-poc/csaf_distribution/v2/util"
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultConfigPath = "aggregator.toml"
|
|
||||||
defaultWorkers = 10
|
defaultWorkers = 10
|
||||||
defaultFolder = "/var/www"
|
defaultFolder = "/var/www"
|
||||||
defaultWeb = "/var/www/html"
|
defaultWeb = "/var/www/html"
|
||||||
|
|
@ -74,7 +73,8 @@ type config struct {
|
||||||
LockFile *string `toml:"lock_file"`
|
LockFile *string `toml:"lock_file"`
|
||||||
|
|
||||||
// Interim performs an interim scan.
|
// Interim performs an interim scan.
|
||||||
Interim bool `toml:"interim"`
|
Interim bool `short:"i" long:"interim" description:"Perform an interim scan" toml:"interim"`
|
||||||
|
Version bool `long:"version" description:"Display version of the binary" toml:"-"`
|
||||||
|
|
||||||
// InterimYears is numbers numbers of years to look back
|
// InterimYears is numbers numbers of years to look back
|
||||||
// for interim advisories. Less/equal zero means forever.
|
// for interim advisories. Less/equal zero means forever.
|
||||||
|
|
@ -90,11 +90,34 @@ type config struct {
|
||||||
// 'update_interval'.
|
// 'update_interval'.
|
||||||
UpdateInterval *string `toml:"update_interval"`
|
UpdateInterval *string `toml:"update_interval"`
|
||||||
|
|
||||||
|
Config string `short:"c" long:"config" description:"Path to config TOML file" value-name:"TOML-FILE" toml:"-"`
|
||||||
|
|
||||||
keyMu sync.Mutex
|
keyMu sync.Mutex
|
||||||
key *crypto.Key
|
key *crypto.Key
|
||||||
keyErr error
|
keyErr error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// configPaths are the potential file locations of the config file.
|
||||||
|
var configPaths = []string{
|
||||||
|
// TODO: Make symmetric to checker and downloader.
|
||||||
|
"aggregator.toml",
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
},
|
||||||
|
HasVersion: func(cfg *config) bool { return cfg.Version },
|
||||||
|
// Establish default values if not set.
|
||||||
|
EnsureDefaults: (*config).setDefaults,
|
||||||
|
}
|
||||||
|
return p.Parse()
|
||||||
|
}
|
||||||
|
|
||||||
// tooOldForInterims returns a function that tells if a given
|
// tooOldForInterims returns a function that tells if a given
|
||||||
// time is too old for the configured interims interval.
|
// time is too old for the configured interims interval.
|
||||||
func (c *config) tooOldForInterims() func(time.Time) bool {
|
func (c *config) tooOldForInterims() func(time.Time) bool {
|
||||||
|
|
@ -299,27 +322,3 @@ func (c *config) check() error {
|
||||||
|
|
||||||
return c.checkMirror()
|
return c.checkMirror()
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfig(path string) (*config, error) {
|
|
||||||
if path == "" {
|
|
||||||
path = defaultConfigPath
|
|
||||||
}
|
|
||||||
|
|
||||||
var cfg config
|
|
||||||
md, err := toml.DecodeFile(path, &cfg)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if undecoded := md.Undecoded(); len(undecoded) != 0 {
|
|
||||||
return nil, fmt.Errorf("could not parse %q from config.toml", undecoded)
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg.setDefaults()
|
|
||||||
|
|
||||||
if err := cfg.check(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &cfg, nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -15,17 +15,11 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/csaf-poc/csaf_distribution/v2/util"
|
"github.com/csaf-poc/csaf_distribution/v2/internal/options"
|
||||||
"github.com/gofrs/flock"
|
"github.com/gofrs/flock"
|
||||||
"github.com/jessevdk/go-flags"
|
"github.com/jessevdk/go-flags"
|
||||||
)
|
)
|
||||||
|
|
||||||
type options struct {
|
|
||||||
Config string `short:"c" long:"config" description:"File name of the configuration file" value-name:"CFG-FILE" default:"aggregator.toml"`
|
|
||||||
Version bool `long:"version" description:"Display version of the binary"`
|
|
||||||
Interim bool `short:"i" long:"interim" description:"Perform an interim scan"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func errCheck(err error) {
|
func errCheck(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if flags.WroteHelp(err) {
|
if flags.WroteHelp(err) {
|
||||||
|
|
@ -60,24 +54,9 @@ func lock(lockFile *string, fn func() error) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
opts := new(options)
|
_, cfg, err := parseArgsConfig()
|
||||||
|
options.ErrorCheck(err)
|
||||||
_, err := flags.Parse(opts)
|
options.ErrorCheck(cfg.check())
|
||||||
errCheck(err)
|
|
||||||
|
|
||||||
if opts.Version {
|
|
||||||
fmt.Println(util.SemVersion)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
interim := opts.Interim
|
|
||||||
|
|
||||||
cfg, err := loadConfig(opts.Config)
|
|
||||||
errCheck(err)
|
|
||||||
|
|
||||||
if interim {
|
|
||||||
cfg.Interim = true
|
|
||||||
}
|
|
||||||
|
|
||||||
p := processor{cfg: cfg}
|
p := processor{cfg: cfg}
|
||||||
errCheck(lock(cfg.LockFile, p.process))
|
errCheck(lock(cfg.LockFile, p.process))
|
||||||
|
|
|
||||||
|
|
@ -77,7 +77,8 @@ func parseArgsConfig() ([]string, *config, error) {
|
||||||
ConfigLocation: func(cfg *config) string {
|
ConfigLocation: func(cfg *config) string {
|
||||||
return cfg.Config
|
return cfg.Config
|
||||||
},
|
},
|
||||||
Usage: "[OPTIONS] domain...",
|
Usage: "[OPTIONS] domain...",
|
||||||
|
HasVersion: func(cfg *config) bool { return cfg.Version },
|
||||||
SetDefaults: func(cfg *config) {
|
SetDefaults: func(cfg *config) {
|
||||||
cfg.Format = defaultFormat
|
cfg.Format = defaultFormat
|
||||||
cfg.RemoteValidatorPresets = []string{defaultPreset}
|
cfg.RemoteValidatorPresets = []string{defaultPreset}
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,8 @@ func parseArgsConfig() ([]string, *config, error) {
|
||||||
ConfigLocation: func(cfg *config) string {
|
ConfigLocation: func(cfg *config) string {
|
||||||
return cfg.Config
|
return cfg.Config
|
||||||
},
|
},
|
||||||
Usage: "[OPTIONS] domain...",
|
Usage: "[OPTIONS] domain...",
|
||||||
|
HasVersion: func(cfg *config) bool { return cfg.Version },
|
||||||
SetDefaults: func(cfg *config) {
|
SetDefaults: func(cfg *config) {
|
||||||
cfg.Worker = defaultWorker
|
cfg.Worker = defaultWorker
|
||||||
cfg.RemoteValidatorPresets = []string{defaultPreset}
|
cfg.RemoteValidatorPresets = []string{defaultPreset}
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,12 @@
|
||||||
csaf_aggregator [OPTIONS]
|
csaf_aggregator [OPTIONS]
|
||||||
|
|
||||||
Application Options:
|
Application Options:
|
||||||
-c, --config=CFG-FILE File name of the configuration file (default:
|
-i, --interim Perform an interim scan
|
||||||
aggregator.toml)
|
--version Display version of the binary
|
||||||
--version Display version of the binary
|
-c, --config=TOML-FILE Path to config TOML file
|
||||||
-i, --interim Perform an interim scan
|
|
||||||
|
|
||||||
Help Options:
|
Help Options:
|
||||||
-h, --help Show this help message
|
-h, --help Show this help message
|
||||||
```
|
```
|
||||||
|
|
||||||
Usage example for a single run, to test if the config is good:
|
Usage example for a single run, to test if the config is good:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue