41
41
providers []* types.Provider
42
42
backend state.Backender
43
43
activeState * state.State
44
+ verbose bool
44
45
)
45
46
46
47
// rootCmd represents the base command when called without any subcommands
@@ -51,6 +52,21 @@ var rootCmd = &cobra.Command{
51
52
// Uncomment the following line if your bare application
52
53
// has an action associated with it:
53
54
Run : func (cmd * cobra.Command , args []string ) {
55
+ // Load Config and Defaults
56
+ config .LoadConfig ()
57
+
58
+ cfg = config .GetConfig ()
59
+
60
+ for _ , rule := range cfg .Rules {
61
+ // Convert Rule in config to rules.Rule type
62
+ r := * ((* rules .Rule )(unsafe .Pointer (& rule )))
63
+ r .Tags = resource .Tags (rule .Tags )
64
+ rules .ParseRule (r )
65
+ }
66
+
67
+ // Initialize Provider objects
68
+ providers = initProviders ()
69
+ backend = state .InitBackend ()
54
70
// RefreshResources on every execution
55
71
refreshResources (providers )
56
72
for _ , p := range providers {
@@ -87,6 +103,7 @@ func init() {
87
103
cobra .OnInitialize (initConfig )
88
104
rootCmd .PersistentFlags ().StringVar (& cfgFile , "config" , "" , "config file (default is $HOME/.reka.yaml)" )
89
105
rootCmd .Flags ().BoolP ("unused-only" , "t" , false , "Reap only unused resources" )
106
+ rootCmd .PersistentFlags ().BoolVarP (& verbose , "verbose" , "v" , false , "Output verbose logs (DEBUG)" )
90
107
}
91
108
92
109
// initConfig reads in config file and ENV variables if set.
@@ -114,20 +131,11 @@ func initConfig() {
114
131
log .Info ("Using config file:" , viper .ConfigFileUsed ())
115
132
}
116
133
117
- // Load Config and Defaults
118
- config .LoadConfig ()
119
- cfg = config .GetConfig ()
120
-
121
- for _ , rule := range cfg .Rules {
122
- // Convert Rule in config to rules.Rule type
123
- r := * ((* rules .Rule )(unsafe .Pointer (& rule )))
124
- r .Tags = resource .Tags (rule .Tags )
125
- rules .ParseRule (r )
134
+ if verbose {
135
+ config .SetVerboseLogging ()
136
+ log .SetLevel (log .DebugLevel )
126
137
}
127
138
128
- // Initialize Provider objects
129
- providers = initProviders ()
130
- backend = state .InitBackend ()
131
139
}
132
140
133
141
func initProviders () []* types.Provider {
@@ -146,14 +154,12 @@ func initProviders() []*types.Provider {
146
154
if err != nil {
147
155
log .Fatalf ("Could not initialize %s Provider: %s" , p , err .Error ())
148
156
}
149
- // TODO Config providers
150
157
providers = append (providers , provider )
151
158
}
152
159
return providers
153
160
}
154
161
155
162
// Refresh current status of resources from Providers
156
- // TODO Reconcile state so that new resources are added to desired states and former resources removed
157
163
func refreshResources (providers []* types.Provider ) {
158
164
// activeState is the current state stored in backend
159
165
activeState = backend .GetState ()
@@ -166,8 +172,6 @@ func refreshResources(providers []*types.Provider) {
166
172
167
173
// Add new resources to desired state if they don't already exists
168
174
// this is to ensure all new resources created are also added to reka's desired state
169
- // too many for loops 😫
170
- // TODO check for attribute changes not in desired state for instance, if node pool was scaled
171
175
for currentProvider , currentProviderResources := range activeState .Current {
172
176
if _ , ok := activeState .Desired [currentProvider ]; ok {
173
177
for u , w := range currentProviderResources {
@@ -189,9 +193,6 @@ func refreshResources(providers []*types.Provider) {
189
193
backend .WriteState (activeState )
190
194
}
191
195
192
- func reapResources () {
193
- }
194
-
195
196
func containsResource (res []* resource.Resource , r * resource.Resource ) bool {
196
197
for _ , rs := range res {
197
198
if rs .UUID == r .UUID {
0 commit comments