this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

move MST pretty print code to new file

+128 -110
+1 -110
cmd/goat/repo.go
··· 8 8 "fmt" 9 9 "os" 10 10 "path/filepath" 11 - "strings" 12 11 "time" 13 12 14 13 comatproto "github.com/bluesky-social/indigo/api/atproto" 15 14 "github.com/bluesky-social/indigo/atproto/data" 16 15 "github.com/bluesky-social/indigo/atproto/syntax" 17 - "github.com/bluesky-social/indigo/mst" 18 16 "github.com/bluesky-social/indigo/repo" 19 17 "github.com/bluesky-social/indigo/util" 20 18 "github.com/bluesky-social/indigo/xrpc" 21 19 22 20 "github.com/ipfs/go-cid" 23 - cbor "github.com/ipfs/go-ipld-cbor" 24 - ipld "github.com/ipfs/go-ipld-format" 25 21 "github.com/urfave/cli/v2" 26 - "github.com/xlab/treeprint" 27 22 ) 28 23 29 24 var cmdRepo = &cli.Command{ ··· 247 242 if err != nil { 248 243 return err 249 244 } 250 - // read repository tree in to memory 251 - r, err := repo.ReadRepoFromCar(ctx, inputCAR) 252 - if err != nil { 253 - return err 254 - } 255 - cst := util.CborStore(r.Blockstore()) 256 - // determine which root cid to use, defaulting to repo data root 257 - rootCID := r.DataCid() 258 - if opts.root != "" { 259 - optsRootCID, err := cid.Decode(opts.root) 260 - if err != nil { 261 - return err 262 - } 263 - rootCID = optsRootCID 264 - } 265 - // start walking mst 266 - exists, err := nodeExists(ctx, cst, rootCID) 267 - if err != nil { 268 - return err 269 - } 270 - tree := treeprint.NewWithRoot(displayCID(&rootCID, exists, opts)) 271 - if exists { 272 - if err := walkMST(ctx, cst, rootCID, tree, opts); err != nil { 273 - return err 274 - } 275 - } 276 - // print tree 277 - fmt.Println(tree.String()) 278 - return nil 279 - } 280 - 281 - func walkMST(ctx context.Context, cst *cbor.BasicIpldStore, cid cid.Cid, tree treeprint.Tree, opts repoMSTOptions) error { 282 - var node mst.NodeData 283 - if err := cst.Get(ctx, cid, &node); err != nil { 284 - return err 285 - } 286 - if node.Left != nil { 287 - exists, err := nodeExists(ctx, cst, *node.Left) 288 - if err != nil { 289 - return err 290 - } 291 - subtree := tree.AddBranch(displayCID(node.Left, exists, opts)) 292 - if exists { 293 - if err := walkMST(ctx, cst, *node.Left, subtree, opts); err != nil { 294 - return err 295 - } 296 - } 297 - } 298 - for _, entry := range node.Entries { 299 - exists, err := nodeExists(ctx, cst, entry.Val) 300 - if err != nil { 301 - return err 302 - } 303 - tree.AddNode(displayEntryVal(&entry, exists, opts)) 304 - if entry.Tree != nil { 305 - exists, err := nodeExists(ctx, cst, *entry.Tree) 306 - if err != nil { 307 - return err 308 - } 309 - subtree := tree.AddBranch(displayCID(entry.Tree, exists, opts)) 310 - if exists { 311 - if err := walkMST(ctx, cst, *entry.Tree, subtree, opts); err != nil { 312 - return err 313 - } 314 - } 315 - } 316 - } 317 - return nil 318 - } 319 - 320 - func displayEntryVal(entry *mst.TreeEntry, exists bool, opts repoMSTOptions) string { 321 - key := string(entry.KeySuffix) 322 - divider := " " 323 - if opts.fullCID { 324 - divider = "\n" 325 - } 326 - return strings.Repeat("∙", int(entry.PrefixLen)) + key + divider + displayCID(&entry.Val, exists, opts) 327 - } 328 - 329 - func displayCID(cid *cid.Cid, exists bool, opts repoMSTOptions) string { 330 - cidDisplay := cid.String() 331 - if !opts.fullCID { 332 - cidDisplay = "…" + string(cidDisplay[len(cidDisplay)-7:]) 333 - } 334 - connector := "─◉" 335 - if !exists { 336 - connector = "─◌" 337 - } 338 - return "[" + cidDisplay + "]" + connector 339 - } 340 - 341 - type repoMSTOptions struct { 342 - carPath string 343 - fullCID bool 344 - root string 345 - } 346 - 347 - func nodeExists(ctx context.Context, cst *cbor.BasicIpldStore, cid cid.Cid) (bool, error) { 348 - if _, err := cst.Blocks.Get(ctx, cid); err != nil { 349 - if errors.Is(err, ipld.ErrNotFound{}) { 350 - return false, nil 351 - } 352 - return false, err 353 - } 354 - return true, nil 245 + return prettyMST(ctx, inputCAR, opts) 355 246 } 356 247 357 248 func runRepoUnpack(cctx *cli.Context) error {
+127
cmd/goat/repo_prettyprint.go
··· 1 + package main 2 + 3 + import ( 4 + "context" 5 + "errors" 6 + "fmt" 7 + "io" 8 + "strings" 9 + 10 + "github.com/bluesky-social/indigo/mst" 11 + "github.com/bluesky-social/indigo/repo" 12 + "github.com/bluesky-social/indigo/util" 13 + 14 + "github.com/ipfs/go-cid" 15 + cbor "github.com/ipfs/go-ipld-cbor" 16 + ipld "github.com/ipfs/go-ipld-format" 17 + "github.com/xlab/treeprint" 18 + ) 19 + 20 + func prettyMST(ctx context.Context, carFile io.Reader, opts repoMSTOptions) error { 21 + 22 + // read repository tree in to memory 23 + r, err := repo.ReadRepoFromCar(ctx, carFile) 24 + if err != nil { 25 + return err 26 + } 27 + cst := util.CborStore(r.Blockstore()) 28 + // determine which root cid to use, defaulting to repo data root 29 + rootCID := r.DataCid() 30 + if opts.root != "" { 31 + optsRootCID, err := cid.Decode(opts.root) 32 + if err != nil { 33 + return err 34 + } 35 + rootCID = optsRootCID 36 + } 37 + // start walking mst 38 + exists, err := nodeExists(ctx, cst, rootCID) 39 + if err != nil { 40 + return err 41 + } 42 + tree := treeprint.NewWithRoot(displayCID(&rootCID, exists, opts)) 43 + if exists { 44 + if err := walkMST(ctx, cst, rootCID, tree, opts); err != nil { 45 + return err 46 + } 47 + } 48 + // print tree 49 + fmt.Println(tree.String()) 50 + return nil 51 + } 52 + 53 + func walkMST(ctx context.Context, cst *cbor.BasicIpldStore, cid cid.Cid, tree treeprint.Tree, opts repoMSTOptions) error { 54 + var node mst.NodeData 55 + if err := cst.Get(ctx, cid, &node); err != nil { 56 + return err 57 + } 58 + if node.Left != nil { 59 + exists, err := nodeExists(ctx, cst, *node.Left) 60 + if err != nil { 61 + return err 62 + } 63 + subtree := tree.AddBranch(displayCID(node.Left, exists, opts)) 64 + if exists { 65 + if err := walkMST(ctx, cst, *node.Left, subtree, opts); err != nil { 66 + return err 67 + } 68 + } 69 + } 70 + for _, entry := range node.Entries { 71 + exists, err := nodeExists(ctx, cst, entry.Val) 72 + if err != nil { 73 + return err 74 + } 75 + tree.AddNode(displayEntryVal(&entry, exists, opts)) 76 + if entry.Tree != nil { 77 + exists, err := nodeExists(ctx, cst, *entry.Tree) 78 + if err != nil { 79 + return err 80 + } 81 + subtree := tree.AddBranch(displayCID(entry.Tree, exists, opts)) 82 + if exists { 83 + if err := walkMST(ctx, cst, *entry.Tree, subtree, opts); err != nil { 84 + return err 85 + } 86 + } 87 + } 88 + } 89 + return nil 90 + } 91 + 92 + func displayEntryVal(entry *mst.TreeEntry, exists bool, opts repoMSTOptions) string { 93 + key := string(entry.KeySuffix) 94 + divider := " " 95 + if opts.fullCID { 96 + divider = "\n" 97 + } 98 + return strings.Repeat("∙", int(entry.PrefixLen)) + key + divider + displayCID(&entry.Val, exists, opts) 99 + } 100 + 101 + func displayCID(cid *cid.Cid, exists bool, opts repoMSTOptions) string { 102 + cidDisplay := cid.String() 103 + if !opts.fullCID { 104 + cidDisplay = "…" + string(cidDisplay[len(cidDisplay)-7:]) 105 + } 106 + connector := "─◉" 107 + if !exists { 108 + connector = "─◌" 109 + } 110 + return "[" + cidDisplay + "]" + connector 111 + } 112 + 113 + type repoMSTOptions struct { 114 + carPath string 115 + fullCID bool 116 + root string 117 + } 118 + 119 + func nodeExists(ctx context.Context, cst *cbor.BasicIpldStore, cid cid.Cid) (bool, error) { 120 + if _, err := cst.Blocks.Get(ctx, cid); err != nil { 121 + if errors.Is(err, ipld.ErrNotFound{}) { 122 + return false, nil 123 + } 124 + return false, err 125 + } 126 + return true, nil 127 + }