From 591aa261d17e60619708b48b312b5db6ed64df10 Mon Sep 17 00:00:00 2001 From: Niall Sheridan Date: Sun, 29 May 2016 02:13:00 +0100 Subject: Switch from json to hcl configs This is backward-compatible with the JSON config format - this is a non-breaking change. HCL treats config blocks as repeated fields so the config has to be unmarshalled into a struct comprised of []Server, []Auth, []SSH first. --- server/config/config.go | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) (limited to 'server/config') 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 } -- cgit v1.2.3