|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/pkg/errors" |
| 8 | + "github.com/replicatedhq/troubleshoot/pkg/constants" |
| 9 | + "github.com/replicatedhq/troubleshoot/pkg/lint" |
| 10 | + "github.com/replicatedhq/troubleshoot/pkg/types" |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/spf13/viper" |
| 13 | +) |
| 14 | + |
| 15 | +func LintCmd() *cobra.Command { |
| 16 | + cmd := &cobra.Command{ |
| 17 | + Use: "lint [spec-files...]", |
| 18 | + Args: cobra.MinimumNArgs(1), |
| 19 | + Short: "Lint v1beta2/v1beta3 troubleshoot specs for syntax and structural errors", |
| 20 | + Long: `Lint v1beta2/v1beta3 troubleshoot specs (both preflight and support-bundle) for syntax and structural errors. |
| 21 | +
|
| 22 | +This command validates v1beta2/v1beta3 troubleshoot specs and checks for: |
| 23 | +- YAML syntax errors |
| 24 | +- Missing required fields (apiVersion, kind, metadata, spec) |
| 25 | +- Invalid template syntax ({{ .Values.* }}) |
| 26 | +- Missing collectors or hostCollectors |
| 27 | +- Common structural issues |
| 28 | +- Missing docStrings (warning) |
| 29 | +
|
| 30 | +Examples: |
| 31 | + # Lint a single spec file |
| 32 | + support-bundle lint my-spec.yaml |
| 33 | +
|
| 34 | + # Lint multiple spec files |
| 35 | + support-bundle lint spec1.yaml spec2.yaml spec3.yaml |
| 36 | +
|
| 37 | + # Lint with automatic fixes |
| 38 | + support-bundle lint --fix my-spec.yaml |
| 39 | +
|
| 40 | + # Lint and output as JSON for CI/CD integration |
| 41 | + support-bundle lint --format json my-spec.yaml |
| 42 | +
|
| 43 | +Notes: |
| 44 | +- v1beta2 does not support templating; template syntax in v1beta2 files will be flagged as errors. |
| 45 | +- v1beta3 supports templating and is linted with template-awareness. |
| 46 | +
|
| 47 | +Exit codes: |
| 48 | + 0 - No errors found |
| 49 | + 2 - Validation errors found`, |
| 50 | + PreRun: func(cmd *cobra.Command, args []string) { |
| 51 | + viper.BindPFlags(cmd.Flags()) |
| 52 | + }, |
| 53 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 54 | + v := viper.GetViper() |
| 55 | + |
| 56 | + opts := lint.LintOptions{ |
| 57 | + FilePaths: args, |
| 58 | + Fix: v.GetBool("fix"), |
| 59 | + Format: v.GetString("format"), |
| 60 | + } |
| 61 | + |
| 62 | + return runLint(opts) |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + cmd.Flags().Bool("fix", false, "Automatically fix issues where possible") |
| 67 | + cmd.Flags().String("format", "text", "Output format: text or json") |
| 68 | + |
| 69 | + return cmd |
| 70 | +} |
| 71 | + |
| 72 | +func runLint(opts lint.LintOptions) error { |
| 73 | + // Validate file paths exist |
| 74 | + for _, filePath := range opts.FilePaths { |
| 75 | + if _, err := os.Stat(filePath); err != nil { |
| 76 | + return errors.Wrapf(err, "file not found: %s", filePath) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Run linting |
| 81 | + results, err := lint.LintFiles(opts) |
| 82 | + if err != nil { |
| 83 | + return errors.Wrap(err, "failed to lint files") |
| 84 | + } |
| 85 | + |
| 86 | + // Format and print results |
| 87 | + output := lint.FormatResults(results, opts.Format) |
| 88 | + fmt.Print(output) |
| 89 | + |
| 90 | + // Return appropriate exit code |
| 91 | + if lint.HasErrors(results) { |
| 92 | + return types.NewExitCodeError(constants.EXIT_CODE_SPEC_ISSUES, nil) |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
0 commit comments