/
put.go
68 lines (60 loc) · 1.27 KB
/
put.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"time"
"github.com/pkg/errors"
"github.com/bobg/bs"
"github.com/bobg/bs/anchor"
"github.com/bobg/bs/split"
)
func (c maincmd) put(ctx context.Context, a string, dosplit bool, atstr string, bits, fanout uint, _ []string) error {
var (
ref bs.Ref
err error
added bool
)
if dosplit {
w := split.NewWriter(ctx, c.s, split.Bits(bits))
_, err = io.Copy(w, os.Stdin)
if err != nil {
return errors.Wrap(err, "splitting stdin to store")
}
err = w.Close()
if err != nil {
return errors.Wrap(err, "finishing splitting stdin to store")
}
ref = w.Root
} else {
blob, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return errors.Wrap(err, "reading stdin")
}
ref, added, err = c.s.Put(ctx, blob)
if err != nil {
return errors.Wrap(err, "storing blob")
}
}
if a != "" {
at := time.Now()
if atstr != "" {
at, err = parsetime(atstr)
if err != nil {
return errors.Wrap(err, "parsing -at")
}
}
err = anchor.Put(ctx, c.s, a, ref, at)
if err != nil {
return errors.Wrapf(err, "associating anchor %s with blob %s at time %s", a, ref, at)
}
}
if dosplit {
fmt.Printf("ref %s\n", ref)
} else {
fmt.Printf("ref %s (added: %v)\n", ref, added)
}
return nil
}