|
| 1 | +package openapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "sort" |
| 8 | + |
| 9 | + "github.com/speakeasy-api/openapi/openapi/linter/converter" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var convertRulesCmd = &cobra.Command{ |
| 14 | + Use: "convert-rules <config-file>", |
| 15 | + Short: "Convert Spectral/Vacuum/legacy configs to native linter format", |
| 16 | + Long: `Convert a Spectral, Vacuum, or legacy Speakeasy lint config into the native |
| 17 | +linter format. This generates: |
| 18 | +
|
| 19 | + - A lint.yaml config file with mapped rule overrides |
| 20 | + - TypeScript rule files for custom rules that don't have native equivalents |
| 21 | +
|
| 22 | +Supported input formats: |
| 23 | + - Spectral configs (.spectral.yml / .spectral.yaml) |
| 24 | + - Vacuum configs (Spectral-compatible format) |
| 25 | + - Legacy Speakeasy lint.yaml (with lintVersion/defaultRuleset/rulesets) |
| 26 | +
|
| 27 | +Examples: |
| 28 | + openapi spec lint convert-rules .spectral.yml |
| 29 | + openapi spec lint convert-rules .spectral.yml --output ./converted |
| 30 | + openapi spec lint convert-rules lint.yaml --dry-run |
| 31 | + openapi spec lint convert-rules .spectral.yml --force`, |
| 32 | + Args: cobra.ExactArgs(1), |
| 33 | + Run: runConvertRules, |
| 34 | +} |
| 35 | + |
| 36 | +var ( |
| 37 | + convertOutput string |
| 38 | + convertRulesDir string |
| 39 | + convertForce bool |
| 40 | + convertDryRun bool |
| 41 | +) |
| 42 | + |
| 43 | +func init() { |
| 44 | + convertRulesCmd.Flags().StringVarP(&convertOutput, "output", "o", ".", "Output directory for generated files") |
| 45 | + convertRulesCmd.Flags().StringVar(&convertRulesDir, "rules-dir", "./rules", "Subdirectory for generated .ts rule files") |
| 46 | + convertRulesCmd.Flags().BoolVarP(&convertForce, "force", "f", false, "Overwrite existing files") |
| 47 | + convertRulesCmd.Flags().BoolVar(&convertDryRun, "dry-run", false, "Print summary without writing files") |
| 48 | + |
| 49 | + lintCmd.AddCommand(convertRulesCmd) |
| 50 | +} |
| 51 | + |
| 52 | +func runConvertRules(cmd *cobra.Command, args []string) { |
| 53 | + configFile := args[0] |
| 54 | + |
| 55 | + // Parse the input config |
| 56 | + ir, err := converter.ParseFile(configFile) |
| 57 | + if err != nil { |
| 58 | + fmt.Fprintf(os.Stderr, "Error parsing config: %v\n", err) |
| 59 | + os.Exit(1) |
| 60 | + } |
| 61 | + |
| 62 | + // Generate native output |
| 63 | + result, err := converter.Generate(ir, |
| 64 | + converter.WithRulesDir(convertRulesDir), |
| 65 | + ) |
| 66 | + if err != nil { |
| 67 | + fmt.Fprintf(os.Stderr, "Error generating output: %v\n", err) |
| 68 | + os.Exit(1) |
| 69 | + } |
| 70 | + |
| 71 | + // Print summary |
| 72 | + printConvertSummary(result, configFile) |
| 73 | + |
| 74 | + // Print warnings |
| 75 | + if len(result.Warnings) > 0 { |
| 76 | + fmt.Println("\nWarnings:") |
| 77 | + for _, w := range result.Warnings { |
| 78 | + prefix := "" |
| 79 | + if w.RuleID != "" { |
| 80 | + prefix = fmt.Sprintf("[%s] ", w.RuleID) |
| 81 | + } |
| 82 | + fmt.Printf(" %s(%s) %s\n", prefix, w.Phase, w.Message) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if convertDryRun { |
| 87 | + fmt.Println("\n--dry-run: no files written") |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + // Check for existing files unless --force |
| 92 | + if !convertForce { |
| 93 | + configPath := filepath.Join(convertOutput, "lint.yaml") |
| 94 | + if _, err := os.Stat(configPath); err == nil { |
| 95 | + fmt.Fprintf(os.Stderr, "Error: %s already exists (use --force to overwrite)\n", configPath) |
| 96 | + os.Exit(1) |
| 97 | + } |
| 98 | + rulesPath := filepath.Join(convertOutput, convertRulesDir) |
| 99 | + if _, err := os.Stat(rulesPath); err == nil { |
| 100 | + fmt.Fprintf(os.Stderr, "Error: %s already exists (use --force to overwrite)\n", rulesPath) |
| 101 | + os.Exit(1) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Ensure output directory exists |
| 106 | + if err := os.MkdirAll(convertOutput, 0o755); err != nil { //nolint:gosec |
| 107 | + fmt.Fprintf(os.Stderr, "Error creating output directory: %v\n", err) |
| 108 | + os.Exit(1) |
| 109 | + } |
| 110 | + |
| 111 | + // Write files |
| 112 | + if err := result.WriteFiles(convertOutput); err != nil { |
| 113 | + fmt.Fprintf(os.Stderr, "Error writing files: %v\n", err) |
| 114 | + os.Exit(1) |
| 115 | + } |
| 116 | + |
| 117 | + fmt.Printf("\nFiles written to %s\n", convertOutput) |
| 118 | +} |
| 119 | + |
| 120 | +func printConvertSummary(result *converter.GenerateResult, inputFile string) { |
| 121 | + fmt.Printf("Converting: %s\n\n", inputFile) |
| 122 | + |
| 123 | + // Extends |
| 124 | + if len(result.Config.Extends) > 0 { |
| 125 | + fmt.Printf("Extends: %v\n", result.Config.Extends) |
| 126 | + } |
| 127 | + |
| 128 | + // Rule overrides |
| 129 | + overrideCount := 0 |
| 130 | + for _, entry := range result.Config.Rules { |
| 131 | + if entry.Disabled != nil || entry.Severity != nil { |
| 132 | + overrideCount++ |
| 133 | + } |
| 134 | + } |
| 135 | + if overrideCount > 0 { |
| 136 | + fmt.Printf("Rule overrides: %d\n", overrideCount) |
| 137 | + } |
| 138 | + |
| 139 | + // Generated rules |
| 140 | + if len(result.GeneratedRules) > 0 { |
| 141 | + ruleIDs := sortedKeys(result.GeneratedRules) |
| 142 | + fmt.Printf("Generated rules: %d\n", len(result.GeneratedRules)) |
| 143 | + for _, ruleID := range ruleIDs { |
| 144 | + fmt.Printf(" - %s.ts\n", ruleID) |
| 145 | + } |
| 146 | + |
| 147 | + // Files to be written |
| 148 | + fmt.Println("\nFiles:") |
| 149 | + fmt.Println(" - lint.yaml") |
| 150 | + for _, ruleID := range ruleIDs { |
| 151 | + fmt.Printf(" - %s/%s.ts\n", convertRulesDir, ruleID) |
| 152 | + } |
| 153 | + } else { |
| 154 | + fmt.Println("\nFiles:") |
| 155 | + fmt.Println(" - lint.yaml") |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func sortedKeys(m map[string]string) []string { |
| 160 | + keys := make([]string, 0, len(m)) |
| 161 | + for k := range m { |
| 162 | + keys = append(keys, k) |
| 163 | + } |
| 164 | + sort.Strings(keys) |
| 165 | + return keys |
| 166 | +} |
0 commit comments