-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
186 lines (143 loc) · 4.54 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"bufio"
"fmt"
"github.com/jessevdk/go-flags"
"github.com/rackspace/gophercloud"
osObjects "github.com/rackspace/gophercloud/openstack/objectstorage/v1/objects"
"github.com/rackspace/gophercloud/pagination"
"github.com/rackspace/gophercloud/rackspace"
"github.com/rackspace/gophercloud/rackspace/objectstorage/v1/objects"
"io"
"os"
)
func getServiceClient(userName string, apiKey string, region string) (*gophercloud.ServiceClient, error) {
ao := gophercloud.AuthOptions{
Username: userName,
APIKey: apiKey,
}
provider, err := rackspace.AuthenticatedClient(ao)
if err != nil {
return nil, err
}
serviceClient, err := rackspace.NewObjectStorageV1(provider, gophercloud.EndpointOpts{
Region: region,
})
if err != nil {
return nil, err
}
return serviceClient, nil
}
func getObjectsList(serviceClient *gophercloud.ServiceClient, contaier string) error {
options := &osObjects.ListOpts{Full: true}
objects.List(serviceClient, contaier, options).EachPage(func(page pagination.Page) (bool, error) {
nameList, err := objects.ExtractNames(page)
if err != nil {
return false, err
}
for _, name := range nameList {
fmt.Printf("%+v\n", name)
}
return true, nil
})
return nil
}
func deleteObject(serviceClient *gophercloud.ServiceClient, contaier string, objectName string) error {
_, err := objects.Delete(serviceClient, contaier, objectName, nil).Extract()
return err
}
func getWriterForPath(path string) (*bufio.Writer, error) {
if path == "-" {
writer := bufio.NewWriter(os.Stdout)
return writer, nil
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return nil, err
}
writer := bufio.NewWriter(f)
return writer, nil
}
func downloadObject(serviceClient *gophercloud.ServiceClient, container string, objectName string, writer *bufio.Writer) error {
dr := objects.Download(serviceClient, container, objectName, nil)
if dr.Err != nil {
return dr.Err
}
defer dr.Body.Close()
defer writer.Flush()
_, err := io.Copy(writer, dr.Body)
// fmt.Printf("Written %d\n", bytes)
if err != nil {
return err
}
return dr.Body.Close()
}
func getReaderForPath(path string) (io.ReadSeeker, error) {
if path == "-" {
return os.Stdin, nil
}
return os.Open(path)
}
func uploadObject(serviceClient *gophercloud.ServiceClient, container string, objectName string, reader io.ReadSeeker) error {
res := objects.Create(serviceClient, container, objectName, reader, nil)
return res.Err
}
type BaseFileCommand struct {
File flags.Filename `short:"f" required:"true" long:"file" default:"-"`
Object string `short:"o" required:"true" long:"object"`
}
type Options struct {
UserName string `short:"u" required:"true" long:"user-name" description:"Username"`
ApiKey string `short:"k" required:"true" long:"api-key" description:"Api key"`
Region string `short:"r" required:"false" long:"region" description:"Container region" default:"LON"`
Container string `short:"c" required:"true" long:"container" description:"Container"`
DownloadCommand struct {
*BaseFileCommand
} `command:"download" description:"Downaload object from storage"`
UploadCommand struct {
*BaseFileCommand
} `command:"upload" description:"Upload object to storage"`
ListCommand struct {
} `command:"list" description:"Get list of objects in storage"`
DeleteCommand struct {
Object string `short:"o" required:"true" long:"object"`
} `command:"delete" description:"Delete object from storage"`
}
func doAction() error {
var options Options
parser := flags.NewParser(&options, flags.Default)
_, err := parser.Parse()
if err != nil {
return err
}
serviceClient, err := getServiceClient(options.UserName, options.ApiKey, options.Region)
if err != nil {
return err
}
switch parser.Active {
case parser.Command.Find("list"):
return getObjectsList(serviceClient, options.Container)
case parser.Command.Find("delete"):
return deleteObject(serviceClient, options.Container, options.DeleteCommand.Object)
case parser.Command.Find("upload"):
reader, err := getReaderForPath(string(options.UploadCommand.File))
if err != nil {
return err
}
return uploadObject(serviceClient, options.Container, options.UploadCommand.Object, reader)
case parser.Command.Find("download"):
writer, err := getWriterForPath(string(options.DownloadCommand.File))
if err != nil {
return err
}
return downloadObject(serviceClient, options.Container, options.DownloadCommand.Object, writer)
}
return nil
}
func main() {
err := doAction()
if err != nil {
os.Exit(1)
}
os.Exit(0)
}