traefik/pkg/cli/loader_env.go

31 lines
910 B
Go
Raw Normal View History

package cli
import (
"fmt"
"os"
2019-06-24 14:40:06 +00:00
"strings"
2019-08-03 01:58:23 +00:00
"github.com/containous/traefik/v2/pkg/config/env"
"github.com/containous/traefik/v2/pkg/log"
)
// 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.
func (e *EnvLoader) Load(_ []string, cmd *Command) (bool, error) {
2019-06-21 08:08:04 +00:00
vars := env.FindPrefixedEnvVars(os.Environ(), env.DefaultNamePrefix, cmd.Configuration)
if len(vars) == 0 {
return false, nil
}
2019-06-21 08:08:04 +00:00
if err := env.Decode(vars, env.DefaultNamePrefix, cmd.Configuration); err != nil {
2019-06-24 14:40:06 +00:00
log.WithoutContext().Debug("environment variables", strings.Join(vars, ", "))
2020-05-11 10:06:07 +00:00
return false, fmt.Errorf("failed to decode configuration from environment variables: %w ", err)
}
log.WithoutContext().Println("Configuration loaded from environment variables.")
return true, nil
}