Skip to content

Commit

Permalink
fix: generate better jsonpatches (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarrosop authored Aug 19, 2023
1 parent 578ce17 commit 6b3f6bf
Show file tree
Hide file tree
Showing 23 changed files with 1,957 additions and 568 deletions.
17 changes: 6 additions & 11 deletions cmd/config/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"path/filepath"
"sort"

jsonpatch "github.com/mattbaird/jsonpatch"
"github.com/nhost/be/services/mimir/model"
"github.com/nhost/cli/clienv"
"github.com/pelletier/go-toml/v2"
"github.com/urfave/cli/v2"
"github.com/wI2L/jsondiff"
)

const (
Expand Down Expand Up @@ -81,7 +81,7 @@ func copyConfig(ce *clienv.CliEnv, dst, overlay string) error {
return nil
}

func toJSON(filepath string) ([]byte, error) {
func readFile(filepath string) (any, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)
Expand All @@ -98,26 +98,21 @@ func toJSON(filepath string) ([]byte, error) {
return nil, fmt.Errorf("failed to unmarshal toml: %w", err)
}

b, err = json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("failed to marshal json: %w", err)
}

return b, nil
return v, nil
}

func generateJSONPatch(origfilepath, newfilepath, dst string) error {
origb, err := toJSON(origfilepath)
origo, err := readFile(origfilepath)
if err != nil {
return fmt.Errorf("failed to convert original toml to json: %w", err)
}

newb, err := toJSON(newfilepath)
newo, err := readFile(newfilepath)
if err != nil {
return fmt.Errorf("failed to convert new toml to json: %w", err)
}

patches, err := jsonpatch.CreatePatch(origb, newb)
patches, err := jsondiff.Compare(origo, newo)
if err != nil {
return fmt.Errorf("failed to generate json patch: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions examples/myproject/nhost/nhost.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ key = '{{ secrets.HASURA_GRAPHQL_JWT_SECRET }}'

[hasura.settings]
enableRemoteSchemaPermissions = false
corsDomain = ['https://zero.app.io', 'https://one.app.io', 'https://two.app.io']

[functions]
[functions.node]
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ require (
github.com/go-git/go-git/v5 v5.6.1
github.com/google/go-cmp v0.5.9
github.com/hashicorp/go-getter v1.7.1
github.com/mattbaird/jsonpatch v0.0.0-20230413205102-771768614e91
github.com/nhost/be v0.0.0-20230716092554-a6815011be18
github.com/pelletier/go-toml/v2 v2.0.8
github.com/urfave/cli/v2 v2.25.5
github.com/wI2L/jsondiff v0.4.0
golang.org/x/mod v0.10.0
golang.org/x/term v0.8.0
gopkg.in/evanphx/json-patch.v5 v5.6.0
Expand All @@ -37,7 +37,6 @@ require (
github.com/cockroachdb/apd/v2 v2.0.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/evanphx/json-patch v0.5.2 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,6 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.m
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0=
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evanphx/json-patch v0.5.2 h1:xVCHIVMUu1wtM/VkR9jVZ45N3FhZfYMMYGorLCR8P3k=
github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY=
Expand Down Expand Up @@ -398,7 +396,6 @@ github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
Expand All @@ -425,8 +422,6 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A=
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/mattbaird/jsonpatch v0.0.0-20230413205102-771768614e91 h1:JnZSkFP1/GLwKCEuuWVhsacvbDQIVa5BRwAwd+9k2Vw=
github.com/mattbaird/jsonpatch v0.0.0-20230413205102-771768614e91/go.mod h1:M1qoD/MqPgTZIk0EWKB38wE28ACRfVcn+cU08jyArI0=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
Expand Down Expand Up @@ -499,6 +494,8 @@ github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQ
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/vektah/gqlparser/v2 v2.5.3 h1:goUwv4+blhtwR3GwefadPVI4ubYc/WZSypljWMQa6IE=
github.com/vektah/gqlparser/v2 v2.5.3/go.mod h1:z8xXUff237NntSuH8mLFijZ+1tjV1swDbpDqjJmk6ME=
github.com/wI2L/jsondiff v0.4.0 h1:iP56F9tK83eiLttg3YdmEENtZnwlYd3ezEpNNnfZVyM=
github.com/wI2L/jsondiff v0.4.0/go.mod h1:nR/vyy1efuDeAtMwc3AF6nZf/2LD1ID8GTyyJ+K8YB0=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
Expand Down
24 changes: 0 additions & 24 deletions vendor/github.com/mattbaird/jsonpatch/.gitignore

This file was deleted.

202 changes: 0 additions & 202 deletions vendor/github.com/mattbaird/jsonpatch/LICENSE

This file was deleted.

46 changes: 0 additions & 46 deletions vendor/github.com/mattbaird/jsonpatch/README.md

This file was deleted.

Loading

0 comments on commit 6b3f6bf

Please sign in to comment.