diff options
author | Niall Sheridan <nsheridan@gmail.com> | 2016-05-29 14:46:06 +0100 |
---|---|---|
committer | Niall Sheridan <nsheridan@gmail.com> | 2016-05-29 14:46:06 +0100 |
commit | 1a16ff15b87a10b2e7359ba53104ed4bd9fef9e8 (patch) | |
tree | 1f6ea2155189713110bf05dc62af4a560152917c /server/config | |
parent | 2d76d6028d75dda1d239d228d47679b5b35ee1e1 (diff) | |
parent | 46ec48845704b54d395727441edc049b009da774 (diff) |
Merge pull request #10 from nsheridan/hcl
Switch from json to hcl configs
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 } |