2019-06-17 09:48:05 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-06-24 14:40:06 +00:00
|
|
|
"strings"
|
2019-06-17 09:48:05 +00:00
|
|
|
|
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/env"
|
2019-06-17 09:48:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// EnvLoader loads a configuration from all the environment variables prefixed with "TRAEFIK_".
|
|
|
|
type EnvLoader struct{}
|
|
|
|
|
|
|
|
// Load loads the command's configuration from the environment variables.
|
2020-08-17 16:04:03 +00:00
|
|
|
func (e *EnvLoader) Load(_ []string, cmd *cli.Command) (bool, error) {
|
2019-06-21 08:08:04 +00:00
|
|
|
vars := env.FindPrefixedEnvVars(os.Environ(), env.DefaultNamePrefix, cmd.Configuration)
|
|
|
|
if len(vars) == 0 {
|
2019-06-17 09:48:05 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-06-21 08:08:04 +00:00
|
|
|
if err := env.Decode(vars, env.DefaultNamePrefix, cmd.Configuration); err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Debug().Msgf("environment variables: %s", strings.Join(vars, ", "))
|
|
|
|
return false, fmt.Errorf("failed to decode configuration from environment variables: %w", err)
|
2019-06-17 09:48:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Print("Configuration loaded from environment variables")
|
2019-06-17 09:48:05 +00:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|