-
Notifications
You must be signed in to change notification settings - Fork 2
/
tmrc.go
116 lines (112 loc) · 2.54 KB
/
tmrc.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
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
116
package main
import (
"fmt"
"github.com/gosuri/uiprogress"
"github.com/sergevs/GoMailRuCloud/Api"
"log"
"os"
"path"
"path/filepath"
)
func progress(c <-chan int) {
uiprogress.Start()
b := uiprogress.AddBar(<-c)
for i := range c {
b.Set(i)
}
uiprogress.Stop()
}
func usage() {
fmt.Fprintf(os.Stderr, "%s is Trivial Mail.ru cloud Client\n", filepath.Base(os.Args[0]))
fmt.Fprintf(os.Stderr, "Usage: %s -<COMMAND> [FILE/DIR] [FILE/DIR]\n", filepath.Base(os.Args[0]))
fmt.Fprintf(os.Stderr, "Provides basic operates with files at mail.ru cloud storage\n\n")
fmt.Fprintf(os.Stderr, "COMMAND := < cp | cat | get | ls | mkdir | mv | put | rm | stat >\n\n")
fmt.Fprintf(os.Stderr, "Example: tmrc -ls\n")
os.Exit(1)
}
func main() {
if len(os.Args) < 2 {
usage()
}
// Uncomment the next line to disable very useful default logger
// mailrucloud.Logger = log.New(ioutil.Discard, "", log.Lshortfile)
c, err := mailrucloud.NewCloud(os.ExpandEnv("$MAILRU_USER"), os.ExpandEnv("$MAILRU_PASSWORD"), "mail.ru")
if err != nil {
os.Exit(1)
}
switch cmd := os.Args[1]; cmd {
case "-ls":
var dir string
if len(os.Args) > 2 {
dir = os.Args[2]
} else {
dir = "/"
}
err = c.PrintFileList(dir)
case "-rm":
if len(os.Args) > 2 {
err = c.Remove(os.Args[2])
} else {
log.Fatal("File or dir is not specified")
}
case "-mkdir":
if len(os.Args) > 2 {
err = c.Mkdir(os.Args[2])
} else {
log.Fatal("Dir is not specified")
}
case "-stat":
if len(os.Args) > 2 {
err = c.PrintFileStat(os.Args[2])
} else {
log.Fatal("Dir is not specified")
}
case "-cat":
if len(os.Args) != 3 {
log.Fatal("not enougth arguments")
}
err = c.Cat(os.Args[2])
case "-get":
var dst string
if len(os.Args) > 3 {
dst = os.Args[3]
} else if len(os.Args) < 3 {
log.Fatal("not enougth arguments")
} else {
dst = path.Base(os.Args[2])
}
pc := make(chan int)
go progress(pc)
err = c.Get(os.Args[2], dst, pc)
case "-put":
var dst string
if len(os.Args) > 3 {
dst = os.Args[3]
} else if len(os.Args) < 3 {
log.Fatal("not enougth arguments")
} else {
dst = "/" + filepath.Base(os.Args[2])
}
pc := make(chan int)
go progress(pc)
err = c.Upload(os.Args[2], dst, pc)
case "-mv":
if len(os.Args) > 3 {
err = c.Move(os.Args[2], os.Args[3])
} else {
log.Fatal("not enougth arguments")
}
case "-cp":
if len(os.Args) > 3 {
err = c.Copy(os.Args[2], os.Args[3])
} else {
log.Fatal("not enougth arguments")
}
default:
usage()
log.Fatal("Wrong command")
}
if err != nil {
os.Exit(1)
}
}