support for global flags

This commit is contained in:
cupcakearmy 2021-10-28 18:10:14 +02:00
parent fb6217d868
commit 440609220c
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
3 changed files with 29 additions and 7 deletions

View File

@ -22,10 +22,14 @@ var CI bool = false
var VERBOSE bool = false var VERBOSE bool = false
var CRON_LEAN bool = false var CRON_LEAN bool = false
type OptionMap map[string][]interface{}
type Options map[string]OptionMap
type Config struct { type Config struct {
Extras interface{} `yaml:"extras"` Extras interface{} `yaml:"extras"`
Locations map[string]Location `yaml:"locations"` Locations map[string]Location `yaml:"locations"`
Backends map[string]Backend `yaml:"backends"` Backends map[string]Backend `yaml:"backends"`
Global Options `yaml:"global"`
} }
var once sync.Once var once sync.Once
@ -233,23 +237,40 @@ func (c *Config) SaveConfig() error {
return viper.WriteConfig() return viper.WriteConfig()
} }
func getOptions(options Options, key string) []string { func optionToString(option string) string {
var selected []string if !strings.HasPrefix(option, "-") {
for k, values := range options[key] { return "--" + option
}
return option
}
func appendOptionsToSlice(str *[]string, options OptionMap) {
for key, values := range options {
for _, value := range values { for _, value := range values {
// Bool // Bool
asBool, ok := value.(bool) asBool, ok := value.(bool)
if ok && asBool { if ok && asBool {
selected = append(selected, fmt.Sprintf("--%s", k)) *str = append(*str, optionToString(key))
continue continue
} }
// String // String
asString, ok := value.(string) asString, ok := value.(string)
if ok { if ok {
selected = append(selected, fmt.Sprintf("--%s", k), asString) *str = append(*str, optionToString(key), asString)
continue continue
} }
} }
} }
}
func getOptions(options Options, key string) []string {
var selected []string
var keys = []string{"all"}
if key != "" {
keys = append(keys, key)
}
for _, key := range keys {
appendOptionsToSlice(&selected, options[key])
}
return selected return selected
} }

View File

@ -31,8 +31,6 @@ type Hooks struct {
Failure HookArray `yaml:"failure,omitempty"` Failure HookArray `yaml:"failure,omitempty"`
} }
type Options map[string]map[string][]interface{}
type Location struct { type Location struct {
name string `yaml:",omitempty"` name string `yaml:",omitempty"`
From string `yaml:"from,omitempty"` From string `yaml:"from,omitempty"`

View File

@ -53,6 +53,9 @@ func ExecuteCommand(options ExecuteOptions, args ...string) (string, error) {
func ExecuteResticCommand(options ExecuteOptions, args ...string) (string, error) { func ExecuteResticCommand(options ExecuteOptions, args ...string) (string, error) {
options.Command = RESTIC_BIN options.Command = RESTIC_BIN
var c = GetConfig()
var optionsAsString = getOptions(c.Global, "")
args = append(optionsAsString, args...)
return ExecuteCommand(options, args...) return ExecuteCommand(options, args...)
} }