diff --git a/config/config.go b/config/config.go index bf302454..ee0ee6bd 100644 --- a/config/config.go +++ b/config/config.go @@ -11,47 +11,61 @@ import ( "gopkg.in/yaml.v2" ) +// LogConfig stores the log configuration +type LogConfig struct { + Level string `short:"l" long:"log-level" env:"TERRABOARD_LOG_LEVEL" yaml:"level" description:"Set log level ('debug', 'info', 'warn', 'error', 'fatal', 'panic')." default:"info"` + Format string `long:"log-format" yaml:"format" env:"TERRABOARD_LOG_FORMAT" description:"Set log format ('plain', 'json')." default:"plain"` +} + +// DBConfig stores the database configuration +type DBConfig struct { + Host string `long:"db-host" env:"DB_HOST" yaml:"host" description:"Database host." default:"db"` + Port uint16 `long:"db-port" env:"DB_PORT" yaml:"port" description:"Database port." default:"5432"` + User string `long:"db-user" env:"DB_USER" yaml:"user" description:"Database user." default:"gorm"` + Password string `long:"db-password" env:"DB_PASSWORD" yaml:"password" description:"Database password."` + Name string `long:"db-name" env:"DB_NAME" yaml:"name" description:"Database name." default:"gorm"` + NoSync bool `long:"no-sync" yaml:"no-sync" description:"Do not sync database."` +} + +// S3BucketConfig stores the S3 bucket configuration +type S3BucketConfig struct { + Bucket string `long:"s3-bucket" env:"AWS_BUCKET" yaml:"bucket" description:"AWS S3 bucket."` + KeyPrefix string `long:"key-prefix" env:"AWS_KEY_PREFIX" yaml:"key-prefix" description:"AWS Key Prefix."` + FileExtension string `long:"file-extension" env:"AWS_FILE_EXTENSION" yaml:"file-extension" description:"File extension of state files." default:".tfstate"` +} + +// AWSConfig stores the DynamoDB table and S3 Bucket configuration +type AWSConfig struct { + DynamoDBTable string `long:"dynamodb-table" env:"AWS_DYNAMODB_TABLE" yaml:"dynamodb-table" description:"AWS DynamoDB table for locks."` + S3 S3BucketConfig `group:"S3 Options" yaml:"s3"` +} + +// WebConfig stores the UI interface parameters +type WebConfig struct { + Port uint16 `short:"p" long:"port" env:"TERRABOARD_PORT" yaml:"port" description:"Port to listen on." default:"8080"` + BaseURL string `long:"base-url" env:"TERRABOARD_BASE_URL" yaml:"base-url" description:"Base URL." default:"/"` + LogoutURL string `long:"logout-url" env:"TERRABOARD_LOGOUT_URL" yaml:"logout-url" description:"Logout URL."` +} + // Config stores the handler's configuration and UI interface parameters type Config struct { Version bool `short:"V" long:"version" description:"Display version."` ConfigFilePath string `short:"c" long:"config-file" env:"CONFIG_FILE" description:"Config File path"` - Log struct { - Level string `short:"l" long:"log-level" env:"TERRABOARD_LOG_LEVEL" yaml:"level" description:"Set log level ('debug', 'info', 'warn', 'error', 'fatal', 'panic')." default:"info"` - Format string `long:"log-format" yaml:"format" env:"TERRABOARD_LOG_FORMAT" description:"Set log format ('plain', 'json')." default:"plain"` - } `group:"Logging Options" yaml:"log"` - - DB struct { - Host string `long:"db-host" env:"DB_HOST" yaml:"host" description:"Database host." default:"db"` - Port string `long:"db-port" env:"DB_PORT" yaml:"port" description:"Database port." default:"5432"` - User string `long:"db-user" env:"DB_USER" yaml:"user" description:"Database user." default:"gorm"` - Password string `long:"db-password" env:"DB_PASSWORD" yaml:"password" description:"Database password."` - Name string `long:"db-name" env:"DB_NAME" yaml:"name" description:"Database name." default:"gorm"` - NoSync bool `long:"no-sync" yaml:"no-sync" description:"Do not sync database."` - } `group:"Database Options" yaml:"database"` - - AWS struct { - DynamoDBTable string `long:"dynamodb-table" env:"AWS_DYNAMODB_TABLE" yaml:"dynamodb-table" description:"AWS DynamoDB table for locks."` - - S3 struct { - Bucket string `long:"s3-bucket" env:"AWS_BUCKET" yaml:"bucket" description:"AWS S3 bucket."` - KeyPrefix string `long:"key-prefix" env:"AWS_KEY_PREFIX" yaml:"key-prefix" description:"AWS Key Prefix."` - FileExtension string `long:"file-extension" env:"AWS_FILE_EXTENSION" yaml:"file-extension" description:"File extension of state files." default:".tfstate"` - } `group:"S3 Options" yaml:"s3"` - } `group:"AWS Options" yaml:"aws"` - - Web struct { - Port int `short:"p" long:"port" env:"TERRABOARD_PORT" yaml:"port" description:"Port to listen on." default:"8080"` - BaseURL string `long:"base-url" env:"TERRABOARD_BASE_URL" yaml:"base-url" description:"Base URL." default:"/"` - LogoutURL string `long:"logout-url" env:"TERRABOARD_LOGOUT_URL" yaml:"logout-url" description:"Logout URL."` - } `group:"Web" yaml:"web"` + Log LogConfig `group:"Logging Options" yaml:"log"` + + DB DBConfig `group:"Database Options" yaml:"database"` + + AWS AWSConfig `group:"AWS Options" yaml:"aws"` + + Web WebConfig `group:"Web" yaml:"web"` } // LoadConfigFromYaml loads the config from config file -func (c *Config) LoadConfigFromYaml(configFilePath string) *Config { +func (c *Config) LoadConfigFromYaml() *Config { fmt.Printf("Loading config from %s\n", c.ConfigFilePath) - yamlFile, err := ioutil.ReadFile(configFilePath) + yamlFile, err := ioutil.ReadFile(c.ConfigFilePath) if err != nil { log.Printf("yamlFile.Get err #%v ", err) } @@ -74,7 +88,7 @@ func LoadConfig(version string) *Config { if c.ConfigFilePath != "" { if _, err := os.Stat(c.ConfigFilePath); err == nil { - c.LoadConfigFromYaml(c.ConfigFilePath) + c.LoadConfigFromYaml() } else { fmt.Printf("File %s doesn't exists!\n", c.ConfigFilePath) os.Exit(1) diff --git a/config/config_test.go b/config/config_test.go index 9bb47da5..3c724a13 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,10 +1,46 @@ package config import ( + "testing" + log "github.com/sirupsen/logrus" ) -import "testing" +func TestLoadConfigFromYaml(t *testing.T) { + compareConfig := Config{ + Log: LogConfig{ + Level: "error", + Format: "json", + }, + ConfigFilePath: "config_test.yml", + DB: DBConfig{ + Host: "postgres", + Port: 15432, + User: "terraboard-user", + Password: "terraboard-pass", + Name: "terraboard-db", + NoSync: true, + }, + AWS: AWSConfig{ + DynamoDBTable: "terraboard-dynamodb", + S3: S3BucketConfig{ + Bucket: "terraboard-bucket", + KeyPrefix: "test/", + FileExtension: ".tfstate", + }, + }, + Web: WebConfig{ + Port: 39090, + BaseURL: "/test/", + LogoutURL: "/test-logout", + }, + } + c := Config{ConfigFilePath: "config_test.yml"} + c.LoadConfigFromYaml() + if c != compareConfig { + t.Fatalf("Expected: %v\nGot: %v", compareConfig, c) + } +} func TestSetLogging_debug(t *testing.T) { c := Config{} diff --git a/config/config_test.yml b/config/config_test.yml new file mode 100644 index 00000000..28b82c87 --- /dev/null +++ b/config/config_test.yml @@ -0,0 +1,23 @@ +log: + level: error + format: json + +database: + host: postgres + port: 15432 + user: terraboard-user + password: terraboard-pass + name: terraboard-db + no-sync: true + +aws: + dynamodb-table: terraboard-dynamodb + s3: + bucket: terraboard-bucket + key-prefix: test/ + file-extension: .tfstate + +web: + port: 39090 + base-url: /test/ + logout-url: /test-logout diff --git a/db/db.go b/db/db.go index cdebd745..d4cfd4b2 100644 --- a/db/db.go +++ b/db/db.go @@ -25,9 +25,9 @@ type Database struct { var pageSize = 20 // Init setups up the Database and a pointer to it -func Init(host, port, user, dbname, password, logLevel string) *Database { +func Init(host string, port uint16, user, dbname, password, logLevel string) *Database { var err error - connString := fmt.Sprintf("host=%s port=%s user=%s dbname=%s sslmode=disable password=%s", host, port, user, dbname, password) + connString := fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable password=%s", host, port, user, dbname, password) db, err := gorm.Open("postgres", connString) if err != nil { log.Fatal(err)