forked from jfrog/artifactory-cli-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioUtils.go
247 lines (211 loc) · 6.21 KB
/
ioUtils.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package utils
import (
"io"
"os"
"bytes"
"strings"
"bufio"
"strconv"
"net/http"
"io/ioutil"
"path/filepath"
)
var tempDirPath string
func GetFileNameFromPath(path string) string {
index := strings.LastIndex(path, "/")
if index != -1 {
return path[index+1:]
}
index = strings.LastIndex(path, "\\")
if index != -1 {
return path[index+1:]
}
return path
}
func IsDir(path string) bool {
if !IsPathExists(path) {
return false
}
f, err := os.Stat(path)
CheckError(err)
return f.IsDir()
}
func IsPathExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
func IsFileExists(path string) bool {
if !IsPathExists(path) {
return false
}
f, err := os.Stat(path)
CheckError(err)
return !f.IsDir()
}
func IsDirExists(path string) bool {
if !IsPathExists(path) {
return false
}
f, err := os.Stat(path)
CheckError(err)
return f.IsDir()
}
func ReadFile(filePath string) []byte {
content, err := ioutil.ReadFile(filePath)
CheckError(err)
return content
}
func UploadFile(f *os.File, url string, artifactoryDetails ArtifactoryDetails, details *FileDetails) *http.Response {
if details == nil {
details = GetFileDetails(f.Name())
}
req, err := http.NewRequest("PUT", url, f)
CheckError(err)
req.ContentLength = details.Size
req.Close = true
addAuthHeaders(req, artifactoryDetails)
addUserAgentHeader(req)
size := strconv.FormatInt(details.Size, 10)
req.Header.Set("Content-Length", size)
req.Header.Set("X-Checksum-Sha1", details.Sha1)
req.Header.Set("X-Checksum-Md5", details.Md5)
client := &http.Client{}
resp, err := client.Do(req)
CheckError(err)
defer resp.Body.Close()
return resp
}
func DownloadFile(downloadPath, localPath, fileName string, flat bool, artifactoryDetails ArtifactoryDetails) *http.Response {
if !flat && localPath != "" {
os.MkdirAll(localPath ,0777)
fileName = localPath + "/" + fileName
}
out, err := os.Create(fileName)
CheckError(err)
defer out.Close()
resp, body := SendGet(downloadPath, nil, artifactoryDetails)
out.Write(body)
CheckError(err)
return resp
}
func SendPut(url string, content []byte, headers map[string]string, artifactoryDetails ArtifactoryDetails) (*http.Response, []byte) {
return Send("PUT", url, content, headers, artifactoryDetails)
}
func SendPost(url string, content []byte, artifactoryDetails ArtifactoryDetails) (*http.Response, []byte) {
return Send("POST", url, content, nil, artifactoryDetails)
}
func SendGet(url string, headers map[string]string, artifactoryDetails ArtifactoryDetails) (*http.Response, []byte) {
return Send("GET", url, nil, headers, artifactoryDetails)
}
func SendHead(url string, artifactoryDetails ArtifactoryDetails) (*http.Response, []byte) {
return Send("HEAD", url, nil, nil, artifactoryDetails)
}
func Send(method string, url string, content []byte, headers map[string]string,
artifactoryDetails ArtifactoryDetails) (*http.Response, []byte) {
var req *http.Request
var err error
if content != nil {
req, err = http.NewRequest(method, url, bytes.NewBuffer(content))
} else {
req, err = http.NewRequest(method, url, nil)
}
CheckError(err)
req.Close = true
addAuthHeaders(req, artifactoryDetails)
addUserAgentHeader(req)
if headers != nil {
for name := range headers {
req.Header.Set(name, headers[name])
}
}
client := &http.Client{}
resp, err := client.Do(req)
CheckError(err)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return resp, body
}
// Return the recursive list of files and directories in the specified path
func ListFilesRecursive(path string) []string {
fileList := []string{}
err := filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
fileList = append(fileList, path)
return nil
})
CheckError(err)
return fileList
}
// Return the list of files and directories in the specified path
func ListFiles(path string) []string {
sep := GetFileSeperator()
if !strings.HasSuffix(path, sep) {
path += sep
}
fileList := []string{}
files, _ := ioutil.ReadDir(path)
for _, f := range files {
filePath := path + f.Name()
if IsFileExists(filePath) {
fileList = append(fileList, filePath)
}
}
return fileList
}
func GetTempDirPath() string {
if tempDirPath == "" {
Exit(ExitCodeError, "Function cannot be used before 'tempDirPath' is created.")
}
return tempDirPath
}
func CreateTempDirPath() {
if tempDirPath != "" {
Exit(ExitCodeError, "'tempDirPath' has already been initialized.")
}
path, err := ioutil.TempDir("", "artifactory.cli.")
CheckError(err)
tempDirPath = path
}
func RemoveTempDir() {
if IsDirExists(tempDirPath) {
os.RemoveAll(tempDirPath)
}
}
// Reads the content of the file in the source path and appends it to
// the file in the destination path.
func AppendFile(srcPath string, destFile *os.File) {
srcFile, err := os.Open(srcPath)
CheckError(err)
defer func() {
err := srcFile.Close();
CheckError(err)
}()
reader := bufio.NewReader(srcFile)
writer := bufio.NewWriter(destFile)
buf := make([]byte, 1024000)
for {
n, err := reader.Read(buf)
if err != io.EOF {
CheckError(err)
}
if n == 0 {
break
}
_, err = writer.Write(buf[:n])
CheckError(err)
}
err = writer.Flush()
CheckError(err)
}
func addAuthHeaders(req *http.Request, artifactoryDetails ArtifactoryDetails) {
if artifactoryDetails.SshAuthHeaders != nil {
for name := range artifactoryDetails.SshAuthHeaders {
req.Header.Set(name, artifactoryDetails.SshAuthHeaders[name])
}
} else
if artifactoryDetails.User != "" && artifactoryDetails.Password != "" {
req.SetBasicAuth(artifactoryDetails.User, artifactoryDetails.Password)
}
}
func addUserAgentHeader(req *http.Request) {
req.Header.Set("User-Agent", "artifactory-cli-go/" + GetVersion())
}