|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze" |
| 9 | + "github.com/replicatedhq/troubleshoot/pkg/convert" |
| 10 | + "github.com/replicatedhq/troubleshoot/pkg/logger" |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/spf13/viper" |
| 13 | + "gopkg.in/yaml.v2" |
| 14 | +) |
| 15 | + |
| 16 | +func Analyze() *cobra.Command { |
| 17 | + cmd := &cobra.Command{ |
| 18 | + Use: "analyze", |
| 19 | + Short: "analyze a support bundle", |
| 20 | + Long: `...`, |
| 21 | + PreRun: func(cmd *cobra.Command, args []string) { |
| 22 | + viper.BindPFlag("url", cmd.Flags().Lookup("url")) |
| 23 | + viper.BindPFlag("output", cmd.Flags().Lookup("output")) |
| 24 | + viper.BindPFlag("quiet", cmd.Flags().Lookup("quiet")) |
| 25 | + }, |
| 26 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 27 | + v := viper.GetViper() |
| 28 | + |
| 29 | + logger.SetQuiet(v.GetBool("quiet")) |
| 30 | + |
| 31 | + result, err := analyzer.DownloadAndAnalyze(context.TODO(), v.GetString("url")) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + |
| 36 | + var data interface{} |
| 37 | + switch v.GetString("compatibility") { |
| 38 | + case "support-bundle": |
| 39 | + data = convert.FromAnalyzerResult(result) |
| 40 | + default: |
| 41 | + data = result |
| 42 | + } |
| 43 | + |
| 44 | + var formatted []byte |
| 45 | + switch v.GetString("output") { |
| 46 | + case "json": |
| 47 | + formatted, err = json.MarshalIndent(data, "", " ") |
| 48 | + case "", "yaml": |
| 49 | + formatted, err = yaml.Marshal(data) |
| 50 | + default: |
| 51 | + return fmt.Errorf("unsupported output format: %q", v.GetString("output")) |
| 52 | + } |
| 53 | + |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + fmt.Printf("%s", formatted) |
| 59 | + return nil |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + cmd.Flags().String("url", "", "URL of the support bundle to analyze") |
| 64 | + cmd.MarkFlagRequired("url") |
| 65 | + cmd.Flags().String("output", "", "output format: json, yaml") |
| 66 | + cmd.Flags().String("compatibility", "", "output compatibility mode: support-bundle") |
| 67 | + cmd.Flags().MarkHidden("compatibility") |
| 68 | + cmd.Flags().Bool("quiet", false, "enable/disable error messaging and only show parseable output") |
| 69 | + |
| 70 | + viper.BindPFlags(cmd.Flags()) |
| 71 | + |
| 72 | + return cmd |
| 73 | +} |
0 commit comments