@@ -12,11 +12,26 @@ import (
12
12
"github.com/spf13/viper"
13
13
)
14
14
15
+ const (
16
+ env_user = "D2_TARGET_DB_USER"
17
+ env_password = "D2_TARGET_DB_PASSWORD"
18
+ env_host = "D2_TARGET_DB_HOST"
19
+ env_port = "D2_TARGET_DB_PORT"
20
+ env_name = "D2_TARGET_DB_NAME"
21
+ env_type = "D2_TARGET_DB_TYPE"
22
+ env_links = "VIRTUAL_LINKS"
23
+ env_links_path = "VIRTUAL_LINKS_PATH"
24
+ env_groups = "TABLE_GROUPS"
25
+ env_groups_path = "TABLE_GROUPS_PATH"
26
+ env_restriction = "RESTRICTOR_TYPE"
27
+ env_perspective_user = "DESIGNATED_USER"
28
+ )
29
+
15
30
// get_virtual_links returns the virtual links for the program.
16
31
// These are currently set by a file specified by the VIRTUAL_LINKS_PATH environment variable.
17
32
// The file should be a json array of virtual links. See the virtual package for more information.
18
33
func get_virtual_links () []virtual.VirtualLink {
19
- links_reader , err := os .Open (viper .GetString ("VIRTUAL_LINKS_PATH" ))
34
+ links_reader , err := os .Open (viper .GetString (env_links_path ))
20
35
if err != nil {
21
36
//TODO: Log error, or bubble up instead of printing to console
22
37
fmt .Println ("Failed to open virtual links file" )
@@ -43,7 +58,7 @@ func read_virtual_links(_input io.Reader) ([]virtual.VirtualLink, error) {
43
58
// These are currently set by a file specified by the TABLE_GROUPS_PATH environment variable.
44
59
// The file should be a json array of table groups. See the core package for more information.
45
60
func get_table_groups () []core.TableGroup {
46
- table_groups_reader , err := os .Open (viper .GetString ("TABLE_GROUPS_PATH" ))
61
+ table_groups_reader , err := os .Open (viper .GetString (env_groups_path ))
47
62
if err != nil {
48
63
//TODO: Log error, or bubble up instead of printing to console
49
64
fmt .Println ("Failed to read table groups file" )
@@ -70,7 +85,7 @@ func read_table_groups(_input io.Reader) ([]core.TableGroup, error) {
70
85
// get_designated_user returns the designated user for the program. Set by the DESIGNATED_USER environment variable.
71
86
// This is used to restrict the schema to the tables that the designated user has access to. See the mysql package for more information.
72
87
func get_designated_user () string {
73
- return viper .GetString ("DESIGNATED_USER" )
88
+ return viper .GetString (env_perspective_user )
74
89
}
75
90
76
91
// options is a struct that contains the options for the program.
@@ -98,10 +113,10 @@ func get_options() options {
98
113
register_environent_variables ()
99
114
//TODO: Add validation for options
100
115
return options {
101
- use_virtual_links : viper .GetBool ("VIRTUAL_LINKS" ),
102
- use_table_groups : viper .GetBool ("TABLE_GROUPS" ),
103
- restrictor_type : viper .GetString ("RESTRICTOR_TYPE" ),
104
- db_source_type : viper .GetString ("D2_TARGET_DB_TYPE" ),
116
+ use_virtual_links : viper .GetBool (env_links ),
117
+ use_table_groups : viper .GetBool (env_groups ),
118
+ restrictor_type : viper .GetString (env_restriction ),
119
+ db_source_type : viper .GetString (env_type ),
105
120
}
106
121
}
107
122
@@ -122,62 +137,62 @@ func register_commandline_flags() {
122
137
123
138
pflag .Parse ()
124
139
if * virtual_links != "" {
125
- viper .RegisterAlias ("VIRTUAL_LINKS" , "VirtualLinks" )
140
+ viper .RegisterAlias (env_links , "VirtualLinks" )
126
141
}
127
142
if * virtual_links_path != "" {
128
- viper .RegisterAlias ("VIRTUAL_LINKS_PATH" , "VirtualLinksPath" )
143
+ viper .RegisterAlias (env_links_path , "VirtualLinksPath" )
129
144
}
130
145
if * table_groups != "" {
131
- viper .RegisterAlias ("TABLE_GROUPS" , "TableGroups" )
146
+ viper .RegisterAlias (env_groups , "TableGroups" )
132
147
}
133
148
134
149
if * table_groups_path != "" {
135
- viper .RegisterAlias ("TABLE_GROUPS_PATH" , "TableGroupsPath" )
150
+ viper .RegisterAlias (env_groups_path , "TableGroupsPath" )
136
151
}
137
152
if * restrictor_type != "" {
138
- viper .RegisterAlias ("RESTRICTOR_TYPE" , "RestrictorType" )
153
+ viper .RegisterAlias (env_restriction , "RestrictorType" )
139
154
}
140
155
if * db_user != "" {
141
- viper .RegisterAlias ("D2_TARGET_DB_USER" , "D2TargetDbUser" )
156
+ viper .RegisterAlias (env_user , "D2TargetDbUser" )
142
157
}
143
158
if * db_password != "" {
144
- viper .RegisterAlias ("D2_TARGET_DB_PASSWORD" , "D2TargetDbPassword" )
159
+ viper .RegisterAlias (env_password , "D2TargetDbPassword" )
145
160
}
146
161
if * db_host != "" {
147
- viper .RegisterAlias ("D2_TARGET_DB_HOST" , "D2TargetDbHost" )
162
+ viper .RegisterAlias (env_host , "D2TargetDbHost" )
148
163
}
149
164
150
165
if * db_port != "" {
151
- viper .RegisterAlias ("D2_TARGET_DB_PORT" , "D2TargetDbPort" )
166
+ viper .RegisterAlias (env_port , "D2TargetDbPort" )
152
167
}
153
168
if * db_name != "" {
154
- viper .RegisterAlias ("D2_TARGET_DB_NAME" , "D2TargetDbName" )
169
+ viper .RegisterAlias (env_name , "D2TargetDbName" )
155
170
}
156
171
if * db_type != "" {
157
- viper .RegisterAlias ("D2_TARGET_DB_TYPE" , "D2TargetDbType" )
172
+ viper .RegisterAlias (env_type , "D2TargetDbType" )
158
173
}
159
174
if * db_designated_user != "" {
160
- viper .RegisterAlias ("DESIGNATED_USER" , "DesignatedUser" )
175
+ viper .RegisterAlias (env_perspective_user , "DesignatedUser" )
161
176
}
162
177
viper .BindPFlags (pflag .CommandLine )
163
178
}
164
179
165
180
func register_environent_variables () {
166
- viper .BindEnv ("D2_TARGET_DB_USER" )
167
- viper .BindEnv ("D2_TARGET_DB_PASSWORD" )
168
- viper .BindEnv ("D2_TARGET_DB_HOST" )
169
- viper .BindEnv ("D2_TARGET_DB_PORT" )
170
- viper .BindEnv ("D2_TARGET_DB_NAME" )
171
- viper .BindEnv ("D2_TARGET_DB_TYPE" )
181
+ viper .BindEnv (env_user )
182
+ viper .BindEnv (env_password )
183
+ viper .BindEnv (env_host )
184
+ viper .BindEnv (env_port )
185
+ viper .BindEnv (env_name )
186
+ viper .BindEnv (env_type )
172
187
173
- viper .BindEnv ("VIRTUAL_LINKS" )
174
- viper .BindEnv ("VIRTUAL_LINKS_PATH" )
188
+ viper .BindEnv (env_links )
189
+ viper .BindEnv (env_links_path )
175
190
176
- viper .BindEnv ("TABLE_GROUPS" )
177
- viper .BindEnv ("TABLE_GROUPS_PATH" )
191
+ viper .BindEnv (env_groups )
192
+ viper .BindEnv (env_groups_path )
178
193
179
- viper .BindEnv ("RESTRICTOR_TYPE" )
194
+ viper .BindEnv (env_restriction )
180
195
181
- viper .BindEnv ("DESIGNATED_USER" )
196
+ viper .BindEnv (env_perspective_user )
182
197
183
198
}
0 commit comments