1- package cli
1+ package preflight
22
33import (
44 "context"
@@ -21,7 +21,6 @@ import (
2121 "github.com/replicatedhq/troubleshoot/pkg/docrewrite"
2222 "github.com/replicatedhq/troubleshoot/pkg/k8sutil"
2323 "github.com/replicatedhq/troubleshoot/pkg/oci"
24- "github.com/replicatedhq/troubleshoot/pkg/preflight"
2524 "github.com/replicatedhq/troubleshoot/pkg/specs"
2625 "github.com/spf13/viper"
2726 spin "github.com/tj/go-spin"
@@ -31,8 +30,8 @@ import (
3130 "k8s.io/client-go/kubernetes/scheme"
3231)
3332
34- func runPreflights ( v * viper. Viper , arg string ) error {
35- if v . GetBool ( " interactive" ) {
33+ func RunPreflights ( interactive bool , output , format , arg string ) error {
34+ if interactive {
3635 fmt .Print (cursor .Hide ())
3736 defer fmt .Print (cursor .Show ())
3837 }
@@ -120,7 +119,7 @@ func runPreflights(v *viper.Viper, arg string) error {
120119 return errors .Wrapf (err , "failed to parse %s" , arg )
121120 }
122121
123- var collectResults []preflight. CollectResult
122+ var collectResults []CollectResult
124123 preflightSpecName := ""
125124
126125 progressCh := make (chan interface {})
@@ -131,7 +130,7 @@ func runPreflights(v *viper.Viper, arg string) error {
131130 defer stopProgressCollection ()
132131 progressCollection , ctx := errgroup .WithContext (ctx )
133132
134- if v . GetBool ( " interactive" ) {
133+ if interactive {
135134 progressCollection .Go (collectInteractiveProgress (ctx , progressCh ))
136135 } else {
137136 progressCollection .Go (collectNonInteractiveProgess (ctx , progressCh ))
@@ -183,14 +182,14 @@ func runPreflights(v *viper.Viper, arg string) error {
183182 stopProgressCollection ()
184183 progressCollection .Wait ()
185184
186- if v . GetBool ( " interactive" ) {
185+ if interactive {
187186 if len (analyzeResults ) == 0 {
188187 return errors .New ("no data has been collected" )
189188 }
190- return showInteractiveResults (preflightSpecName , v . GetString ( " output" ) , analyzeResults )
189+ return showInteractiveResults (preflightSpecName , output , analyzeResults )
191190 }
192191
193- return showStdoutResults (v . GetString ( " format" ) , preflightSpecName , analyzeResults )
192+ return showStdoutResults (format , preflightSpecName , analyzeResults )
194193}
195194
196195func collectInteractiveProgress (ctx context.Context , progressCh <- chan interface {}) func () error {
@@ -235,7 +234,7 @@ func collectNonInteractiveProgess(ctx context.Context, progressCh <-chan interfa
235234 fmt .Fprintf (os .Stderr , "error - %v\n " , msg )
236235 case string :
237236 fmt .Fprintf (os .Stderr , "%s\n " , msg )
238- case preflight. CollectProgress :
237+ case CollectProgress :
239238 fmt .Fprintf (os .Stderr , "%s\n " , msg .String ())
240239
241240 }
@@ -246,15 +245,15 @@ func collectNonInteractiveProgess(ctx context.Context, progressCh <-chan interfa
246245 }
247246}
248247
249- func collectInCluster (preflightSpec * troubleshootv1beta2.Preflight , progressCh chan interface {}) (* preflight. CollectResult , error ) {
248+ func collectInCluster (preflightSpec * troubleshootv1beta2.Preflight , progressCh chan interface {}) (* CollectResult , error ) {
250249 v := viper .GetViper ()
251250
252251 restConfig , err := k8sutil .GetRESTConfig ()
253252 if err != nil {
254253 return nil , errors .Wrap (err , "failed to convert kube flags to rest config" )
255254 }
256255
257- collectOpts := preflight. CollectOpts {
256+ collectOpts := CollectOpts {
258257 Namespace : v .GetString ("namespace" ),
259258 IgnorePermissionErrors : v .GetBool ("collect-without-permissions" ),
260259 ProgressChan : progressCh ,
@@ -268,11 +267,11 @@ func collectInCluster(preflightSpec *troubleshootv1beta2.Preflight, progressCh c
268267 }
269268 }
270269
271- collectResults , err := preflight . Collect (collectOpts , preflightSpec )
270+ collectResults , err := Collect (collectOpts , preflightSpec )
272271 if err != nil {
273- if ! collectResults .IsRBACAllowed () {
272+ if collectResults != nil && ! collectResults .IsRBACAllowed () {
274273 if preflightSpec .Spec .UploadResultsTo != "" {
275- clusterCollectResults := collectResults .(preflight. ClusterCollectResult )
274+ clusterCollectResults := collectResults .(ClusterCollectResult )
276275 err := uploadErrors (preflightSpec .Spec .UploadResultsTo , clusterCollectResults .Collectors )
277276 if err != nil {
278277 progressCh <- err
@@ -285,7 +284,7 @@ func collectInCluster(preflightSpec *troubleshootv1beta2.Preflight, progressCh c
285284 return & collectResults , nil
286285}
287286
288- func collectRemote (preflightSpec * troubleshootv1beta2.HostPreflight , progressCh chan interface {}) (* preflight. CollectResult , error ) {
287+ func collectRemote (preflightSpec * troubleshootv1beta2.HostPreflight , progressCh chan interface {}) (* CollectResult , error ) {
289288 v := viper .GetViper ()
290289
291290 restConfig , err := k8sutil .GetRESTConfig ()
@@ -308,7 +307,7 @@ func collectRemote(preflightSpec *troubleshootv1beta2.HostPreflight, progressCh
308307 timeout = 30 * time .Second
309308 }
310309
311- collectOpts := preflight. CollectOpts {
310+ collectOpts := CollectOpts {
312311 Namespace : namespace ,
313312 IgnorePermissionErrors : v .GetBool ("collect-without-permissions" ),
314313 ProgressChan : progressCh ,
@@ -319,20 +318,20 @@ func collectRemote(preflightSpec *troubleshootv1beta2.HostPreflight, progressCh
319318 Timeout : timeout ,
320319 }
321320
322- collectResults , err := preflight . CollectRemote (collectOpts , preflightSpec )
321+ collectResults , err := CollectRemote (collectOpts , preflightSpec )
323322 if err != nil {
324323 return nil , errors .Wrap (err , "failed to collect from remote" )
325324 }
326325
327326 return & collectResults , nil
328327}
329328
330- func collectHost (hostPreflightSpec * troubleshootv1beta2.HostPreflight , progressCh chan interface {}) (* preflight. CollectResult , error ) {
331- collectOpts := preflight. CollectOpts {
329+ func collectHost (hostPreflightSpec * troubleshootv1beta2.HostPreflight , progressCh chan interface {}) (* CollectResult , error ) {
330+ collectOpts := CollectOpts {
332331 ProgressChan : progressCh ,
333332 }
334333
335- collectResults , err := preflight . CollectHost (collectOpts , hostPreflightSpec )
334+ collectResults , err := CollectHost (collectOpts , hostPreflightSpec )
336335 if err != nil {
337336 return nil , errors .Wrap (err , "failed to collect from host" )
338337 }
0 commit comments