oauth2_proxy/pkg/apis/options/options.go
2019-06-08 22:52:59 +02:00

71 lines
1.6 KiB
Go

package options
import (
"fmt"
"io"
"strings"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
)
// Options contains configuration options for the OAuth2 Proxy
type Options struct {
Cookie *CookieOptions
}
// New creates a new deafulted copy of the Options struct
func New() *Options {
options := &Options{}
err := defaultStruct(options)
if err != nil {
// If we get an error here, there must be a code error
panic(err)
}
return options
}
// Load reads a config file, flag arguments and the environment to set the
// correct configuration options
func Load(config io.Reader, configType string, args []string) (*Options, error) {
flagSet := flag.NewFlagSet("oauth2-proxy", flag.ExitOnError)
// Add FlagSets to main flagSet
flagSet.AddFlagSet(cookieFlagSet)
flagSet.Parse(args)
// Create a viper for binding config
v := viper.New()
// Bind flags to viper
err := v.BindPFlags(flagSet)
if err != nil {
return nil, err
}
// Configure loading of environment variables
// All flag options are prefixed by the EnvPrefix
v.SetEnvPrefix("OAUTH2_PROXY_")
// Substitute "-" for "_" so `FOO_BAR` matches the flag `foo-bar`
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
v.AutomaticEnv()
// Read the configuration file
if config != nil {
if configType == "" {
return nil, fmt.Errorf("config-type not set")
}
v.SetConfigType(configType)
v.ReadConfig(config)
}
// Unmarhsal the config into the Options struct
options := New()
err = v.Unmarshal(&options)
if err != nil {
return nil, fmt.Errorf("error unmarshalling config: %v", err)
}
return options, nil
}