forked from cloudfoundry/bosh-azure-storage-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (101 loc) · 2.81 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"flag"
"fmt"
"github.com/cloudfoundry/bosh-azure-storage-cli/client"
"github.com/cloudfoundry/bosh-azure-storage-cli/config"
"log"
"os"
"time"
)
func main() {
configPath := flag.String("c", "", "configuration path")
flag.Parse()
configFile, err := os.Open(*configPath)
if err != nil {
log.Fatalln(err)
}
azConfig, err := config.NewFromReader(configFile)
if err != nil {
log.Fatalln(err)
}
storageClient, err := client.NewStorageClient(azConfig)
if err != nil {
log.Fatalln(err)
}
blobstoreClient, err := client.New(storageClient)
if err != nil {
log.Fatalln(err)
}
nonFlagArgs := flag.Args()
if len(nonFlagArgs) < 2 {
log.Fatalf("Expected at least two arguments got %d\n", len(nonFlagArgs))
}
cmd := nonFlagArgs[0]
switch cmd {
case "put":
if len(nonFlagArgs) != 3 {
log.Fatalf("Put method expected 3 arguments got %d\n", len(nonFlagArgs))
}
src, dst := nonFlagArgs[1], nonFlagArgs[2]
var sourceFile *os.File
sourceFile, err = os.Open(src)
if err != nil {
log.Fatalln(err)
}
defer sourceFile.Close()
err = blobstoreClient.Put(sourceFile, dst)
case "get":
if len(nonFlagArgs) != 3 {
log.Fatalf("Get method expected 3 arguments got %d\n", len(nonFlagArgs))
}
src, dst := nonFlagArgs[1], nonFlagArgs[2]
var dstFile *os.File
dstFile, err = os.Create(dst)
if err != nil {
log.Fatalln(err)
}
defer dstFile.Close()
err = blobstoreClient.Get(src, dstFile)
case "delete":
if len(nonFlagArgs) != 2 {
log.Fatalf("Delete method expected 2 arguments got %d\n", len(nonFlagArgs))
}
err = blobstoreClient.Delete(nonFlagArgs[1])
case "exists":
if len(nonFlagArgs) != 2 {
log.Fatalf("Exists method expected 2 arguments got %d\n", len(nonFlagArgs))
}
var exists bool
exists, err = blobstoreClient.Exists(nonFlagArgs[1])
// If the object exists the exit status is 0, otherwise it is 3
// We are using `3` since `1` and `2` have special meanings
if err == nil && !exists {
os.Exit(3)
}
case "sign":
if len(nonFlagArgs) != 4 {
log.Fatalf("Sign method expects 3 arguments got %d\n", len(nonFlagArgs)-1)
}
objectID, action := nonFlagArgs[1], nonFlagArgs[2]
if action != "get" && action != "put" {
log.Fatalf("Action not implemented: %s. Available actions are 'get' and 'put'", action)
}
expiration, err := time.ParseDuration(nonFlagArgs[3])
if err != nil {
log.Fatalf("Expiration should be in the format of a duration i.e. 1h, 60m, 3600s. Got: %s", nonFlagArgs[3])
}
signedURL, err := blobstoreClient.Sign(objectID, action, expiration)
if err != nil {
log.Fatalf("Failed to sign request: %s", err)
os.Exit(1)
}
fmt.Println(signedURL)
os.Exit(0)
default:
log.Fatalf("unknown command: '%s'\n", cmd)
}
if err != nil {
log.Fatalf("performing operation %s: %s\n", cmd, err)
}
}