MCPcopy Create free account
hub / github.com/nhost/nhost / GenerateJSONPatch

Function GenerateJSONPatch

cli/cmd/config/edit.go:104–140  ·  view source on GitHub ↗
(origfilepath, newfilepath, dst string)

Source from the content-addressed store, hash-verified

102}
103
104func 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
142func edit(ctx context.Context, cmd *cli.Command) error {
143 ce := clienv.FromCLI(cmd)

Callers 2

commandConfigEditFunction · 0.92
editFunction · 0.85

Calls 4

ErrorfMethod · 0.80
readFileFunction · 0.70
CloseMethod · 0.65
WriteMethod · 0.65

Tested by

no test coverage detected