|
| 1 | +package overlay |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/speakeasy-api/openapi/overlay" |
| 8 | + "github.com/speakeasy-api/openapi/overlay/loader" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "gopkg.in/yaml.v3" |
| 11 | +) |
| 12 | + |
| 13 | +var upgradeCmd = &cobra.Command{ |
| 14 | + Use: "upgrade <overlay-file> [output-file]", |
| 15 | + Short: "Upgrade an Overlay document to the latest supported version (1.1.0)", |
| 16 | + Long: `Upgrade an Overlay specification document to the latest supported version (1.1.0). |
| 17 | +
|
| 18 | +The upgrade process includes: |
| 19 | +- Updating the Overlay version field from 1.0.0 to 1.1.0 |
| 20 | +- Enabling RFC 9535 JSONPath as the default implementation |
| 21 | +- Clearing redundant x-speakeasy-jsonpath: rfc9535 (now default in 1.1.0) |
| 22 | +- All existing actions remain valid and functional |
| 23 | +- Support for new 1.1.0 features like copy actions and info description |
| 24 | +
|
| 25 | +Version Differences: |
| 26 | + 1.0.0: Legacy JSONPath by default, RFC 9535 opt-in with x-speakeasy-jsonpath: rfc9535 |
| 27 | + 1.1.0: RFC 9535 JSONPath by default, legacy opt-out with x-speakeasy-jsonpath: legacy |
| 28 | +
|
| 29 | +Output options: |
| 30 | + - No output file specified: writes to stdout (pipe-friendly) |
| 31 | + - Output file specified: writes to the specified file |
| 32 | + - --write flag: writes in-place to the input file`, |
| 33 | + Example: ` # Preview upgrade (output to stdout) |
| 34 | + openapi overlay upgrade my-overlay.yaml |
| 35 | +
|
| 36 | + # Upgrade and save to new file |
| 37 | + openapi overlay upgrade my-overlay.yaml upgraded-overlay.yaml |
| 38 | +
|
| 39 | + # Upgrade in-place |
| 40 | + openapi overlay upgrade -w my-overlay.yaml`, |
| 41 | + Args: cobra.RangeArgs(1, 2), |
| 42 | + Run: runOverlayUpgrade, |
| 43 | +} |
| 44 | + |
| 45 | +var overlayWriteInPlace bool |
| 46 | + |
| 47 | +func init() { |
| 48 | + upgradeCmd.Flags().BoolVarP(&overlayWriteInPlace, "write", "w", false, |
| 49 | + "write result in-place to input file") |
| 50 | +} |
| 51 | + |
| 52 | +func runOverlayUpgrade(cmd *cobra.Command, args []string) { |
| 53 | + ctx := cmd.Context() |
| 54 | + inputFile := args[0] |
| 55 | + |
| 56 | + var outputFile string |
| 57 | + if len(args) > 1 { |
| 58 | + outputFile = args[1] |
| 59 | + } |
| 60 | + |
| 61 | + // Load the overlay |
| 62 | + o, err := loader.LoadOverlay(inputFile) |
| 63 | + if err != nil { |
| 64 | + Dief("Failed to load overlay: %v", err) |
| 65 | + } |
| 66 | + |
| 67 | + // Validate the overlay before upgrade |
| 68 | + if err := o.Validate(); err != nil { |
| 69 | + Dief("Overlay validation failed: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + originalVersion := o.Version |
| 73 | + |
| 74 | + // Perform the upgrade |
| 75 | + upgraded, err := overlay.Upgrade(ctx, o) |
| 76 | + if err != nil { |
| 77 | + Dief("Failed to upgrade overlay: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + // Print status |
| 81 | + if !upgraded { |
| 82 | + fmt.Fprintf(os.Stderr, "No upgrade needed - overlay is already at version %s\n", originalVersion) |
| 83 | + } else { |
| 84 | + fmt.Fprintf(os.Stderr, "Successfully upgraded overlay from %s to %s\n", originalVersion, o.Version) |
| 85 | + } |
| 86 | + |
| 87 | + // Validate the upgraded overlay |
| 88 | + if err := o.Validate(); err != nil { |
| 89 | + Dief("Upgraded overlay failed validation: %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + // Serialize output |
| 93 | + output, err := o.ToString() |
| 94 | + if err != nil { |
| 95 | + Dief("Failed to serialize overlay: %v", err) |
| 96 | + } |
| 97 | + |
| 98 | + // Determine output destination |
| 99 | + switch { |
| 100 | + case overlayWriteInPlace: |
| 101 | + if err := os.WriteFile(inputFile, []byte(output), 0644); err != nil { |
| 102 | + Dief("Failed to write to input file: %v", err) |
| 103 | + } |
| 104 | + fmt.Fprintf(os.Stderr, "Wrote upgraded overlay to %s\n", inputFile) |
| 105 | + case outputFile != "": |
| 106 | + if err := os.WriteFile(outputFile, []byte(output), 0644); err != nil { |
| 107 | + Dief("Failed to write to output file: %v", err) |
| 108 | + } |
| 109 | + fmt.Fprintf(os.Stderr, "Wrote upgraded overlay to %s\n", outputFile) |
| 110 | + default: |
| 111 | + // Write to stdout |
| 112 | + var node yaml.Node |
| 113 | + if err := yaml.Unmarshal([]byte(output), &node); err != nil { |
| 114 | + Dief("Failed to parse output: %v", err) |
| 115 | + } |
| 116 | + encoder := yaml.NewEncoder(os.Stdout) |
| 117 | + encoder.SetIndent(2) |
| 118 | + if err := encoder.Encode(&node); err != nil { |
| 119 | + Dief("Failed to write to stdout: %v", err) |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments