Improve configuration error checking (fix panic)

A panic would occur if a field in the config file (e.g. youtube-dl.youtube-dl-path) was not set
This commit is contained in:
r4 2022-09-23 15:46:18 +02:00
parent 7bdfcf9776
commit c8f9832f48
2 changed files with 19 additions and 13 deletions

View File

@ -97,14 +97,20 @@ func macosEnableExecutable(filename string) error {
// configuration file does not exist or is invalid. // configuration file does not exist or is invalid.
func Load(filename string) (*Config, error) { func Load(filename string) (*Config, error) {
cfg := &Config{} cfg := &Config{}
_, err := toml.DecodeFile(filename, cfg) meta, err := toml.DecodeFile(filename, cfg)
if err != nil { if err != nil {
if pe, ok := err.(toml.ParseError); ok {
fmt.Println(pe.ErrorWithUsage())
}
return nil, err return nil, err
} }
if undec := meta.Undecoded(); len(undec) > 0 {
return nil, fmt.Errorf("%v: field '%v' could not be decoded", filename, undec[0])
}
if cfg.Token == defaultToken || cfg.Token == "" { if cfg.Token == defaultToken || cfg.Token == "" {
return nil, ErrTokenNotSet return nil, ErrTokenNotSet
} }
if err := cfg.Extractors.CheckTypes(); err != nil { if err := cfg.Extractors.CheckValidity(); err != nil {
return nil, err return nil, err
} }
if _, err := exec.LookPath(cfg.Extractors["youtube-dl"]["youtube-dl-path"].(string)); err != nil { if _, err := exec.LookPath(cfg.Extractors["youtube-dl"]["youtube-dl-path"].(string)); err != nil {

View File

@ -22,7 +22,7 @@ var (
) )
func Extract(cfg Config, input string) ([]Data, error) { func Extract(cfg Config, input string) ([]Data, error) {
if err := cfg.CheckTypes(); err != nil { if err := cfg.CheckValidity(); err != nil {
return nil, err return nil, err
} }
for _, e := range extractors { for _, e := range extractors {
@ -45,7 +45,7 @@ func Extract(cfg Config, input string) ([]Data, error) {
} }
func Search(cfg Config, input string) ([]Data, error) { func Search(cfg Config, input string) ([]Data, error) {
if err := cfg.CheckTypes(); err != nil { if err := cfg.CheckValidity(); err != nil {
return nil, err return nil, err
} }
for _, s := range searchers { for _, s := range searchers {
@ -59,7 +59,7 @@ func Search(cfg Config, input string) ([]Data, error) {
} }
func Suggest(cfg Config, input string) ([]string, error) { func Suggest(cfg Config, input string) ([]string, error) {
if err := cfg.CheckTypes(); err != nil { if err := cfg.CheckValidity(); err != nil {
return nil, err return nil, err
} }
for _, s := range suggestors { for _, s := range suggestors {
@ -115,16 +115,16 @@ func DefaultConfig() Config {
} }
} }
func (cfg Config) CheckTypes() error { func (cfg Config) CheckValidity() error {
for provider, pCfg := range cfg { for chkProvider, chkCfg := range DefaultConfig() {
if pCfg == nil { if _, ok := cfg[chkProvider]; !ok {
return fmt.Errorf("extractor config for %v is nil", provider) return fmt.Errorf("extractor config for %v is nil", chkProvider)
} }
for k, v := range pCfg { for k, v := range chkCfg {
got, expected := reflect.TypeOf(v), reflect.TypeOf(DefaultConfig()[provider][k]) expected, got := reflect.TypeOf(v), reflect.TypeOf(cfg[chkProvider][k])
if got != expected { if got != expected {
return &ConfigTypeError{ return &ConfigTypeError{
Provider: provider, Provider: chkProvider,
Key: k, Key: k,
Expected: expected, Expected: expected,
Got: got, Got: got,
@ -151,7 +151,7 @@ func (e *ConfigTypeError) Error() string {
if e.Got != nil { if e.Got != nil {
gotName = e.Got.Name() gotName = e.Got.Name()
} }
return "extractor config type error: " + e.Provider + "." + e.Key + ": expected " + expectedName + " but got " + gotName return "invalid extractor configuration: " + e.Provider + "." + e.Key + ": expected " + expectedName + " but got " + gotName
} }
type ProviderConfig map[string]any type ProviderConfig map[string]any