Skip to content

Commit

Permalink
fix(config): missing default values with yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
hbollon committed May 25, 2022
1 parent e74c342 commit 3f31b1f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 39 deletions.
74 changes: 36 additions & 38 deletions config/config_test.go
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/jessevdk/go-flags"
log "github.com/sirupsen/logrus"
)

Expand All @@ -24,17 +25,22 @@ func TestSetLogging_debug(t *testing.T) {
}

func TestLoadConfig(t *testing.T) {
t.Skip("Skipping this test since go-flags can't parse properlly flags with go test command")

c := LoadConfig("1.0.0")
compareConfig := Config{
var tmpConfig configFlags
parser := flags.NewParser(&tmpConfig, flags.Default)
if _, err := parser.ParseArgs([]string{"--db-host=test", "--port=1234"}); err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
}
log.Fatalf("Failed to parse flags: %s", err)
}
compareConfig := configFlags{
Log: LogConfig{
Level: "info",
Format: "plain",
},
ConfigFilePath: "",
DB: DBConfig{
Host: "db",
Host: "test",
Port: 5432,
User: "gorm",
Password: "",
Expand All @@ -43,50 +49,42 @@ func TestLoadConfig(t *testing.T) {
NoSync: false,
SyncInterval: 1,
},
AWS: []AWSConfig{
{
AccessKey: "",
SecretAccessKey: "",
DynamoDBTable: "",
S3: []S3BucketConfig{{
Bucket: "",
KeyPrefix: "",
FileExtension: []string{".tfstate"},
ForcePathStyle: false,
}},
},
AWS: AWSConfig{
AccessKey: "",
SecretAccessKey: "",
DynamoDBTable: "",
},
TFE: []TFEConfig{
{
Address: "",
Token: "",
Organization: "",
},
S3: S3BucketConfig{
Bucket: "",
KeyPrefix: "",
FileExtension: []string{".tfstate"},
ForcePathStyle: false,
},
GCP: []GCPConfig{
{
GCSBuckets: nil,
GCPSAKey: "",
},
TFE: TFEConfig{
Address: "",
Token: "",
Organization: "",
},
Gitlab: []GitlabConfig{
{
Address: "https://gitlab.com",
Token: "",
},
GCP: GCPConfig{
GCSBuckets: nil,
GCPSAKey: "",
},
Gitlab: GitlabConfig{
Address: "https://gitlab.com",
Token: "",
},
Web: WebConfig{
Port: 8080,
Port: 1234,
SwaggerPort: 8081,
BaseURL: "/",
LogoutURL: "",
},
}

if !reflect.DeepEqual(*c, compareConfig) {
if !reflect.DeepEqual(tmpConfig, compareConfig) {
t.Errorf(
"TestLoadConfig() -> \n\ngot:\n%v,\n\nwant:\n%v",
spew.Sdump(*c),
spew.Sdump(tmpConfig),
spew.Sdump(compareConfig),
)
}
Expand All @@ -109,9 +107,9 @@ func TestLoadConfigFromYaml(t *testing.T) {
User: "terraboard-user",
Password: "terraboard-pass",
Name: "terraboard-db",
SSLMode: "",
SSLMode: "require",
NoSync: true,
SyncInterval: 0,
SyncInterval: 1,
},
AWS: []AWSConfig{
{
Expand Down
1 change: 0 additions & 1 deletion config/config_test.yml
Expand Up @@ -38,6 +38,5 @@ gitlab:

web:
port: 39090
swagger-port: 8081
base-url: /test/
logout-url: /test-logout
29 changes: 29 additions & 0 deletions config/yaml.go
Expand Up @@ -6,6 +6,35 @@ package config
* (and so makes them optional)
*********************************************/

func (s *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
type rawConfig Config
raw := rawConfig{
DB: DBConfig{
Host: "db",
Port: 5432,
User: "gorm",
Name: "gorm",
SSLMode: "require",
SyncInterval: 1,
},
Log: LogConfig{
Level: "info",
Format: "plain",
},
Web: WebConfig{
Port: 8080,
SwaggerPort: 8081,
BaseURL: "/",
},
}
if err := unmarshal(&raw); err != nil {
return err
}

*s = Config(raw)
return nil
}

func (s *S3BucketConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type rawS3BucketConfig S3BucketConfig
raw := rawS3BucketConfig{
Expand Down

0 comments on commit 3f31b1f

Please sign in to comment.