2019-06-17 09:48:05 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-08-17 16:04:03 +00:00
|
|
|
"github.com/traefik/paerser/cli"
|
|
|
|
"github.com/traefik/paerser/file"
|
|
|
|
"github.com/traefik/paerser/flag"
|
2019-06-17 09:48:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FileLoader loads a configuration from a file.
|
|
|
|
type FileLoader struct {
|
|
|
|
ConfigFileFlag string
|
|
|
|
filename string
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFilename returns the configuration file if any.
|
|
|
|
func (f *FileLoader) GetFilename() string {
|
|
|
|
return f.filename
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads the command's configuration from a file either specified with the -traefik.configfile flag, or from default locations.
|
2020-08-17 16:04:03 +00:00
|
|
|
func (f *FileLoader) Load(args []string, cmd *cli.Command) (bool, error) {
|
2019-06-17 09:48:05 +00:00
|
|
|
ref, err := flag.Parse(args, cmd.Configuration)
|
|
|
|
if err != nil {
|
2019-12-02 16:34:06 +00:00
|
|
|
_ = cmd.PrintHelp(os.Stdout)
|
2019-06-17 09:48:05 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
configFileFlag := "traefik.configfile"
|
2019-08-05 13:22:03 +00:00
|
|
|
if _, ok := ref["traefik.configFile"]; ok {
|
|
|
|
configFileFlag = "traefik.configFile"
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
if f.ConfigFileFlag != "" {
|
2019-08-05 13:22:03 +00:00
|
|
|
configFileFlag = "traefik." + f.ConfigFileFlag
|
|
|
|
if _, ok := ref[strings.ToLower(configFileFlag)]; ok {
|
|
|
|
configFileFlag = "traefik." + strings.ToLower(f.ConfigFileFlag)
|
|
|
|
}
|
2019-06-17 09:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
configFile, err := loadConfigFiles(ref[configFileFlag], cmd.Configuration)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.filename = configFile
|
|
|
|
|
|
|
|
if configFile == "" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Printf("Configuration loaded from file: %s", configFile)
|
2019-06-17 09:48:05 +00:00
|
|
|
|
2021-03-04 19:08:03 +00:00
|
|
|
content, _ := os.ReadFile(configFile)
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Debug().Str("configFile", configFile).Bytes("content", content).Send()
|
2019-06-17 09:48:05 +00:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadConfigFiles tries to decode the given configuration file and all default locations for the configuration file.
|
|
|
|
// It stops as soon as decoding one of them is successful.
|
|
|
|
func loadConfigFiles(configFile string, element interface{}) (string, error) {
|
2020-08-17 16:04:03 +00:00
|
|
|
finder := cli.Finder{
|
2019-06-17 09:48:05 +00:00
|
|
|
BasePaths: []string{"/etc/traefik/traefik", "$XDG_CONFIG_HOME/traefik", "$HOME/.config/traefik", "./traefik"},
|
|
|
|
Extensions: []string{"toml", "yaml", "yml"},
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath, err := finder.Find(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(filePath) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:03 +00:00
|
|
|
if err := file.Decode(filePath, element); err != nil {
|
2019-06-17 09:48:05 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filePath, nil
|
|
|
|
}
|