-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniel Kuffner
committed
May 24, 2015
1 parent
f917bb4
commit d131a52
Showing
14 changed files
with
557 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
type Config struct { | ||
// Repository base url | ||
BaseUrl string | ||
// Destination folder. | ||
Dest string | ||
// File name template | ||
Template string | ||
|
||
DryRun bool | ||
|
||
WorkersCount int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package main | ||
|
||
import ( | ||
"path" | ||
) | ||
|
||
type DownloadRequest struct { | ||
artifact Artifact | ||
inMemory bool | ||
consumer chan DownloadResult | ||
} | ||
|
||
type DownloadResult struct { | ||
info ArtifactInfo | ||
err error | ||
content string | ||
} | ||
|
||
type Downloader struct { | ||
num int | ||
conf Config | ||
request chan DownloadRequest | ||
} | ||
|
||
func (d Downloader) Request(r DownloadRequest) { | ||
d.request <- r | ||
} | ||
|
||
func NewDownloader(conf Config) Downloader { | ||
num := conf.WorkersCount | ||
request := make(chan DownloadRequest, num) | ||
d := Downloader { num, conf, request } | ||
for i := 0; i<num ; i++ { | ||
go func () { | ||
for req := range d.request { | ||
filename, _ := ToFileName(req.artifact, d.conf.Template) | ||
info := ArtifactInfo{ | ||
req.artifact, | ||
ToUrl(d.conf.BaseUrl, req.artifact), | ||
path.Join(d.conf.Dest, filename), | ||
} | ||
var err error = nil | ||
var content string = "" | ||
if ! d.conf.DryRun { | ||
if req.inMemory { | ||
content, err = DownloadAsString(info.url) | ||
} else { | ||
err = Download(info.url, info.destFile) | ||
} | ||
} | ||
result := DownloadResult { | ||
info, | ||
err, | ||
content, | ||
} | ||
req.consumer <- result | ||
} | ||
}() | ||
} | ||
return d | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestDownloader(t *testing.T) { | ||
conf := Config { | ||
"http://central.maven.org/maven2", | ||
"/tmp/down-test", | ||
"{{.Id}}-{{.Version}}.{{.Ext}}", | ||
true, | ||
5, | ||
} | ||
|
||
down := NewDownloader(conf) | ||
asset(t, 5, down.num, "num") | ||
|
||
consume := make(chan DownloadResult, 100) | ||
|
||
req1 := DownloadRequest { | ||
Artifact { "commons-lang", "commons-lang", "2.4", "jar", "jar"}, | ||
false, | ||
consume, | ||
} | ||
|
||
req2 := DownloadRequest { | ||
Artifact { "commons-lang", "commons-lang", "2.4", "pom", "pom"}, | ||
false, | ||
consume, | ||
} | ||
|
||
down.Request(req1) | ||
down.Request(req2) | ||
|
||
num := 0 | ||
for res := range consume { | ||
assetError(t, res.err, "") | ||
num++ | ||
if num == 2 { | ||
close(consume) | ||
} | ||
} | ||
} | ||
|
||
|
||
|
Oops, something went wrong.