aboutsummaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorNiall Sheridan <nsheridan@gmail.com>2016-05-29 02:13:00 +0100
committerNiall Sheridan <nsheridan@gmail.com>2016-05-29 13:59:57 +0100
commit591aa261d17e60619708b48b312b5db6ed64df10 (patch)
tree58dc4970f16ef7c33574ba915ee1c069e57977f8 /server
parent5cbf84c566f648dd7e54a2fdea1b645ef96627b1 (diff)
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.
Diffstat (limited to 'server')
-rw-r--r--server/config/config.go46
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
}