-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.go
178 lines (147 loc) · 3.66 KB
/
files.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
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/minio/minio-go/v7"
)
// Main files entrypoint. Given an array of arguments, handles calling the
// appropriate sub-function.
func files(args []string) {
if len(args) < 1 {
fmt.Println(Warn("At least one argument is needed"))
help(true)
os.Exit(1)
}
switch args[0] {
case "list":
list(true)
case "help":
help(true)
default:
validEnv(args[0], args[1:])
}
}
// Checks if an environment exists. Terminates the program if it doesn't.
func validEnv(env string, options []string) {
envs := list(false)
for _, e := range envs {
if e == env {
handleEnv(env, options)
return
}
}
log.Fatalln(Fata("Environment not found. Use ") + Teal("copycat files list") + Fata(" to view a list of valid environments."))
}
// Depending on the options provided, and an environment, call the
// appropriate sub-function.
func handleEnv(env string, options []string) {
switch options[0] {
case "list":
listFiles(env)
case "upload":
requireArgs(options, 1, false, true)
fileUpload(env, options[1:])
case "download":
requireArgs(options, 1, false, true)
fileDownload(env, options[1:])
}
}
// Given an environment, list all the files in that environment.
func listFiles(env string) {
minioClient, bucket, err := getClient()
if err != nil {
log.Fatalln(err)
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
objectCh := minioClient.ListObjects(ctx, bucket, minio.ListObjectsOptions{
Prefix: env + "_uploads/",
Recursive: false,
})
fmt.Println(White(env + "files:"))
if len(objectCh) == 0 {
fmt.Println("... " + Warn("Empty!"))
return
}
for object := range objectCh {
if object.Err != nil {
fmt.Println(object.Err)
return
}
fmt.Println(Teal(strings.Replace(object.Key, env+"_uploads/", "", 1)))
}
}
// Given an environment, and an array which may contain the following:
// - 0: file to upload
// - 1: upload name
//
// upload the specified file.
func fileUpload(env string, args []string) {
minioClient, bucket, err := getClient()
if err != nil {
log.Fatalln(err)
os.Exit(1)
}
ensureBucket(minioClient, bucket)
uploadName := args[0]
if len(args) == 2 {
uploadName = args[1]
}
fmt.Print(Teal("Uploading " + args[0] + " as " + uploadName + " under environment " + env + "... "))
objectName := env + "_uploads/" + uploadName
filePath := args[0]
contentType := "text/plain"
// Upload the env file.
err = uploadFile(minioClient, objectName, filePath, contentType, bucket)
if err != nil {
fmt.Println(Fata("FAILED!"))
log.Fatalln(err)
}
fmt.Println(OK("DONE!"))
}
// Given an environment, and an array which may contain the following:
// - 0: file to download
// - 1: download name
//
// download the specified file.
func fileDownload(env string, args []string) {
minioClient, bucket, err := getClient()
if err != nil {
log.Fatalln(err)
os.Exit(1)
}
ensureBucket(minioClient, bucket)
dlName := args[0]
if len(args) == 2 {
dlName = args[1]
}
fmt.Print(Teal("Downloading " + args[0] + " from environment " + env + " as " + dlName + "... "))
object, err := minioClient.GetObject(context.Background(), bucket, env+"_uploads/"+args[0], minio.GetObjectOptions{})
_, exists := object.Stat()
if err != nil || exists != nil {
fmt.Println(Fata("FAILED!"))
if err != nil {
fmt.Println(err)
} else {
fmt.Println(exists)
}
return
}
localFile, err := os.Create("./" + dlName)
if err != nil {
fmt.Println(Fata("FAILED!"))
fmt.Println(err)
return
}
if _, err = io.Copy(localFile, object); err != nil {
fmt.Println(Fata("FAILED!"))
fmt.Println(err)
return
}
fmt.Println(OK("DONE!"))
}