2024-03-27 14:20:45 +00:00
|
|
|
// Package config defines how to configure the tool
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/Luzifer/go_helpers/v2/fieldcollection"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// File represents the contents of a configuration file
|
|
|
|
File struct {
|
2024-04-01 11:02:07 +00:00
|
|
|
Uploaders map[string]UploaderConfig `yaml:"uploaders"`
|
2024-03-27 14:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UploaderConfig defines the settings for each uploader
|
|
|
|
UploaderConfig struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Type string `yaml:"type"`
|
|
|
|
Settings *fieldcollection.FieldCollection `yaml:"settings"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Load parses the configuration file from disk
|
|
|
|
func Load(filename string) (config File, err error) {
|
2024-04-01 11:02:07 +00:00
|
|
|
f, err := os.Open(filename) //#nosec G304 // Intended to open user-given config
|
2024-03-27 14:20:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return config, fmt.Errorf("opening config file: %w", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing config file (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
dec := yaml.NewDecoder(f)
|
|
|
|
dec.KnownFields(true)
|
|
|
|
if err = dec.Decode(&config); err != nil {
|
|
|
|
return config, fmt.Errorf("decoding config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|