kairos-sdk/collector/options.go

83 lines
1.5 KiB
Go
Raw Permalink Normal View History

2023-06-14 11:01:06 +00:00
package collector
import (
"fmt"
"io"
)
2023-06-14 11:01:06 +00:00
type Options struct {
ScanDir []string
BootCMDLineFile string
MergeBootCMDLine bool
NoLogs bool
StrictValidation bool
Readers []io.Reader
Overwrites string
2023-06-14 11:01:06 +00:00
}
type Option func(o *Options) error
var NoLogs Option = func(o *Options) error {
o.NoLogs = true
return nil
}
// SoftErr prints a warning if err is no nil and NoLogs is not true.
// It's use to wrap the same handling happening in multiple places.
//
// TODO: Switch to a standard logging library (e.g. verbose, silent mode etc).
func (o *Options) SoftErr(message string, err error) {
if !o.NoLogs && err != nil {
fmt.Printf("WARNING: %s, %s\n", message, err.Error())
}
}
func (o *Options) Apply(opts ...Option) error {
for _, oo := range opts {
if err := oo(o); err != nil {
return err
}
}
return nil
}
var MergeBootLine = func(o *Options) error {
o.MergeBootCMDLine = true
return nil
}
func WithBootCMDLineFile(s string) Option {
return func(o *Options) error {
o.BootCMDLineFile = s
return nil
}
}
func StrictValidation(v bool) Option {
return func(o *Options) error {
o.StrictValidation = v
return nil
}
}
func Directories(d ...string) Option {
return func(o *Options) error {
o.ScanDir = d
return nil
}
}
func Readers(r ...io.Reader) Option {
return func(o *Options) error {
o.Readers = r
return nil
}
}
func Overwrites(m string) Option {
return func(o *Options) error {
o.Overwrites = m
return nil
}
}