summaryrefslogtreecommitdiff
path: root/go/internal
diff options
context:
space:
mode:
Diffstat (limited to 'go/internal')
-rw-r--r--go/internal/config/config.go29
-rw-r--r--go/internal/config/config_test.go24
2 files changed, 41 insertions, 12 deletions
diff --git a/go/internal/config/config.go b/go/internal/config/config.go
index 7d521f5..435cb29 100644
--- a/go/internal/config/config.go
+++ b/go/internal/config/config.go
@@ -13,31 +13,44 @@ const (
logFile = "gitlab-shell.log"
)
+type MigrationConfig struct {
+ Enabled bool `yaml:"enabled"`
+ Features []string `yaml:"features"`
+}
+
type Config struct {
RootDir string
- LogFile string `yaml:"log_file"`
- LogFormat string `yaml:"log_format"`
+ LogFile string `yaml:"log_file"`
+ LogFormat string `yaml:"log_format"`
+ Migration MigrationConfig `yaml:"migration"`
}
func New() (*Config, error) {
- cfg := Config{}
-
dir, err := os.Getwd()
if err != nil {
return nil, err
}
- cfg.RootDir = dir
- configBytes, err := ioutil.ReadFile(path.Join(cfg.RootDir, configFile))
+ return NewFromDir(dir)
+}
+
+func NewFromDir(dir string) (*Config, error) {
+ return newFromFile(path.Join(dir, configFile))
+}
+
+func newFromFile(filename string) (*Config, error) {
+ cfg := &Config{RootDir: path.Dir(filename)}
+
+ configBytes, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
- if err := parseConfig(configBytes, &cfg); err != nil {
+ if err := parseConfig(configBytes, cfg); err != nil {
return nil, err
}
- return &cfg, nil
+ return cfg, nil
}
// parseConfig expects YAML data in configBytes and a Config instance with RootDir set.
diff --git a/go/internal/config/config_test.go b/go/internal/config/config_test.go
index 0a5c842..87a582f 100644
--- a/go/internal/config/config_test.go
+++ b/go/internal/config/config_test.go
@@ -2,20 +2,28 @@ package config
import (
"fmt"
+ "strings"
"testing"
)
-func TestConfigLogFile(t *testing.T) {
+func TestParseConfig(t *testing.T) {
testRoot := "/foo/bar"
testCases := []struct {
- yaml string
- path string
- format string
+ yaml string
+ path string
+ format string
+ migration MigrationConfig
}{
{path: "/foo/bar/gitlab-shell.log", format: "text"},
{yaml: "log_file: my-log.log", path: "/foo/bar/my-log.log", format: "text"},
{yaml: "log_file: /qux/my-log.log", path: "/qux/my-log.log", format: "text"},
{yaml: "log_format: json", path: "/foo/bar/gitlab-shell.log", format: "json"},
+ {
+ yaml: "migration:\n enabled: true\n features:\n - foo\n - bar",
+ path: "/foo/bar/gitlab-shell.log",
+ format: "text",
+ migration: MigrationConfig{Enabled: true, Features: []string{"foo", "bar"}},
+ },
}
for _, tc := range testCases {
@@ -25,6 +33,14 @@ func TestConfigLogFile(t *testing.T) {
t.Fatal(err)
}
+ if cfg.Migration.Enabled != tc.migration.Enabled {
+ t.Fatalf("migration.enabled: expected %v, got %v", tc.migration.Enabled, cfg.Migration.Enabled)
+ }
+
+ if strings.Join(cfg.Migration.Features, ":") != strings.Join(tc.migration.Features, ":") {
+ t.Fatalf("migration.features: expected %#v, got %#v", tc.migration.Features, cfg.Migration.Features)
+ }
+
if cfg.LogFile != tc.path {
t.Fatalf("expected %q, got %q", tc.path, cfg.LogFile)
}