-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
74 lines (64 loc) · 2 KB
/
upload.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
69
70
71
72
73
74
// ro - Copyright (c) Victhor Sartório, 2020-. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package gcp
import (
"cloud.google.com/go/storage"
"context"
"fmt"
"github.com/vsartor/ro/donna"
"github.com/vsartor/ro/linus"
"google.golang.org/api/option"
)
func uploadCmd() {
// Handle command line parameters
donna.ExpectStrOption("b", "bucket", "Bucket that should receive files.", "")
donna.ExpectStrOption("r", "cred", "Path to credentials file.", "")
donna.ExpectArg("sourcePath", "Path to the object that needs to be uploaded.")
donna.ExpectArg("destinationPath", "Path within the bucket that will receive the object.")
err := donna.Parse()
if err != nil {
fmt.Println(err.Error())
donna.DisplayCommandHelp()
}
bucket, passed := donna.GetStrOption("bucket")
if !passed {
fmt.Println("Did not receive bucket.")
donna.DisplayCommandHelp()
}
credential := getCredentials(bucket)
srcPath, ok := donna.NextArg()
if !ok {
fmt.Println("Expected source path as argument.")
donna.DisplayCommandHelp()
}
dstPath, ok := donna.NextArg()
if !ok {
fmt.Println("Expected destination path as argument.")
donna.DisplayCommandHelp()
}
// Create client
ctx := context.Background()
client, err := storage.NewClient(ctx, option.WithCredentialsFile(credential))
if err != nil {
logger.Fatal("Could not create client: %s", err)
}
// Dispatch to correct routine
if !linus.Exists(srcPath) {
logger.Fatal("srcPath does not exist: %s", srcPath)
}
if linus.IsFile(srcPath) {
logger.Info("Uploading a single file.")
err := uploadFile(srcPath, bucket, dstPath, ctx, client)
if err != nil {
logger.Fatal("Could upload file: %s", err)
}
} else {
logger.Info("Uploading a folder.")
err := uploadDir(srcPath, bucket, dstPath, ctx, client)
if err != nil {
logger.Fatal("Could not upload directory: %s", err)
}
}
}