AT Protocol IPLD-CAR Repository toolkit (CLI)

progress bars

tree 69ec21c7 5b22b667

Changed files
+95 -21
cli
cmd
engine
+6 -2
cli/context.go
··· 1 1 package cli 2 2 3 + import "github.com/schollz/progressbar/v3" 4 + 3 5 type Context struct { 4 - Args []string 5 - WorkingDir string 6 + Args []string 7 + WorkingDir string 8 + Raw bool 9 + ProgressBar *progressbar.ProgressBar 6 10 }
+4 -5
cmd/atr-log.go
··· 29 29 ctx := cli.Context{ 30 30 WorkingDir: workingDir, 31 31 Args: args, 32 + Raw: Raw, 32 33 } 33 34 walk := func(ss repo.RepoSnapshot, err error) { 34 35 if ss.Root.String() == "b" { ··· 44 45 } else { 45 46 yellow := color.New(color.FgYellow).SprintFunc() 46 47 cyan := color.New(color.FgCyan).SprintFunc() 47 - green := color.New(color.FgGreen).SprintFunc() 48 48 49 49 fmt.Printf("[%v]\n", yellow(ss.File)) 50 50 for i, cid := range cp { 51 - stats, _ := ss.GetCollectionStats(cid.String()) 51 + /*stats, _ := ss.GetCollectionStats(cid.String()) 52 52 53 53 sum := 0 54 54 for _, v := range stats { 55 55 sum += v 56 - } 57 - fmt.Printf("%v [#%v] %v objects\n", cyan(cid.String()), len(cp)-i, green(sum)) 56 + }*/ 57 + fmt.Printf("%v [#%v]\n", cyan(cid.String()), len(cp)-i) 58 58 } 59 59 } 60 - //fmt.Printf("\n") 61 60 } 62 61 63 62 stat, _ := os.Stdin.Stat()
+2
cmd/atr-show.go
··· 45 45 ctx := cli.Context{ 46 46 WorkingDir: workingDir, 47 47 Args: args, 48 + Raw: Raw, 48 49 } 49 50 50 51 //q := ctx.Args().First() ··· 87 88 if GoBack > 0 { 88 89 gb, _ := ss.Repo.GetCommitsPath(GoBack) 89 90 rr = gb[len(gb)-1].String() 91 + 90 92 } 91 93 ss.LoadItems(rr) 92 94
+61 -11
engine/io.go
··· 8 8 "log" 9 9 "os" 10 10 "strings" 11 + "time" 11 12 13 + "github.com/atscan/atr/cli" 12 14 "github.com/atscan/atr/repo" 13 15 "github.com/klauspost/compress/zstd" 16 + "github.com/schollz/progressbar/v3" 14 17 ) 15 18 16 - func Load(fn string) (repo.RepoSnapshot, error) { 19 + func Load(ctx *cli.Context, fn string) (repo.RepoSnapshot, error) { 17 20 var ss repo.RepoSnapshot 18 21 var err error 19 22 if strings.HasSuffix(fn, ".car.zst") { 20 - ss, err = LoadCompressed(fn) 23 + ss, err = LoadCompressed(ctx, fn) 21 24 } else if strings.HasSuffix(fn, ".car") { 22 - ss, err = LoadRaw(fn) 25 + ss, err = LoadRaw(ctx, fn) 23 26 } 24 27 if err != nil { 25 28 log.Fatal("Cannot load: ", fn) ··· 29 32 return ss, nil 30 33 } 31 34 32 - func LoadRaw(fn string) (repo.RepoSnapshot, error) { 35 + func LoadRaw(ctx *cli.Context, fn string) (repo.RepoSnapshot, error) { 36 + stat, err := os.Stat(fn) 37 + if err != nil { 38 + return repo.RepoSnapshot{}, err 39 + } 33 40 dat, err := os.Open(fn) 41 + if err != nil { 42 + return repo.RepoSnapshot{}, err 43 + } 34 44 defer dat.Close() 35 45 36 46 if err != nil { 37 47 return repo.RepoSnapshot{}, err 38 48 } 39 - return LoadFromStream(dat) 49 + return LoadFromStream(ctx, dat, stat.Size()) 40 50 } 41 51 42 - func LoadCompressed(fn string) (repo.RepoSnapshot, error) { 52 + func LoadCompressed(ctx *cli.Context, fn string) (repo.RepoSnapshot, error) { 43 53 dat, err := os.Open(fn) 54 + if err != nil { 55 + return repo.RepoSnapshot{}, err 56 + } 44 57 defer dat.Close() 45 58 46 59 if err != nil { 47 60 return repo.RepoSnapshot{}, err 48 61 } 49 62 decoder, _ := zstd.NewReader(dat) 50 - return LoadFromStream(decoder) 63 + return LoadFromStream(ctx, decoder, -1) 51 64 } 52 65 53 - func LoadFromStream(input io.Reader) (repo.RepoSnapshot, error) { 54 - ss, err := LoadRepoFromStream(input) 66 + func LoadFromStream(ctx *cli.Context, input io.Reader, sz int64) (repo.RepoSnapshot, error) { 67 + ss, err := LoadRepoFromStream(ctx, input, sz) 55 68 if err != nil { 56 69 return ss, err 57 70 } ··· 61 74 return ss, nil 62 75 } 63 76 64 - func LoadRepoFromStream(input io.Reader) (repo.RepoSnapshot, error) { 77 + func LoadRepoFromStream(ctx *cli.Context, input io.Reader, sz int64) (repo.RepoSnapshot, error) { 65 78 rctx := context.TODO() 66 79 ss := repo.RepoSnapshot{} 67 80 81 + var bar *progressbar.ProgressBar 82 + if sz > 150_000_000 && !ctx.Raw { 83 + bar = progressbar.NewOptions(int(sz), 84 + progressbar.OptionSetDescription("loading"), 85 + progressbar.OptionSetWriter(os.Stderr), 86 + progressbar.OptionShowBytes(true), 87 + //progressbar.OptionSetWidth(10), 88 + progressbar.OptionThrottle(35*time.Millisecond), 89 + progressbar.OptionShowCount(), 90 + //progressbar.OptionOnCompletion(func() { 91 + //}), 92 + //progressbar.OptionSpinnerType(14), 93 + //progressbar.OptionFullWidth(), 94 + //progressbar.OptionUseANSICodes(true), 95 + progressbar.OptionSetRenderBlankState(false), 96 + /*progressbar.OptionSetTheme(progressbar.Theme{ 97 + Saucer: "=", 98 + SaucerHead: ">", 99 + SaucerPadding: " ", 100 + BarStart: "[", 101 + BarEnd: "]", 102 + }),*/ 103 + ) 104 + ctx.ProgressBar = bar 105 + } 106 + 68 107 buf := new(bytes.Buffer) 69 - size, err := io.Copy(buf, input) 108 + var err error 109 + var size int64 110 + if bar != nil { 111 + size, err = io.Copy(io.MultiWriter(buf, bar), input) 112 + } else { 113 + size, err = io.Copy(buf, input) 114 + } 70 115 if err != nil { 71 116 log.Fatal(err) 72 117 } 73 118 ss.Size = int(size) 119 + 120 + if bar != nil { 121 + defer bar.Finish() 122 + defer bar.Clear() 123 + } 74 124 75 125 r, err := repo.ReadRepoFromCar(rctx, buf) 76 126 if err != nil {
+3 -3
engine/walk.go
··· 31 31 return err 32 32 } 33 33 if !info.IsDir() { 34 - cb(Load(dir)) 34 + cb(Load(ctx, dir)) 35 35 return nil 36 36 } 37 37 err = filepath.Walk(dir, ··· 39 39 if err != nil { 40 40 return err 41 41 } 42 - cb(Load(fn)) 42 + cb(Load(ctx, fn)) 43 43 return nil 44 44 }) 45 45 if err != nil { ··· 49 49 } 50 50 51 51 func WalkStream(ctx *cli.Context, input io.Reader, cb func(repo.RepoSnapshot, error)) error { 52 - ss, err := LoadFromStream(input) 52 + ss, err := LoadFromStream(ctx, input, -1) 53 53 if err != nil { 54 54 log.Println("Cannot load from stream:", err) 55 55 return nil
+5
go.mod
··· 17 17 github.com/jmespath/go-jmespath v0.4.0 18 18 github.com/json-iterator/go v1.1.12 19 19 github.com/klauspost/compress v1.16.7 20 + github.com/schollz/progressbar/v3 v3.13.1 20 21 github.com/spf13/cobra v1.7.0 21 22 github.com/whyrusleeping/cbor-gen v0.0.0-20230331140348-1f892b517e70 22 23 golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 ··· 46 47 github.com/klauspost/cpuid/v2 v2.2.4 // indirect 47 48 github.com/mattn/go-colorable v0.1.13 // indirect 48 49 github.com/mattn/go-isatty v0.0.19 // indirect 50 + github.com/mattn/go-runewidth v0.0.14 // indirect 49 51 github.com/minio/sha256-simd v1.0.0 // indirect 52 + github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect 50 53 github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 51 54 github.com/modern-go/reflect2 v1.0.2 // indirect 52 55 github.com/mr-tron/base58 v1.2.0 // indirect ··· 59 62 github.com/opentracing/opentracing-go v1.2.0 // indirect 60 63 github.com/petar/GoLLRB v0.0.0-20210522233825-ae3b015fd3e9 // indirect 61 64 github.com/polydawn/refmt v0.89.1-0.20221221234430-40501e09de1f // indirect 65 + github.com/rivo/uniseg v0.4.4 // indirect 62 66 github.com/spaolacci/murmur3 v1.1.0 // indirect 63 67 github.com/spf13/pflag v1.0.5 // indirect 64 68 github.com/whyrusleeping/cbor v0.0.0-20171005072247-63513f603b11 // indirect ··· 69 73 golang.org/x/crypto v0.9.0 // indirect 70 74 golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect 71 75 golang.org/x/sys v0.10.0 // indirect 76 + golang.org/x/term v0.10.0 // indirect 72 77 lukechampine.com/blake3 v1.1.7 // indirect 73 78 )
+14
go.sum
··· 100 100 github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= 101 101 github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= 102 102 github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= 103 + github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= 103 104 github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= 104 105 github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= 105 106 github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= ··· 122 123 github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= 123 124 github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= 124 125 github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= 126 + github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= 125 127 github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= 126 128 github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 129 + github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= 130 + github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= 127 131 github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ= 128 132 github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= 129 133 github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g= 130 134 github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM= 135 + github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= 136 + github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= 131 137 github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 132 138 github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= 133 139 github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= ··· 165 171 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 166 172 github.com/polydawn/refmt v0.89.1-0.20221221234430-40501e09de1f h1:VXTQfuJj9vKR4TCkEuWIckKvdHFeJH/huIFJ9/cXOB0= 167 173 github.com/polydawn/refmt v0.89.1-0.20221221234430-40501e09de1f/go.mod h1:/zvteZs/GwLtCgZ4BL6CBsk9IKIlexP43ObX9AxTqTw= 174 + github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= 175 + github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= 176 + github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= 168 177 github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= 169 178 github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= 170 179 github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 171 180 github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 181 + github.com/schollz/progressbar/v3 v3.13.1 h1:o8rySDYiQ59Mwzy2FELeHY5ZARXZTVJC7iHD6PEFUiE= 182 + github.com/schollz/progressbar/v3 v3.13.1/go.mod h1:xvrbki8kfT1fzWzBT/UZd9L6GA+jdL7HAgq2RFnO6fQ= 172 183 github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= 173 184 github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= 174 185 github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= ··· 254 265 golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= 255 266 golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 256 267 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 268 + golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= 269 + golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c= 270 + golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o= 257 271 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 258 272 golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 259 273 golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=