| 102 | } |
| 103 | |
| 104 | func GenerateJSONPatch(origfilepath, newfilepath, dst string) error { |
| 105 | origo, err := readFile(origfilepath) |
| 106 | if err != nil { |
| 107 | return fmt.Errorf("failed to convert original toml to json: %w", err) |
| 108 | } |
| 109 | |
| 110 | newo, err := readFile(newfilepath) |
| 111 | if err != nil { |
| 112 | return fmt.Errorf("failed to convert new toml to json: %w", err) |
| 113 | } |
| 114 | |
| 115 | patches, err := jsondiff.Compare(origo, newo) |
| 116 | if err != nil { |
| 117 | return fmt.Errorf("failed to generate json patch: %w", err) |
| 118 | } |
| 119 | |
| 120 | dstf, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644) //nolint:mnd |
| 121 | if err != nil { |
| 122 | return fmt.Errorf("failed to open destination file: %w", err) |
| 123 | } |
| 124 | defer dstf.Close() |
| 125 | |
| 126 | sort.Slice(patches, func(i, j int) bool { |
| 127 | return patches[i].Path < patches[j].Path |
| 128 | }) |
| 129 | |
| 130 | dstb, err := json.MarshalIndent(patches, "", " ") |
| 131 | if err != nil { |
| 132 | return fmt.Errorf("failed to prettify json: %w", err) |
| 133 | } |
| 134 | |
| 135 | if _, err := dstf.Write(dstb); err != nil { |
| 136 | return fmt.Errorf("failed to write to destination file: %w", err) |
| 137 | } |
| 138 | |
| 139 | return nil |
| 140 | } |
| 141 | |
| 142 | func edit(ctx context.Context, cmd *cli.Command) error { |
| 143 | ce := clienv.FromCLI(cmd) |