|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/replicatedhq/troubleshoot/cmd/internal/util" |
| 10 | + "github.com/replicatedhq/troubleshoot/internal/traces" |
| 11 | + "github.com/replicatedhq/troubleshoot/pkg/constants" |
| 12 | + "github.com/replicatedhq/troubleshoot/pkg/k8sutil" |
| 13 | + "github.com/replicatedhq/troubleshoot/pkg/logger" |
| 14 | + "github.com/replicatedhq/troubleshoot/pkg/preflight" |
| 15 | + "github.com/replicatedhq/troubleshoot/pkg/types" |
| 16 | + "github.com/spf13/cobra" |
| 17 | + "github.com/spf13/viper" |
| 18 | + "k8s.io/klog/v2" |
| 19 | +) |
| 20 | + |
| 21 | +func RootCmd() *cobra.Command { |
| 22 | + cmd := &cobra.Command{ |
| 23 | + Use: "lint", |
| 24 | + Args: cobra.MinimumNArgs(1), |
| 25 | + Short: "Linting troubleshoot specs", |
| 26 | + Long: "Lint specs against troubleshoot schemas that run a valid check result", |
| 27 | + SilenceUsage: true, |
| 28 | + SilenceErrors: true, |
| 29 | + PreRun: func(cmd *cobra.Command, args []string) { |
| 30 | + v := viper.GetViper() |
| 31 | + v.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) |
| 32 | + v.BindPFlags(cmd.Flags()) |
| 33 | + |
| 34 | + logger.SetupLogger(v) |
| 35 | + |
| 36 | + if err := util.StartProfiling(); err != nil { |
| 37 | + klog.Errorf("Failed to start profiling: %v", err) |
| 38 | + } |
| 39 | + }, |
| 40 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 41 | + v := viper.GetViper() |
| 42 | + closer, err := traces.ConfigureTracing("lint") |
| 43 | + if err != nil { |
| 44 | + // Do not fail running lints if tracing fails |
| 45 | + klog.Errorf("Failed to initialize open tracing provider: %v", err) |
| 46 | + } else { |
| 47 | + defer closer() |
| 48 | + } |
| 49 | + fmt.Println(v) |
| 50 | + return err |
| 51 | + }, |
| 52 | + PostRun: func(cmd *cobra.Command, args []string) { |
| 53 | + if err := util.StopProfiling(); err != nil { |
| 54 | + klog.Errorf("Failed to stop profiling: %v", err) |
| 55 | + } |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + cobra.OnInitialize(initConfig) |
| 60 | + |
| 61 | + cmd.AddCommand(util.VersionCmd()) |
| 62 | + preflight.AddFlags(cmd.PersistentFlags()) |
| 63 | + |
| 64 | + // Dry run flag should be in cmd.PersistentFlags() flags made available to all subcommands |
| 65 | + // Adding here to avoid that |
| 66 | + cmd.Flags().Bool("dry-run", false, "print the preflight spec without running preflight checks") |
| 67 | + |
| 68 | + k8sutil.AddFlags(cmd.Flags()) |
| 69 | + |
| 70 | + // Initialize klog flags |
| 71 | + logger.InitKlogFlags(cmd) |
| 72 | + |
| 73 | + // CPU and memory profiling flags |
| 74 | + util.AddProfilingFlags(cmd) |
| 75 | + |
| 76 | + return cmd |
| 77 | +} |
| 78 | + |
| 79 | +func InitAndExecute() { |
| 80 | + cmd := RootCmd() |
| 81 | + err := cmd.Execute() |
| 82 | + |
| 83 | + if err != nil { |
| 84 | + var exitErr types.ExitError |
| 85 | + if errors.As(err, &exitErr) { |
| 86 | + // We need to do this, there's situations where we need the non-zero exit code (which comes as part of the custom error struct) |
| 87 | + // but there's no actual error, just an exit code. |
| 88 | + // If there's also an error to output (eg. invalid format etc) then print it as well |
| 89 | + if exitErr.ExitStatus() != constants.EXIT_CODE_FAIL && exitErr.ExitStatus() != constants.EXIT_CODE_WARN { |
| 90 | + cmd.PrintErrln("Error:", err.Error()) |
| 91 | + } |
| 92 | + |
| 93 | + os.Exit(exitErr.ExitStatus()) |
| 94 | + } |
| 95 | + |
| 96 | + // Fallback, should almost never be used (the above Exit() should handle almost all situations |
| 97 | + cmd.PrintErrln("Error:", err.Error()) |
| 98 | + os.Exit(1) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func initConfig() { |
| 103 | + viper.SetEnvPrefix("LINT") |
| 104 | + viper.AutomaticEnv() |
| 105 | +} |
0 commit comments