forked from kpfaulkner/azurecopy-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
205 lines (159 loc) · 6.29 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"github.com/teliportme/azurecopy-go/azurecopy"
"github.com/teliportme/azurecopy-go/azurecopy/models"
"github.com/teliportme/azurecopy-go/azurecopy/utils/misc"
"flag"
"fmt"
"os"
log "github.com/Sirupsen/logrus"
)
var Version string
func generateSpace(c int) string {
s := ""
for i := 0; i < c; i++ {
s = s + " "
}
return s
}
func printContainer(container *models.SimpleContainer, depth int) {
s := generateSpace(depth)
log.Printf("%scontainer: %s", s, container.Name)
depth = depth + 2
s = generateSpace(depth)
for _, b := range container.BlobSlice {
log.Printf("%sblob: %s", s, b.Name)
}
for _, c := range container.ContainerSlice {
printContainer(c, depth)
}
}
// getCommand. Naive way to determine what the actual user wants to do. Copy, list etc etc.
// rework when it gets more complex.
func getCommand(copyCommand bool, listCommand bool, createContainerCommand string, copyBlobCommand bool) int {
if !copyCommand && !listCommand && createContainerCommand == "" && !copyBlobCommand {
fmt.Println("No command given")
os.Exit(1)
}
if copyCommand {
return misc.CommandCopy
}
if copyBlobCommand {
return misc.CommandCopyBlob
}
if listCommand {
return misc.CommandList
}
if createContainerCommand != "" {
log.Debug("createcommand issued")
return misc.CommandCreateContainer
}
log.Fatal("unsure of command to use")
return misc.CommandUnknown
}
func setupConfiguration() *misc.CloudConfig {
config := misc.NewCloudConfig()
var concurrentCount = flag.Uint("cc", 5, "Concurrent Count. How many blobs are copied concurrently")
var version = flag.Bool("version", false, "Display Version")
var source = flag.String("source", "", "Source URL")
var dest = flag.String("dest", "", "Destination URL")
var debug = flag.Bool("debug", false, "Debug output")
var copyCommand = flag.Bool("copy", false, "Copy from source to destination")
var copyBlobCommand = flag.Bool("copyblob", false, "Copy from source to destination using Azure CopyBlob flag. Can only be used if Azure is destination")
//var copyBlobCommand = false
var listCommand = flag.Bool("list", false, "List contents from source")
var createContainerCommand = flag.String("createcontainer", "", "Create container for destination")
var replace = flag.Bool("replace", true, "Replace blob if already exists")
var azureDefaultAccountName = flag.String("AzureDefaultAccountName", "", "Default Azure Account Name")
var azureDefaultAccountKey = flag.String("AzureDefaultAccountKey", "", "Default Azure Account Key")
var azureSourceAccountName = flag.String("AzureSourceAccountName", "", "Source Azure Account Name")
var azureSourceAccountKey = flag.String("AzureSourceAccountKey", "", "Source Azure Account Key")
var azureDestAccountName = flag.String("AzureDestAccountName", "", "Destination Azure Account Name")
var azureDestAccountKey = flag.String("AzureDestAccountKey", "", "Destination Azure Account Key")
var s3DefaultAccessID = flag.String("S3DefaultAccessID", "", "Default S3 Access ID")
var s3DefaultAccessSecret = flag.String("S3DefaultAccessSecret", "", "Default S3 Access Secret")
var s3DefaultRegion = flag.String("S3DefaultRegion", "", "Default S3 Region")
var s3SourceAccessID = flag.String("S3SourceAccessID", "", "Source S3 Access ID")
var s3SourceAccessSecret = flag.String("S3SourceAccessSecret", "", "Source S3 Access Secret")
var s3SourceRegion = flag.String("S3SourceRegion", "", "Source S3 Region")
var s3DestAccessID = flag.String("S3DestAccessID", "", "Destination S3 Access ID")
var s3DestAccessSecret = flag.String("S3DestAccessSecret", "", "Destination S3 Access Secret")
var s3DestRegion = flag.String("S3DestRegion", "", "Destination S3 Region")
flag.Parse()
config.Version = *version
config.Debug = *debug
if !*version {
// seems toooooo manual. Figure out something nicer later.
if *concurrentCount > 1000 {
fmt.Printf("Maximum number for concurrent count is 1000")
os.Exit(1)
}
config.Command = getCommand(*copyCommand, *listCommand, *createContainerCommand, *copyBlobCommand)
config.Configuration[misc.Source] = *source
config.Configuration[misc.Dest] = *dest
config.Replace = *replace
config.ConcurrentCount = *concurrentCount
config.Configuration[misc.CreateContainerName] = *createContainerCommand
config.Configuration[misc.AzureDefaultAccountName] = *azureDefaultAccountName
config.Configuration[misc.AzureDefaultAccountKey] = *azureDefaultAccountKey
config.Configuration[misc.AzureSourceAccountName] = *azureSourceAccountName
config.Configuration[misc.AzureSourceAccountKey] = *azureSourceAccountKey
config.Configuration[misc.AzureDestAccountName] = *azureDestAccountName
config.Configuration[misc.AzureDestAccountKey] = *azureDestAccountKey
config.Configuration[misc.S3DefaultAccessID] = *s3DefaultAccessID
config.Configuration[misc.S3DefaultAccessSecret] = *s3DefaultAccessSecret
config.Configuration[misc.S3DefaultRegion] = *s3DefaultRegion
config.Configuration[misc.S3SourceAccessID] = *s3SourceAccessID
config.Configuration[misc.S3SourceAccessSecret] = *s3SourceAccessSecret
config.Configuration[misc.S3SourceRegion] = *s3SourceRegion
config.Configuration[misc.S3DestAccessID] = *s3DestAccessID
config.Configuration[misc.S3DestAccessSecret] = *s3DestAccessSecret
config.Configuration[misc.S3DestRegion] = *s3DestRegion
}
return config
}
// "so it begins"
func main() {
config := setupConfiguration()
if !config.Debug {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.DebugLevel)
}
log.Debug("after config setup")
// if display version, then display then exit
if config.Version {
fmt.Println("Version: " + Version)
return
}
ac := azurecopy.NewAzureCopy(*config)
switch config.Command {
case misc.CommandCopy:
err := ac.CopyBlobByURL(config.Replace, false)
if err != nil {
log.Fatal(err)
}
break
case misc.CommandCopyBlob:
err := ac.CopyBlobByURL(config.Replace, true)
if err != nil {
log.Fatal(err)
}
break
case misc.CommandList:
container, err := ac.ListContainer()
if err != nil {
log.Fatal(err)
}
log.Debug("List results")
container.DisplayContainer("")
break
case misc.CommandCreateContainer:
err := ac.CreateContainer(config.Configuration[misc.CreateContainerName])
if err != nil {
log.Fatal(err)
}
case misc.CommandUnknown:
log.Fatal("Unsure of command to execute")
}
}