github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/cmd/noms/noms_blob_get.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // This file incorporates work covered by the following copyright and 16 // permission notice: 17 // 18 // Copyright 2016 Attic Labs, Inc. All rights reserved. 19 // Licensed under the Apache License, version 2.0: 20 // http://www.apache.org/licenses/LICENSE-2.0 21 22 package main 23 24 import ( 25 "context" 26 "fmt" 27 "io" 28 "os" 29 "time" 30 31 humanize "github.com/dustin/go-humanize" 32 33 "github.com/dolthub/dolt/go/store/cmd/noms/util" 34 "github.com/dolthub/dolt/go/store/config" 35 "github.com/dolthub/dolt/go/store/types" 36 "github.com/dolthub/dolt/go/store/util/profile" 37 "github.com/dolthub/dolt/go/store/util/progressreader" 38 "github.com/dolthub/dolt/go/store/util/status" 39 ) 40 41 func nomsBlobGet(ctx context.Context, ds string, filePath string) int { 42 cfg := config.NewResolver() 43 var blob types.Blob 44 if db, val, err := cfg.GetPath(ctx, ds); err != nil { 45 util.CheckErrorNoUsage(err) 46 } else if val == nil { 47 util.CheckErrorNoUsage(fmt.Errorf("No value at %s", ds)) 48 } else if b, ok := val.(types.Blob); !ok { 49 util.CheckErrorNoUsage(fmt.Errorf("Value at %s is not a blob", ds)) 50 } else { 51 defer db.Close() 52 blob = b 53 } 54 55 defer profile.MaybeStartProfile().Stop() 56 57 if filePath == "" { 58 blob.Copy(ctx, os.Stdout) 59 return 0 60 } 61 62 // Note: overwrites any existing file. 63 file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0644) 64 util.CheckErrorNoUsage(err) 65 defer file.Close() 66 67 start := time.Now() 68 expected := humanize.Bytes(blob.Len()) 69 70 // Create a pipe so that we can connect a progress reader 71 preader, pwriter := io.Pipe() 72 73 go func() { 74 blob.Copy(ctx, pwriter) 75 pwriter.Close() 76 }() 77 78 blobReader := progressreader.New(preader, func(seen uint64) { 79 elapsed := time.Since(start).Seconds() 80 rate := uint64(float64(seen) / elapsed) 81 status.Printf("%s of %s written in %ds (%s/s)...", humanize.Bytes(seen), expected, int(elapsed), humanize.Bytes(rate)) 82 }) 83 84 io.Copy(file, blobReader) 85 status.Done() 86 return 0 87 }