diff options
Diffstat (limited to 'server/config')
-rw-r--r-- | server/config/config.go | 46 |
1 files changed, 38 insertions, 8 deletions
diff --git a/server/config/config.go b/server/config/config.go index 7598f0a..fae823b 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -1,16 +1,25 @@ package config import ( + "errors" "io" + "github.com/hashicorp/go-multierror" "github.com/spf13/viper" ) -// Config holds the values from the json config file. +// Config holds the server configuration. type Config struct { - Server Server `mapstructure:"server"` - Auth Auth `mapstructure:"auth"` - SSH SSH `mapstructure:"ssh"` + Server `mapstructure:"server"` + Auth `mapstructure:"auth"` + SSH `mapstructure:"ssh"` +} + +// unmarshalled holds the raw config. +type unmarshalled struct { + Server []Server `mapstructure:"server"` + Auth []Auth `mapstructure:"auth"` + SSH []SSH `mapstructure:"ssh"` } // Server holds the configuration specific to the web server and sessions. @@ -39,16 +48,37 @@ type SSH struct { Permissions []string `mapstructure:"permissions"` } +func verifyConfig(u *unmarshalled) error { + var err error + if len(u.SSH) == 0 { + err = multierror.Append(errors.New("missing ssh config block")) + } + if len(u.Auth) == 0 { + err = multierror.Append(errors.New("missing auth config block")) + } + if len(u.Server) == 0 { + err = multierror.Append(errors.New("missing server config block")) + } + return err +} + // ReadConfig parses a JSON configuration file into a Config struct. func ReadConfig(r io.Reader) (*Config, error) { - config := &Config{} + u := &unmarshalled{} v := viper.New() - v.SetConfigType("json") + v.SetConfigType("hcl") if err := v.ReadConfig(r); err != nil { return nil, err } - if err := v.Unmarshal(config); err != nil { + if err := v.Unmarshal(u); err != nil { + return nil, err + } + if err := verifyConfig(u); err != nil { return nil, err } - return config, nil + return &Config{ + Server: u.Server[0], + Auth: u.Auth[0], + SSH: u.SSH[0], + }, nil } |