Skip to content

Commit

Permalink
Merge pull request #81 from nabbar/pkg-artifact
Browse files Browse the repository at this point in the history
Fix hashicorp version cannot extrat version from string
  • Loading branch information
Nicolas JUHEL authored Feb 12, 2021
2 parents 49a29b5 + ad19c96 commit 9862661
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
6 changes: 3 additions & 3 deletions artifact/jfrog/artifactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"github.com/nabbar/golib/errors"
)

func NewArtifactory(ctx context.Context, Do func(req *http.Request) (*http.Response, error), uri, containName, regexName string, reposPath ...string) (artifact.Client, errors.Error) {
func NewArtifactory(ctx context.Context, Do func(req *http.Request) (*http.Response, error), uri, releaseRegex string, releaseGroup int, reposPath ...string) (artifact.Client, errors.Error) {
if u, e := url.Parse(uri); e != nil {
return nil, ErrorURLParse.ErrorParent(e)
} else {
Expand All @@ -45,8 +45,8 @@ func NewArtifactory(ctx context.Context, Do func(req *http.Request) (*http.Respo
ctx: ctx,
endpoint: u,
path: reposPath,
name: containName,
regex: regexName,
group: releaseGroup,
regex: releaseRegex,
}

a.ClientHelper.F = a.ListReleases
Expand Down
53 changes: 29 additions & 24 deletions artifact/jfrog/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type artifactoryModel struct {
ctx context.Context
endpoint *url.URL
path []string
name string
group int
regex string
}

Expand Down Expand Up @@ -177,8 +177,17 @@ func (a *artifactoryModel) request(uri string, bodyResponse interface{}) liberr.
func (a *artifactoryModel) getStorageList() (sto []ResponseStorage, err liberr.Error) {
var (
lst = ResponseReposStorage{}
reg = regexp.MustCompile(a.regex)
)

if a.regex == "" {
return nil, ErrorParamsEmpty.ErrorParent(fmt.Errorf("regex is empty: %s", a.regex))
}

if a.group < 1 {
return nil, ErrorParamsEmpty.ErrorParent(fmt.Errorf("group extracted from regex is empty: %s - %v", a.regex, a.group))
}

if err = a.request("", &lst); err != nil {
return nil, err
} else if len(lst.Children) < 1 {
Expand All @@ -193,6 +202,14 @@ func (a *artifactoryModel) getStorageList() (sto []ResponseStorage, err liberr.E
res = ResponseStorage{}
)

if c.Folder {
continue
}

if !reg.MatchString(c.Uri) {
continue
}

if err = a.request(c.Uri, &res); err != nil {
return nil, err
}
Expand All @@ -219,28 +236,22 @@ func (a *artifactoryModel) releasesAppendNotExist(releases version.Collection, v

func (a *artifactoryModel) ListReleases() (releases version.Collection, err liberr.Error) {
var (
r *regexp.Regexp
reg = regexp.MustCompile(a.regex)
sto []ResponseStorage
)

if a.regex != "" {
r = regexp.MustCompile(a.regex)
}

if sto, err = a.getStorageList(); err != nil {
return nil, err
}

for _, f := range sto {
if a.name != "" && !strings.Contains(f.Path, a.name) {
continue
}
grp := reg.FindStringSubmatch(f.Path)

if r != nil && !r.MatchString(f.Path) {
if len(grp) < a.group {
continue
}

if v, e := version.NewVersion(f.Path); e != nil {
if v, e := version.NewVersion(grp[a.group]); e != nil {
continue
} else if !libart.ValidatePreRelease(v) {
continue
Expand All @@ -254,42 +265,36 @@ func (a *artifactoryModel) ListReleases() (releases version.Collection, err libe

func (a *artifactoryModel) getArtifact(containName string, regexName string, release *version.Version) (art *ResponseStorage, err liberr.Error) {
var (
r1 *regexp.Regexp
r2 *regexp.Regexp
reg = regexp.MustCompile(a.regex)
rg2 *regexp.Regexp

sto []ResponseStorage
)

if a.regex != "" {
r1 = regexp.MustCompile(a.regex)
}

if regexName != "" {
r2 = regexp.MustCompile(regexName)
rg2 = regexp.MustCompile(regexName)
}

if sto, err = a.getStorageList(); err != nil {
return nil, err
}

for _, f := range sto {
if a.name != "" && !strings.Contains(f.Path, a.name) {
continue
}
grp := reg.FindStringSubmatch(f.Path)

if r1 != nil && !r1.MatchString(f.Path) {
if len(grp) < a.group {
continue
}

if v, e := version.NewVersion(f.Path); e != nil {
if v, e := version.NewVersion(grp[a.group]); e != nil {
continue
} else if !libart.ValidatePreRelease(v) {
continue
} else if release != nil && !v.Equal(release) {
continue
} else if containName != "" && !strings.Contains(f.Path, containName) {
continue
} else if r2 != nil && !r2.MatchString(f.Path) {
} else if rg2 != nil && !rg2.MatchString(f.Path) {
continue
} else {
return &f, nil
Expand Down

0 comments on commit 9862661

Please sign in to comment.