Skip to content

Commit

Permalink
feat(cmds): add dag diff
Browse files Browse the repository at this point in the history
  • Loading branch information
schomatis committed Feb 2, 2022
1 parent bc7ddef commit d58bad1
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 10 deletions.
35 changes: 35 additions & 0 deletions core/commands/dag/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ to deprecate and replace the existing 'ipfs object' command moving forward.
"import": DagImportCmd,
"export": DagExportCmd,
"stat": DagStatCmd,
"diff": DagDiffCmd,
},
}

Expand Down Expand Up @@ -319,3 +320,37 @@ Note: This command skips duplicate blocks in reporting both size and the number
}),
},
}

// DagDiffCmd is a command for comparing DAG nodes.
// FIXME: Right now only supports DAG-PB encoding (for the roadmap of this
// command see https://github.com/ipfs/go-ipfs/issues/4801).
var DagDiffCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Diffs two DAG nodes.",
ShortDescription: `
'ipfs dag diff' fetches two DAG nodes and returns their differences.
`,
},
Arguments: []cmds.Argument{
cmds.StringArg("node_before", true, false, "First node, \"before\", in the diff."),
cmds.StringArg("node_after", true, false, "First node, \"after\", in the diff."),
},
Options: []cmds.Option{ // FIXME: Remove if unused.
},
Run: dagDiff,
Type: DagStat{}, // FIXME: Change.
PostRun: cmds.PostRunMap{
cmds.CLI: finishCLIStat,
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, event *DagStat) error {
_, err := fmt.Fprintf(
w,
"%v\n",
event,
)
return err
}),
},
}

64 changes: 64 additions & 0 deletions core/commands/dag/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package dagcmd

import (
"fmt"
cmds "github.com/ipfs/go-ipfs-cmds"
"github.com/ipfs/go-ipfs/core/commands/cmdenv"
"io/ioutil"
"github.com/mattbaird/jsonpatch"
)

func dagDiff(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
if len(req.Arguments) < 2 {
return fmt.Errorf("less than 2 arguments provided for node comparison")
}
if len(req.Arguments) > 2 {
return fmt.Errorf("more than 2 arguments provided for node comparison")
}
//nodeBefore := path.New(req.Arguments[0])
//nodeAfter := path.New(req.Arguments[1])

api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}

getArgNodeAsJson := func(argNumber int) (string, error) {
r, err := getNodeWithCodec(req.Context, req.Arguments[argNumber], "dag-json", api)
if err != nil {
return "", err
}

jsonOutput, err := ioutil.ReadAll(r)
if err != nil {
return "", err
}

return string(jsonOutput), nil
}

nodeBefore, err := getArgNodeAsJson(0)
if err != nil {
return err
}
nodeAfter, err := getArgNodeAsJson(1)
if err != nil {
return err
}

fmt.Printf("BEFORE\n%s\n\n", string(nodeBefore))
fmt.Printf("AFTER\n%s\n\n", string(nodeAfter))

patch, err := jsonpatch.CreatePatch([]byte(nodeBefore), []byte(nodeAfter)) // FIXME: Remove string conversion above
if err != nil {
return err
}

fmt.Printf("======PATCH:======\n")

for _, operation := range patch {
fmt.Printf("DIFF:%s\n", operation.Json())
}

return nil
}
32 changes: 22 additions & 10 deletions core/commands/dag/get.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dagcmd

import (
"context"
"fmt"
coreiface "github.com/ipfs/interface-go-ipfs-core"
"io"

"github.com/ipfs/go-ipfs/core/commands/cmdenv"
Expand All @@ -23,24 +25,34 @@ func dagGet(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) e
}

codecStr, _ := req.Options["output-codec"].(string)
var codec mc.Code
if err := codec.Set(codecStr); err != nil {

r, err := getNodeWithCodec(req.Context, codecStr, req.Arguments[0], api)
if err != nil {
return err
}

rp, err := api.ResolvePath(req.Context, path.New(req.Arguments[0]))
return res.Emit(r)
}

func getNodeWithCodec(ctx context.Context, nodePath string, codecName string, api coreiface.CoreAPI) (io.Reader, error) {
var codec mc.Code
if err := codec.Set(codecName); err != nil {
return nil, err
}

rp, err := api.ResolvePath(ctx, path.New(nodePath))
if err != nil {
return err
return nil, err
}

obj, err := api.Dag().Get(req.Context, rp.Cid())
obj, err := api.Dag().Get(ctx, rp.Cid())
if err != nil {
return err
return nil, err
}

universal, ok := obj.(ipldlegacy.UniversalNode)
if !ok {
return fmt.Errorf("%T is not a valid IPLD node", obj)
return nil, fmt.Errorf("%T is not a valid IPLD node", obj)
}

finalNode := universal.(ipld.Node)
Expand All @@ -50,13 +62,13 @@ func dagGet(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) e

finalNode, err = traversal.Get(finalNode, remainderPath)
if err != nil {
return err
return nil, err
}
}

encoder, err := multicodec.LookupEncoder(uint64(codec))
if err != nil {
return fmt.Errorf("invalid encoding: %s - %s", codec, err)
return nil, fmt.Errorf("invalid encoding: %s - %s", codec, err)
}

r, w := io.Pipe()
Expand All @@ -67,5 +79,5 @@ func dagGet(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) e
}
}()

return res.Emit(r)
return r, nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/coreos/go-systemd/v22 v22.3.2
github.com/dustin/go-humanize v1.0.0
github.com/elgris/jsondiff v0.0.0-20160530203242-765b5c24c302
github.com/evanphx/json-patch v0.5.2 // indirect
github.com/fsnotify/fsnotify v1.5.1
github.com/gabriel-vasile/mimetype v1.4.0
github.com/go-bindata/go-bindata/v3 v3.1.3
Expand Down Expand Up @@ -89,6 +90,7 @@ require (
github.com/libp2p/go-socket-activation v0.1.0
github.com/libp2p/go-tcp-transport v0.4.0
github.com/libp2p/go-ws-transport v0.5.0
github.com/mattbaird/jsonpatch v0.0.0-20200820163806-098863c1fc24
github.com/miekg/dns v1.1.43
github.com/mitchellh/go-homedir v1.1.0
github.com/multiformats/go-multiaddr v0.4.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ=
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/facebookgo/atomicfile v0.0.0-20151019160806-2de1f203e7d5 h1:BBso6MBKW8ncyZLv37o+KNyy0HrrHgfnOaGQC2qvN+A=
github.com/facebookgo/atomicfile v0.0.0-20151019160806-2de1f203e7d5/go.mod h1:JpoxHjuQauoxiFMl1ie8Xc/7TfLuMZ5eOCONd1sUBHg=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
Expand Down Expand Up @@ -1021,6 +1023,8 @@ github.com/marten-seemann/qtls-go1-17 v0.1.0 h1:P9ggrs5xtwiqXv/FHNwntmuLMNq3KaSI
github.com/marten-seemann/qtls-go1-17 v0.1.0/go.mod h1:fz4HIxByo+LlWcreM4CZOYNuz3taBQ8rN2X6FqvaWo8=
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd h1:br0buuQ854V8u83wA0rVZ8ttrq5CpaPZdvrK0LP2lOk=
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd/go.mod h1:QuCEs1Nt24+FYQEqAAncTDPJIuGs+LxK1MCiFL25pMU=
github.com/mattbaird/jsonpatch v0.0.0-20200820163806-098863c1fc24 h1:uYuGXJBAi1umT+ZS4oQJUgKtfXCAYTR+n9zw1ViT0vA=
github.com/mattbaird/jsonpatch v0.0.0-20200820163806-098863c1fc24/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-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
Expand Down

0 comments on commit d58bad1

Please sign in to comment.