Skip to content

Commit 95a9232

Browse files
committed
rework "latest" behaviour
1 parent fcb34fd commit 95a9232

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

hub.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func checkCmd(m []byte) {
193193
args := strings.Split(s, " ")
194194
if len(args) > 1 {
195195
go func() {
196-
err := Tools.Download(args[1], "latest", "replace")
196+
err := Tools.Download(args[1], "latest", "keep")
197197
if err != nil {
198198
mapD := map[string]string{"DownloadStatus": "Error", "Msg": err.Error()}
199199
mapB, _ := json.Marshal(mapD)

tools/download.go

+17-23
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,7 @@ func mimeType(data []byte) (string, error) {
7373
// version again. If instead behaviour is "keep" it will not download the version
7474
// if it already exists.
7575
func (t *Tools) Download(name, version, behaviour string) error {
76-
var key string
77-
if version == "latest" {
78-
key = name
79-
} else {
80-
key = name + "-" + version
81-
}
8276

83-
// Check if it already exists
84-
if version != "latest" && behaviour == "keep" {
85-
if _, ok := t.installed[key]; ok {
86-
t.Logger.Println("The tool is already present on the system")
87-
return nil
88-
}
89-
}
9077
// Fetch the index
9178
resp, err := http.Get(t.IndexURL)
9279
if err != nil {
@@ -123,6 +110,16 @@ func (t *Tools) Download(name, version, behaviour string) error {
123110
}
124111
}
125112

113+
key := correctTool.Name + "-" + correctTool.Version
114+
115+
// Check if it already exists
116+
if behaviour == "keep" {
117+
if _, ok := t.installed[key]; ok {
118+
t.Logger.Println("The tool is already present on the system")
119+
return nil
120+
}
121+
}
122+
126123
// Download the tool
127124
t.Logger.Println("Downloading tool " + name + " from " + correctSystem.URL)
128125
resp, err = http.Get(correctSystem.URL)
@@ -221,7 +218,10 @@ func findBaseDir(dirList []string) string {
221218
return baseDir
222219
}
223220
}
224-
baseDir = candidateBaseDir
221+
// avoid setting the candidate if it is the last file
222+
if dirList[len(dirList)-1] != candidateBaseDir {
223+
baseDir = candidateBaseDir
224+
}
225225
}
226226
return baseDir
227227
}
@@ -236,9 +236,7 @@ func extractZip(body []byte, location string) (string, error) {
236236
var dirList []string
237237

238238
for _, f := range r.File {
239-
if f.FileInfo().IsDir() {
240-
dirList = append(dirList, f.Name)
241-
}
239+
dirList = append(dirList, f.Name)
242240
}
243241

244242
basedir := findBaseDir(dirList)
@@ -288,9 +286,7 @@ func extractTarGz(body []byte, location string) (string, error) {
288286
if err == io.EOF {
289287
break
290288
}
291-
if header.FileInfo().IsDir() {
292-
dirList = append(dirList, header.Name)
293-
}
289+
dirList = append(dirList, header.Name)
294290
}
295291

296292
basedir := findBaseDir(dirList)
@@ -358,9 +354,7 @@ func extractBz2(body []byte, location string) (string, error) {
358354
if err == io.EOF {
359355
break
360356
}
361-
if header.FileInfo().IsDir() {
362-
dirList = append(dirList, header.Name)
363-
}
357+
dirList = append(dirList, header.Name)
364358
}
365359

366360
basedir := findBaseDir(dirList)

0 commit comments

Comments
 (0)