kubo/test/cli/harness/pbinspect.go
Hector Sanjuan 6b55e64918
Some checks failed
CodeQL / codeql (push) Has been cancelled
Docker Build / docker-build (push) Has been cancelled
Gateway Conformance / gateway-conformance (push) Has been cancelled
Gateway Conformance / gateway-conformance-libp2p-experiment (push) Has been cancelled
Go Build / go-build (push) Has been cancelled
Go Check / go-check (push) Has been cancelled
Go Lint / go-lint (push) Has been cancelled
Go Test / go-test (push) Has been cancelled
Interop / interop-prep (push) Has been cancelled
Sharness / sharness-test (push) Has been cancelled
Spell Check / spellcheck (push) Has been cancelled
Interop / helia-interop (push) Has been cancelled
Interop / ipfs-webui (push) Has been cancelled
feat(config): ipfs add and Import options for controling UnixFS DAG Width (#10774)
Co-authored-by: Marcin Rataj <lidel@lidel.org>
2025-04-15 22:56:38 +02:00

55 lines
1.2 KiB
Go

package harness
import (
"bytes"
"encoding/json"
)
// InspectPBNode uses dag-json output of 'ipfs dag get' to inspect
// "Logical Format" of DAG-PB as defined in
// https://web.archive.org/web/20250403194752/https://ipld.io/specs/codecs/dag-pb/spec/#logical-format
// (mainly used for inspecting Links without depending on any libraries)
func (n *Node) InspectPBNode(cid string) (PBNode, error) {
log.Debugf("node %d dag get %s as dag-json", n.ID, cid)
var root PBNode
var dagJsonOutput bytes.Buffer
res := n.Runner.MustRun(RunRequest{
Path: n.IPFSBin,
Args: []string{"dag", "get", "--output-codec=dag-json", cid},
CmdOpts: []CmdOpt{RunWithStdout(&dagJsonOutput)},
})
if res.Err != nil {
return root, res.Err
}
err := json.Unmarshal(dagJsonOutput.Bytes(), &root)
if err != nil {
return root, err
}
return root, nil
}
// Define structs to match the JSON for
type PBHash struct {
Slash string `json:"/"`
}
type PBLink struct {
Hash PBHash `json:"Hash"`
Name string `json:"Name"`
Tsize int `json:"Tsize"`
}
type PBData struct {
Slash struct {
Bytes string `json:"bytes"`
} `json:"/"`
}
type PBNode struct {
Data PBData `json:"Data"`
Links []PBLink `json:"Links"`
}