Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Kuffner committed May 24, 2015
1 parent f917bb4 commit d131a52
Show file tree
Hide file tree
Showing 14 changed files with 557 additions and 161 deletions.
33 changes: 16 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
# gomaven

gomaven is a command line tool to download artifacts from a maven or nexus repository.
# repo
repo is a command line tool to download artifacts from a maven or nexus repository.

## Get started

Download the precompiled binary for your platform here: https://github.com/chilicat/gomaven/releases/tag/v0.1.0
Download the precompiled binary for your platform here: https://github.com/chilicat/repo/releases/tag/v0.1.0

gomaven has a self self-documented command line interface. The best way to figure out what you can do with the tool is calling:
repo has a self self-documented command line interface. The best way to figure out what you can do with the tool is calling:

```
gomaven help
repo help
NAME:
gomaven - to deal with maven repositories
repo - to deal with maven repositories
USAGE:
gomaven [global options] command [command options] [arguments...]
repo [global options] command [command options] [arguments...]
VERSION:
0.1.0
COMMANDS:
download, d Downloads artifact from maven repository.
pull, d Downloads artifact from maven repository.
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
Expand All @@ -33,26 +32,26 @@ And from there you can drill into subcommands:


```
gomaven help download
repo help pull
```


## Download a artifact

gomaven makes it easy to download artifacts from a maven repository by providing the typical artifact coordinations GROUP_ID:ID:VERSION. The example below downlods the commons-io jar file in version 2.4:
repo makes it easy to download artifacts from a maven repository by providing the typical artifact coordinations GROUP_ID:ID:VERSION. The example below downlods the commons-io jar file in version 2.4:

```
gomaven download -a commons-io:commons-io:2.4
repo pull -a commons-io:commons-io:2.4
Download: http://repo1.maven.org/maven2/commons-io/commons-io/2.4/commons-io-2.4.jar -> ./commons-io-2.4.jar
```

gomaven checks also the md5 checksum of the file before downloading. If you execute the command above a second time you can see that the tool actually skipps the download:
repo checks also the md5 checksum of the file before downloading. If you execute the command above a second time you can see that the tool actually skipps the download:


```
gomaven download -a commons-io:commons-io:2.4
repo pull -a commons-io:commons-io:2.4
Download Skipped (up-to-date) -> ./commons-io-2.4.jar
```
Expand All @@ -61,18 +60,18 @@ You can also download multiple artifacts:


```
gomaven download -a -a commons-io:commons-io:2.4,commons-lang:commons-lang:2.4
repo download -a -a commons-io:commons-io:2.4,commons-lang:commons-lang:2.4
```


See help for options:

```
gomaven download help
repo pull help
USAGE:
command download [command options] [arguments...]
command pull [command options] [arguments...]
OPTIONS:
--artifact, -a Defines artifact coordinates to download (e.g.: commons-io:commons-io:2.4)
Expand Down
34 changes: 28 additions & 6 deletions artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,43 @@ import (
"errors"
"strings"
"text/template"
"fmt"
)

type Artifact struct {
Group, Id, Version, Class, Ext string
}

type ArtifactInfo struct {
artifact Artifact
url string
destFile string
}

func (p Artifact) String() string {
return fmt.Sprintf("%s:%s:%s:%s:%s", p.Group, p.Id, p.Version, p.Class, p.Ext)
}

func (a Artifact) Pom() Artifact {
return Artifact { a.Group, a.Id, a.Version, "pom", "pom" }
}

func (a Artifact) IsPom() bool {
return a.Class == "pom" && a.Ext == "pom"
}

func ParseArtifact(a string) (Artifact, error) {
tokens := strings.Split(a, ":")
first := strings.Split(a, "@")
tokens := strings.Split(first[0], ":")

ext := "jar"
if len(first) > 1 { ext = first[1] }

if len(tokens) == 3 {
return Artifact{tokens[0], tokens[1], tokens[2], "", "jar"}, nil
return Artifact{tokens[0], tokens[1], tokens[2], "", ext}, nil
} else if len(tokens) == 4 {
return Artifact{tokens[0], tokens[1], tokens[2], tokens[3], "jar"}, nil
} else if len(tokens) == 5 {
return Artifact{tokens[0], tokens[1], tokens[2], tokens[3], tokens[4]}, nil
}
return Artifact{tokens[0], tokens[1], tokens[2], tokens[3], ext}, nil
}
return Artifact{}, errors.New("Artifact description is insufficent, minium: <group_id>:<id>:<version>")
}

Expand Down
14 changes: 14 additions & 0 deletions config.go
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
}
4 changes: 4 additions & 0 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func downloadMd5(uri string) (string, error) {
return strings.Fields(c)[0], nil
}

func DownloadAsString(uri string) (string, error) {
return downloadAsString(uri)
}

func downloadAsString(uri string) (string, error) {
resp, err := http.Get(uri)
if err != nil {
Expand Down
61 changes: 61 additions & 0 deletions downloader.go
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
}
47 changes: 47 additions & 0 deletions downloader_test.go
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)
}
}
}



Loading

0 comments on commit d131a52

Please sign in to comment.