@@ -3,41 +3,65 @@ package config
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "io/ioutil"
6
7
"os"
7
8
8
9
"github.com/jessevdk/go-flags"
9
10
log "github.com/sirupsen/logrus"
11
+ "gopkg.in/yaml.v2"
10
12
)
11
13
12
14
// Config stores the handler's configuration and UI interface parameters
13
15
type Config struct {
14
16
Version bool `short:"V" long:"version" description:"Display version."`
15
17
16
- Port int `short:"p" long:"port" description:"Port to listen on." default:"8080"`
18
+ Port int `short:"p" long:"port" yaml:"port" description:"Port to listen on." default:"8080"`
19
+
20
+ ConfigFilePath string `long:"config-file" env:"CONFIG_FILE" description:"Config File path" default:""`
17
21
18
22
Log struct {
19
- Level string `short:"l" long:"log-level" description:"Set log level ('debug', 'info', 'warn', 'error', 'fatal', 'panic')." env:"TERRABOARD_LOG_LEVEL" default:"info"`
20
- Format string `long:"log-format" description:"Set log format ('plain', 'json')." env:"TERRABOARD_LOG_FORMAT" default:"plain"`
21
- } `group:"Logging Options"`
23
+ Level string `short:"l" long:"log-level" yaml:"level" description:"Set log level ('debug', 'info', 'warn', 'error', 'fatal', 'panic')." env:"TERRABOARD_LOG_LEVEL" default:"info"`
24
+ Format string `long:"log-format" yaml:"format" description:"Set log format ('plain', 'json')." env:"TERRABOARD_LOG_FORMAT" default:"plain"`
25
+ } `group:"Logging Options" yaml:"log" `
22
26
23
27
DB struct {
24
- Host string `long:"db-host" env:"DB_HOST" description:"Database host." default:"db"`
25
- User string `long:"db-user" env:"DB_USER" description:"Database user." default:"gorm"`
26
- Password string `long:"db-password" env:"DB_PASSWORD" description:"Database password."`
27
- Name string `long:"db-name" env:"DB_NAME" description:"Database name." default:"gorm"`
28
- NoSync bool `long:"no-sync" description:"Do not sync database."`
29
- } `group:"Database Options"`
30
-
31
- S3 struct {
32
- Bucket string `long:"s3-bucket" env:"AWS_BUCKET" description:"AWS S3 bucket."`
33
- DynamoDBTable string `long:"dynamodb-table" env:"AWS_DYNAMODB_TABLE" description:"AWS DynamoDB table for locks."`
34
- KeyPrefix string `long:"key-prefix" env:"AWS_KEY_PREFIX" description:"AWS Key Prefix."`
35
- FileExtension string `long:"file-extension" env:"AWS_FILE_EXTENSION" description:"File extension of state files." default:".tfstate"`
36
- } `group:"AWS S3 Options"`
37
-
38
- Authentication struct {
39
- LogoutURL string `long:"logout-url" env:"TERRABOARD_LOGOUT_URL" description:"Logout URL."`
40
- } `group:"Authentication"`
28
+ Host string `long:"db-host" env:"DB_HOST" yaml:"host" description:"Database host." default:"db"`
29
+ User string `long:"db-user" env:"DB_USER" yaml:"user" description:"Database user." default:"gorm"`
30
+ Password string `long:"db-password" env:"DB_PASSWORD" yaml:"password" description:"Database password."`
31
+ Name string `long:"db-name" env:"DB_NAME" yaml:"name" description:"Database name." default:"gorm"`
32
+ NoSync bool `long:"no-sync" yaml:"no-sync" description:"Do not sync database."`
33
+ } `group:"Database Options" yaml:"database"`
34
+
35
+ AWS struct {
36
+ DynamoDBTable string `long:"dynamodb-table" env:"AWS_DYNAMODB_TABLE" yaml:"dynamodb-table" description:"AWS DynamoDB table for locks."`
37
+
38
+ S3 struct {
39
+ Bucket string `long:"s3-bucket" env:"AWS_BUCKET" yaml:"bucket" description:"AWS S3 bucket."`
40
+ KeyPrefix string `long:"key-prefix" env:"AWS_KEY_PREFIX" yaml:"key-prefix" description:"AWS Key Prefix."`
41
+ FileExtension string `long:"file-extension" env:"AWS_FILE_EXTENSION" yaml:"file-extension" description:"File extension of state files." default:".tfstate"`
42
+ } `group:"S3 Options" yaml:"s3"`
43
+ } `group:"AWS Options" yaml:"aws"`
44
+
45
+ Web struct {
46
+ BaseURL string `long:"base-url" env:"TERRABOARD_BASE_URL" yaml:"base-url" description:"Base URL." default:"/"`
47
+ LogoutURL string `long:"logout-url" env:"TERRABOARD_LOGOUT_URL" yaml:"logout-url" description:"Logout URL."`
48
+ } `group:"Web" yaml:"web"`
49
+ }
50
+
51
+ // LoadConfigFromYaml loads the config from config file
52
+ func (c * Config ) LoadConfigFromYaml (configFilePath string ) * Config {
53
+ fmt .Printf ("Loading config from %s\n " , c .ConfigFilePath )
54
+ yamlFile , err := ioutil .ReadFile (configFilePath )
55
+ if err != nil {
56
+ log .Printf ("yamlFile.Get err #%v " , err )
57
+ }
58
+
59
+ err = yaml .Unmarshal (yamlFile , c )
60
+ if err != nil {
61
+ log .Fatalf ("Unmarshal err: %v" , err )
62
+ }
63
+
64
+ return c
41
65
}
42
66
43
67
// LoadConfig loads the config from flags & environment
@@ -48,6 +72,15 @@ func LoadConfig(version string) *Config {
48
72
os .Exit (1 )
49
73
}
50
74
75
+ if c .ConfigFilePath != "" {
76
+ if _ , err := os .Stat (c .ConfigFilePath ); err == nil {
77
+ c .LoadConfigFromYaml (c .ConfigFilePath )
78
+ } else {
79
+ fmt .Printf ("File %s doesn't exists!\n " , c .ConfigFilePath )
80
+ os .Exit (1 )
81
+ }
82
+ }
83
+
51
84
if c .Version {
52
85
fmt .Printf ("Terraboard v%v\n " , version )
53
86
os .Exit (0 )
0 commit comments