-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.go
More file actions
115 lines (89 loc) · 2.21 KB
/
files.go
File metadata and controls
115 lines (89 loc) · 2.21 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/kurin/blazer/b2"
"github.com/ttacon/chalk"
)
func uploadSingleFile(ctx context.Context, bucket *b2.Bucket, path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
formmatedSize := ByteCountSI(fi.Size())
fileName := filepath.Base(path)
dir := filepath.Dir(path)
fileString := fmt.Sprintf("%s/%s%s %s(%s)%s", dir, chalk.Yellow, fileName, chalk.Blue, formmatedSize, chalk.Reset)
if err = uploadSingleReader(ctx, bucket, f, fileString, path, false); nil != err {
return err
}
return nil
}
func uploadDirectory(ctx context.Context, bucket *b2.Bucket, rootAbs string) error {
walkFunc := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
if err = uploadSingleFile(ctx, bucket, path); nil != err {
return err
}
}
return nil
}
err := filepath.Walk(rootAbs, walkFunc)
if err != nil {
return err
}
return nil
}
func uploadFiles(ctx context.Context, bucket *b2.Bucket, files []string, allowDirectories bool) error {
for _, f := range files {
path, err := filepath.Abs(f)
if err != nil {
return err
}
fs, err := os.Stat(path)
if nil != err {
if os.IsNotExist(err) {
fmt.Printf("%s%s%s does not exist\n",
chalk.Yellow, f, chalk.Reset)
continue
}
return err
}
if fs.IsDir() {
if allowDirectories {
if err = uploadDirectory(ctx, bucket, path); nil != err {
return err
}
} else {
fmt.Printf("%s%s%s is a directory, but '-dir' flag was not specified - skipping\n",
chalk.Yellow, f, chalk.Reset)
}
} else if err = uploadSingleFile(ctx, bucket, path); nil != err {
return err
}
}
return nil
}
func handleFiles(bucketName string, files []string, allowDirectories bool) error {
ctx, buckets, err := getAllBuckets()
if nil != err {
return err
}
bucket, err := pickBucketPrompt(buckets, bucketName)
if nil != err {
return err
}
printBucket(bucket.Name())
fmt.Printf("Uploading files to %sBackBlaze B2%s cloud storage:\n\n", chalk.Red, chalk.Reset)
return uploadFiles(ctx, bucket, files, allowDirectories)
}