@@ -13,6 +13,7 @@ import (
13
13
14
14
log "github.com/sirupsen/logrus"
15
15
"github.com/spf13/cobra"
16
+ "github.com/spf13/viper"
16
17
17
18
"github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/collectors"
18
19
"github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/runner"
@@ -40,19 +41,50 @@ var (
40
41
keepDebugFiles bool
41
42
)
42
43
44
+ type CollectionParams struct {
45
+ KubeConfig string `mapstructure:"kubeconfig"`
46
+ PTPInterface string `mapstructure:"ptp_interface"`
47
+ OutputFile string `mapstructure:"output_file"`
48
+ Duration string `mapstructure:"duration"`
49
+ CollectorNames []string `mapstructure:"collectors"`
50
+ PollInterval int `mapstructure:"poll_interval"`
51
+ DevInfoAnnouceInterval int `mapstructure:"announce_interval"`
52
+ UseAnalyserJSON bool `mapstructure:"use_analyser_format"`
53
+ LogsOutputFile string `mapstructure:"logs_output"`
54
+ IncludeLogTimestamps bool `mapstructure:"log_timestamps"`
55
+ TempDir string `mapstructure:"tempdir"`
56
+ KeepDebugFiles bool `mapstructure:"keep_debug_files"`
57
+ }
58
+
59
+ func (p * CollectionParams ) CheckForRequiredFields () error {
60
+ missing := make ([]string , 0 )
61
+ if p .KubeConfig == "" {
62
+ missing = append (missing , "kubeconfig" )
63
+ }
64
+ if p .PTPInterface == "" {
65
+ missing = append (missing , "interface" )
66
+ }
67
+ if len (missing ) > 0 {
68
+ return fmt .Errorf (`required flag(s) "%s" not set` , strings .Join (missing , `", "` ))
69
+ }
70
+ return nil
71
+ }
72
+
43
73
// collectCmd represents the collect command
44
74
var collectCmd = & cobra.Command {
45
75
Use : "collect" ,
46
76
Short : "Run the collector tool" ,
47
77
Long : `Run the collector tool to gather data from your target cluster` ,
48
78
Run : func (cmd * cobra.Command , args []string ) {
49
- collectionRunner := runner .NewCollectorRunner (collectorNames )
79
+ runtimeConfig := & CollectionParams {}
80
+ err := populateParams (cmd , runtimeConfig )
81
+ utils .IfErrorExitOrPanic (err )
50
82
51
- requestedDuration , err := time .ParseDuration (requestedDurationStr )
83
+ requestedDuration , err := time .ParseDuration (runtimeConfig .Duration )
84
+ utils .IfErrorExitOrPanic (err )
52
85
if requestedDuration .Nanoseconds () < 0 {
53
86
log .Panicf ("Requested duration must be positive" )
54
87
}
55
- utils .IfErrorExitOrPanic (err )
56
88
57
89
for _ , c := range collectorNames {
58
90
if c == collectors .LogsCollectorName && logsOutputFile == "" {
@@ -78,18 +110,19 @@ var collectCmd = &cobra.Command{
78
110
log .Fatal (err )
79
111
}
80
112
113
+ collectionRunner := runner .NewCollectorRunner (runtimeConfig .CollectorNames )
81
114
collectionRunner .Run (
82
- kubeConfig ,
83
- outputFile ,
115
+ runtimeConfig . KubeConfig ,
116
+ runtimeConfig . OutputFile ,
84
117
requestedDuration ,
85
- pollInterval ,
86
- devInfoAnnouceInterval ,
87
- ptpInterface ,
88
- useAnalyserJSON ,
89
- logsOutputFile ,
90
- includeLogTimestamps ,
91
- tempDir ,
92
- keepDebugFiles ,
118
+ runtimeConfig . PollInterval ,
119
+ runtimeConfig . DevInfoAnnouceInterval ,
120
+ runtimeConfig . PTPInterface ,
121
+ runtimeConfig . UseAnalyserJSON ,
122
+ runtimeConfig . LogsOutputFile ,
123
+ runtimeConfig . IncludeLogTimestamps ,
124
+ runtimeConfig . TempDir ,
125
+ runtimeConfig . KeepDebugFiles ,
93
126
)
94
127
},
95
128
}
@@ -110,6 +143,9 @@ func init() { //nolint:funlen // Allow this to get a little long
110
143
"A positive duration string sequence of decimal numbers and a unit suffix, such as \" 300ms\" , \" 1.5h\" or \" 2h45m\" ." +
111
144
" Valid time units are \" s\" , \" m\" , \" h\" ." ,
112
145
)
146
+ err := viper .BindPFlag ("duration" , collectCmd .Flags ().Lookup ("duration" ))
147
+ utils .IfErrorExitOrPanic (err )
148
+
113
149
collectCmd .Flags ().IntVarP (
114
150
& pollInterval ,
115
151
"rate" ,
@@ -118,13 +154,23 @@ func init() { //nolint:funlen // Allow this to get a little long
118
154
"Poll interval for querying the cluster. The value will be polled once every interval. " +
119
155
"Using --rate 10 will cause the value to be polled once every 10 seconds" ,
120
156
)
157
+ err = viper .BindPFlag ("poll_interval" , collectCmd .Flags ().Lookup ("rate" ))
158
+ utils .IfErrorExitOrPanic (err )
159
+ viper .RegisterAlias ("poll_rate" , "poll_interval" )
160
+ viper .RegisterAlias ("rate" , "poll_interval" )
161
+
121
162
collectCmd .Flags ().IntVarP (
122
163
& devInfoAnnouceInterval ,
123
164
"announce" ,
124
165
"a" ,
125
166
defaultDevInfoInterval ,
126
167
"interval at which to emit the device info summary to the targeted output." ,
127
168
)
169
+ err = viper .BindPFlag ("announce_interval" , collectCmd .Flags ().Lookup ("announce" ))
170
+ utils .IfErrorExitOrPanic (err )
171
+ viper .RegisterAlias ("announce_rate" , "announce_interval" )
172
+ viper .RegisterAlias ("announce" , "announce_interval" )
173
+
128
174
defaultCollectorNames := make ([]string , 0 )
129
175
defaultCollectorNames = append (defaultCollectorNames , runner .All )
130
176
collectCmd .Flags ().StringSliceVarP (
@@ -140,19 +186,32 @@ func init() { //nolint:funlen // Allow this to get a little long
140
186
strings .Join (runner .OptionalCollectorNames , ", " ),
141
187
),
142
188
)
189
+ err = viper .BindPFlag ("collectors" , collectCmd .Flags ().Lookup ("collector" ))
190
+ utils .IfErrorExitOrPanic (err )
191
+ viper .RegisterAlias ("collector" , "collectors" )
143
192
144
193
collectCmd .Flags ().StringVarP (
145
194
& logsOutputFile ,
146
195
"logs-output" , "l" , "" ,
147
196
"Path to the logs output file. This is required when using the logs collector" ,
148
197
)
198
+ err = viper .BindPFlag ("logs_output" , collectCmd .Flags ().Lookup ("logs-output" ))
199
+ utils .IfErrorExitOrPanic (err )
200
+
149
201
collectCmd .Flags ().BoolVar (
150
202
& includeLogTimestamps ,
151
203
"log-timestamps" , defaultIncludeLogTimestamps ,
152
204
"Specifies if collected logs should include timestamps or not. (default is false)" ,
153
205
)
206
+ err = viper .BindPFlag ("log_timestamps" , collectCmd .Flags ().Lookup ("log-timestamps" ))
207
+ utils .IfErrorExitOrPanic (err )
154
208
155
209
collectCmd .Flags ().StringVarP (& tempDir , "tempdir" , "t" , defaultTempDir ,
156
210
"Directory for storing temp/debug files. Must exist." )
211
+ err = viper .BindPFlag ("tempdir" , collectCmd .Flags ().Lookup ("tempdir" ))
212
+ utils .IfErrorExitOrPanic (err )
213
+
157
214
collectCmd .Flags ().BoolVar (& keepDebugFiles , "keep" , defaultKeepDebugFiles , "Keep debug files" )
215
+ err = viper .BindPFlag ("keep_debug_files" , collectCmd .Flags ().Lookup ("keep" ))
216
+ utils .IfErrorExitOrPanic (err )
158
217
}
0 commit comments