46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
// 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 {
|
|
Uploaders []UploaderConfig `yaml:"uploaders"`
|
|
}
|
|
|
|
// 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) {
|
|
f, err := os.Open(filename)
|
|
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
|
|
}
|