-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
66 lines (56 loc) · 1.63 KB
/
main.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
package main
import (
"context"
"gscp/logger"
"gscp/pipeline"
"gscp/version"
"io"
"os"
"reflect"
"cloud.google.com/go/storage"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var log = logger.New("gscp")
var app = kingpin.New("gscp", "Copies data from and to Google Cloud Storage")
var from = app.Arg("from", "where to read from: gs://bucketname/path or - from stdin or /path/ for local file").Required().String()
var to = app.Arg("to", "Where to write to: gs://bucketname/path or - to stdout or /path/ for local file").Required().String()
var metadata = pipeline.Metadata(app.Arg("metadata", "KV pairs to append to uploaded object"))
func main() {
app.Author("dz0ny")
app.Version(version.String())
kingpin.MustParse(app.Parse(os.Args[1:]))
ctx := context.Background()
in, err := pipeline.NewPipeline(ctx, *from)
if err != nil {
log.Fatal(err)
}
out, err := pipeline.NewPipeline(ctx, *to)
if err != nil {
log.Fatal(err)
}
if in.IsCloud() && out.IsCloud() {
log.Fatal("Copying beetwen bucket is not yet supported.")
}
if reflect.ValueOf(in.Reader).IsNil() {
log.Fatalf("%s does not exist.", in.Path)
}
if (in.IsStdio() && out.IsCloud()) || (in.IsCloud() && out.IsStdio()) {
if _, err = io.Copy(out.Writer, in.Reader); err != nil {
log.Fatal(err)
}
switch out.Writer.(type) {
case *storage.Writer:
wrt := out.Writer.(*storage.Writer)
if err = wrt.Close(); err != nil {
log.Fatal(err)
}
default:
}
if out.IsCloud() {
if _, err = out.Object.Update(ctx, storage.ObjectAttrsToUpdate{Metadata: metadata}); err != nil {
log.Fatal(err)
}
}
log.Debugf("Copying done from %s to %s", in, out)
}
}