-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathartifact.go
62 lines (51 loc) · 1.32 KB
/
artifact.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
package main
import (
"bytes"
"errors"
"fmt"
"strings"
"text/template"
)
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 (a Artifact) IsSnapshot() bool {
return strings.HasSuffix(a.Version, "-SNAPSHOT")
}
func ParseArtifact(a string, defaultExtension string) (Artifact, error) {
first := strings.Split(a, "@")
tokens := strings.Split(first[0], ":")
ext := defaultExtension
if len(first) > 1 {
ext = first[1]
}
if len(tokens) == 3 {
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], ext}, nil
}
return Artifact{}, errors.New("Artifact description is insufficent, minium: <group_id>:<id>:<version>")
}
func ToFileName(a Artifact, tmp string) (string, error) {
tmpl, err := template.New("artifact-file").Parse(tmp)
if err == nil {
var b bytes.Buffer
err = tmpl.Execute(&b, a)
return b.String(), err
}
return "", err
}