2021-04-26 13:52:50 +02:00
|
|
|
package options
|
2018-09-07 19:41:06 +02:00
|
|
|
|
2018-09-12 15:46:43 +02:00
|
|
|
import (
|
2018-11-06 13:19:34 +02:00
|
|
|
"fmt"
|
2018-09-12 15:46:43 +02:00
|
|
|
"strings"
|
|
|
|
)
|
2018-09-07 19:41:06 +02:00
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
var presets map[string]urlOptions
|
2018-09-07 19:41:06 +02:00
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
func ParsePresets(presetStrs []string) error {
|
|
|
|
for _, presetStr := range presetStrs {
|
|
|
|
if err := parsePreset(presetStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePreset(presetStr string) error {
|
2018-09-07 19:41:06 +02:00
|
|
|
presetStr = strings.Trim(presetStr, " ")
|
|
|
|
|
|
|
|
if len(presetStr) == 0 || strings.HasPrefix(presetStr, "#") {
|
2018-11-06 13:19:34 +02:00
|
|
|
return nil
|
2018-09-07 19:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(presetStr, "=")
|
|
|
|
|
|
|
|
if len(parts) != 2 {
|
2018-11-06 13:19:34 +02:00
|
|
|
return fmt.Errorf("Invalid preset string: %s", presetStr)
|
2018-09-07 19:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
name := strings.Trim(parts[0], " ")
|
|
|
|
if len(name) == 0 {
|
2018-11-06 13:19:34 +02:00
|
|
|
return fmt.Errorf("Empty preset name: %s", presetStr)
|
2018-09-07 19:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
value := strings.Trim(parts[1], " ")
|
|
|
|
if len(value) == 0 {
|
2018-11-06 13:19:34 +02:00
|
|
|
return fmt.Errorf("Empty preset value: %s", presetStr)
|
2018-09-07 19:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
optsStr := strings.Split(value, "/")
|
|
|
|
|
2018-09-12 15:46:43 +02:00
|
|
|
opts, rest := parseURLOptions(optsStr)
|
|
|
|
|
|
|
|
if len(rest) > 0 {
|
2018-11-06 13:19:34 +02:00
|
|
|
return fmt.Errorf("Invalid preset value: %s", presetStr)
|
2018-09-12 15:46:43 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
if presets == nil {
|
|
|
|
presets = make(map[string]urlOptions)
|
|
|
|
}
|
|
|
|
presets[name] = opts
|
2018-11-06 13:19:34 +02:00
|
|
|
|
|
|
|
return nil
|
2018-09-12 15:46:43 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
func ValidatePresets() error {
|
|
|
|
var po ProcessingOptions
|
2018-09-12 15:46:43 +02:00
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
for name, opts := range presets {
|
|
|
|
if err := applyURLOptions(&po, opts); err != nil {
|
2018-11-06 13:19:34 +02:00
|
|
|
return fmt.Errorf("Error in preset `%s`: %s", name, err)
|
2018-09-12 15:46:43 +02:00
|
|
|
}
|
2018-09-07 19:41:06 +02:00
|
|
|
}
|
2018-11-06 13:19:34 +02:00
|
|
|
|
|
|
|
return nil
|
2018-09-07 19:41:06 +02:00
|
|
|
}
|