@@ -2,11 +2,13 @@ package config
2
2
3
3
import (
4
4
"fmt"
5
- "log"
6
5
"os"
7
6
"path"
7
+ "path/filepath"
8
+ "strings"
8
9
9
10
"github.com/aws/aws-sdk-go-v2/aws"
11
+ log "github.com/sirupsen/logrus"
10
12
"github.com/spf13/viper"
11
13
)
12
14
@@ -20,25 +22,27 @@ const (
20
22
appName = "REKA"
21
23
)
22
24
23
- // AWSConfig Related Configurations
24
- type AWSConfig struct {
25
+ // AwsConfig Related Configurations
26
+ type AwsConfig struct {
25
27
// AWS Configs
26
- Config aws.Config
27
- DefaultRegion string
28
+ Config aws.Config
29
+ AccessKey string `yaml:"accessKey"`
30
+ SecretAccessKey string `yaml:"secretAccessKey"`
31
+ DefaultRegion string `yaml:"defaultRegion"`
28
32
}
29
33
30
34
// DatabaseConfig Config for Dabatabase
31
35
type DatabaseConfig struct {
32
- Host string
33
- Name string //database name
34
- User string
35
- Password string
36
- Type string
36
+ Type string `yaml:"type"`
37
+ Name string `yaml:" name"`
38
+ Host string `yaml:"host"`
39
+ User string `yaml:"user"`
40
+ Password string `yaml:"password"`
37
41
}
38
42
39
43
// GetConnectionString the connection string for database
40
44
func (db * DatabaseConfig ) GetConnectionString () string {
41
- return fmt .Sprintf ("host=%s user=%s password=%s dbname=%s sslmode=disable" , config . db .Host , config . db .User , config . db .Password , config . db .Name )
45
+ return fmt .Sprintf ("host=%s user=%s password=%s dbname=%s sslmode=disable" , db .Host , db .User , db .Password , db .Name )
42
46
}
43
47
44
48
// SqliteDefaultPath the default database path to use for sqlite
@@ -48,21 +52,19 @@ func (db *DatabaseConfig) SqliteDefaultPath() string {
48
52
49
53
// Config : The Config values passed to application
50
54
type Config struct {
51
- // A list of supported providers to be enabled
52
- Providers []string
53
- staticPath string
54
-
55
- Aws * AWSConfig
56
- db * DatabaseConfig
57
-
58
- // Authentication details set from config
59
- Username string
60
- Password string
61
-
62
- // RefreshInterval in hours
63
- RefreshInterval int32
64
- // LogPath
65
- LogPath string
55
+ Name string `yaml:"name"`
56
+ Providers []string `yaml:"providers"`
57
+ Database * DatabaseConfig `yaml:"database"`
58
+ Aws * AwsConfig `yaml:"aws"`
59
+ RefreshInterval int32 `yaml:"refreshInterval"`
60
+ LogPath string `yaml:"logPath"`
61
+
62
+ Auth struct {
63
+ Username string `yaml:"username"`
64
+ Password string `yaml:"password"`
65
+ } `yaml:"auth"`
66
+
67
+ staticPath string // Path to Static File
66
68
}
67
69
68
70
// LoadConfig load all passed configs and defaults
@@ -72,51 +74,72 @@ func LoadConfig() *Config {
72
74
panic (err )
73
75
}
74
76
75
- viper .SetEnvPrefix (appName ) // will be uppercased automatically
76
- viper .SetConfigName ("reka" ) // name of config file (without extension)
77
- viper .SetConfigType ("yaml" ) // REQUIRED if the config file does not have the extension in the name
78
-
79
- // If REKA_CONFIG_FILE is set load config from that
80
- viper .AddConfigPath (viper .GetString ("ConfigFile" ))
81
- err = viper .ReadInConfig () // Find and read the config file
82
- if err != nil {
83
- // panic(fmt.Errorf("Fatal error config file: %s", err))
84
- fmt .Errorf ("Fatal error config file: %s" , err )
85
- }
86
-
77
+ viper .SetEnvPrefix (appName )
78
+ viper .AutomaticEnv () // Load Variables from Environment with REKA prefix
79
+ viper .SetEnvKeyReplacer (strings .NewReplacer ("." , "_" ))
87
80
// Defaults
88
81
viper .SetDefault ("StaticPath" , "web/static" )
89
- viper .SetDefault ("DbType" , "sqlite" ) // Default Database type is sqlite
82
+ // viper.SetDefault("DbType", "sqlite") // Default Database type is sqlite
90
83
viper .SetDefault ("LogPath" , path .Join (workingDir , "logs" ))
91
84
viper .SetDefault ("RefreshInterval" , 4 ) // interval between running refresh and checking for resources to updates
92
85
93
- staticPath := viper .GetString ("StaticPath" )
86
+ // Load Config file
87
+ if configPath := viper .GetString ("Config" ); configPath != "" {
88
+ dir , file := filepath .Split (configPath )
89
+ viper .SetConfigName (file ) // name of config file (without extension)
90
+ viper .SetConfigType ("yaml" ) // REQUIRED if the config file does not have the extension in the name
91
+ // If REKA_CONFIG_FILE is set load config from that
92
+ viper .AddConfigPath (dir )
93
+ if err := viper .ReadInConfig (); err != nil {
94
+ if _ , ok := err .(viper.ConfigFileNotFoundError ); ok {
95
+ // Config file not found; ignore error if desired
96
+ log .Fatalf ("error: %s. Consider passing the `--config` variable or settings %s_CONFIG environment" , err , appName )
97
+
98
+ } else {
99
+ // Config file was found but another error was produced
100
+ log .Fatalf ("Error: %s" , err )
101
+ }
102
+ }
103
+ }
94
104
95
105
config = & Config {}
96
106
107
+ staticPath := viper .GetString ("StaticPath" )
97
108
if ! path .IsAbs (staticPath ) {
98
109
config .staticPath = path .Join (workingDir , staticPath )
99
110
}
100
111
101
- // Load Configuration
102
- config .Providers = []string {"aws" } // TODO Remove Test providers init with aws
112
+ config .Providers = viper .GetStringSlice ("Providers" )
113
+ if len (config .Providers ) < 1 {
114
+ log .Fatal ("No providers specified. Reka needs atleast one provider to track" )
115
+ }
116
+
117
+ config .Auth .Username = viper .GetString ("Auth.Username" )
118
+ config .Auth .Password = viper .GetString ("Auth.Password" )
103
119
104
- config .db = & DatabaseConfig {
105
- Type : viper .GetString ("DBType " ),
106
- Host : viper .GetString ("DbHost " ),
107
- Name : viper .GetString ("DbName " ),
108
- User : viper .GetString ("DbUser " ),
109
- Password : viper .GetString ("DbPassword " ),
120
+ config .Database = & DatabaseConfig {
121
+ Type : viper .GetString ("Database.Type " ),
122
+ Host : viper .GetString ("Database.Host " ),
123
+ Name : viper .GetString ("Database.Name " ),
124
+ User : viper .GetString ("Database.User " ),
125
+ Password : viper .GetString ("Database.Password " ),
110
126
}
111
- config .Aws = & AWSConfig {}
127
+ config .Aws = & AwsConfig {}
112
128
config .RefreshInterval = viper .GetInt32 ("RefreshInterval" )
129
+
113
130
config .LogPath = viper .GetString ("LogPath" )
131
+ if ! path .IsAbs (config .LogPath ) {
132
+ config .LogPath = path .Join (workingDir , staticPath )
133
+ }
134
+ // Create the Logs directory if it does not exists
114
135
if _ , err := os .Stat (config .LogPath ); os .IsNotExist (err ) {
115
136
err = os .Mkdir (config .LogPath , os .ModePerm )
116
137
if err != nil {
117
138
log .Fatal ("Could not create log path: " , err )
118
139
}
119
140
}
141
+
142
+ fmt .Println (config .Auth )
120
143
return config
121
144
}
122
145
@@ -127,11 +150,11 @@ func GetConfig() *Config {
127
150
128
151
// GetDB Return database config
129
152
func GetDB () * DatabaseConfig {
130
- return config .db
153
+ return config .Database
131
154
}
132
155
133
156
// GetAWS Return database config
134
- func GetAWS () * AWSConfig {
157
+ func GetAWS () * AwsConfig {
135
158
return config .Aws
136
159
}
137
160
0 commit comments