-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (51 loc) · 1.36 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"github.com/candalo/lb/service/drive"
)
var wg = &sync.WaitGroup{}
func callUploadService(filePath string, folderName string) {
defer wg.Done()
fileID, err := service.Upload(filePath, folderName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
fmt.Printf("File %s uploaded with id %s\n", filePath, fileID)
}
func main() {
if len(os.Args) != 2 && len(os.Args) != 3 {
fmt.Fprintln(os.Stderr, "Usage: lb --filepath<filename or folder> [--folder=<name>]")
return
}
filePath := flag.String("filepath", "", "Use this option to indicate the file which should be uploaded")
folderName := flag.String("folder", "", "Use this option to indicate where uploaded file will be stored")
flag.Parse()
fileInfo, err := os.Stat(*filePath)
if err != nil {
fmt.Fprintf(os.Stderr, "File or folder %s does not exist\n", *filePath)
return
}
if len(*folderName) == 0 {
*folderName = "lb-default-folder"
}
if fileInfo.IsDir() {
filesInfo, err := ioutil.ReadDir(*filePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error on read dir %s\n", *filePath)
}
for _, fileInfo := range filesInfo {
wg.Add(1)
go callUploadService(filepath.Join(*filePath, fileInfo.Name()), *folderName)
}
} else {
wg.Add(1)
go callUploadService(*filePath, *folderName)
}
wg.Wait()
}