Skip to content

Commit

Permalink
Merge pull request #18 from stream-aha/arbitrary-tag
Browse files Browse the repository at this point in the history
allow fetch to work with arbitrary tag formats
  • Loading branch information
brikis98 authored Dec 17, 2016
2 parents b6e097a + 90a030d commit 37cb093
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
22 changes: 13 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,18 @@ func runFetch (c *cli.Context) error {
}
}

// Find the specific release that matches the latest version constraint
latestTag, err := getLatestAcceptableTag(options.TagConstraint, tags)
if err != nil {
if err.errorCode == INVALID_TAG_CONSTRAINT_EXPRESSION {
return errors.New(getErrorMessage(INVALID_TAG_CONSTRAINT_EXPRESSION, err.details))
} else {
return fmt.Errorf("Error occurred while computing latest tag that satisfies version contraint expression: %s", err)
specific, desiredTag := isTagConstraintSpecificTag(options.TagConstraint)
if !specific {
// Find the specific release that matches the latest version constraint
latestTag, err := getLatestAcceptableTag(options.TagConstraint, tags)
if err != nil {
if err.errorCode == INVALID_TAG_CONSTRAINT_EXPRESSION {
return errors.New(getErrorMessage(INVALID_TAG_CONSTRAINT_EXPRESSION, err.details))
} else {
return fmt.Errorf("Error occurred while computing latest tag that satisfies version contraint expression: %s", err)
}
}
desiredTag = latestTag
}

// Prepare the vars we'll need to download
Expand All @@ -129,12 +133,12 @@ func runFetch (c *cli.Context) error {
}

// Download any requested source files
if err := downloadSourcePaths(options.SourcePaths, options.LocalDownloadPath, repo, latestTag, options.BranchName, options.CommitSha); err != nil {
if err := downloadSourcePaths(options.SourcePaths, options.LocalDownloadPath, repo, desiredTag, options.BranchName, options.CommitSha); err != nil {
return err
}

// Download any requested release assets
if err := downloadReleaseAssets(options.ReleaseAssets, options.LocalDownloadPath, repo, latestTag); err != nil {
if err := downloadReleaseAssets(options.ReleaseAssets, options.LocalDownloadPath, repo, desiredTag); err != nil {
return err
}

Expand Down
19 changes: 19 additions & 0 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ import (
"github.com/hashicorp/go-version"
)

func isTagConstraintSpecificTag(tagConstraint string) (bool, string) {
if len(tagConstraint) > 0 {
switch tagConstraint[0] {
// Check for a tagConstraint '='
case '=':
return true, strings.TrimSpace(tagConstraint[1:])

// Check for a tagConstraint without constraint specifier
// Neither of '!=', '>', '>=', '<', '<=', '~>' is prefixed before tag
case '>', '<', '!', '~':
return false, tagConstraint

default:
return true, strings.TrimSpace(tagConstraint)
}
}
return false, tagConstraint
}

func getLatestAcceptableTag(tagConstraint string, tags []string) (string, *FetchError) {
var latestTag string

Expand Down
36 changes: 36 additions & 0 deletions tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,42 @@ func TestGetLatestAcceptableTag(t *testing.T) {
}
}

func TestIsTagConstraintSpecificTag(t *testing.T) {
t.Parallel()

cases := []struct {
tagConstraint string
desiredTag string
specific bool
}{
{"1.0.7", "1.0.7", true},
{" 1.0.7 ", "1.0.7", true},
{"=1.0.7", "1.0.7", true},
{"= 1.0.7", "1.0.7", true},

{"~> 1.0.0", "~> 1.0.0", false},
{"> 1.3", "> 1.3", false},
{">= 1.3", ">= 1.3", false},
{"<= 1.3", "<= 1.3", false},
{"< 1.3", "< 1.3", false},

{"v1.0.7", "v1.0.7", true},
{" v1.0.7 ", "v1.0.7", true},
{"=v1.0.7", "v1.0.7", true},
{"= v1.0.7", "v1.0.7", true},
}

for _, tc := range cases {
specific, desiredTag := isTagConstraintSpecificTag(tc.tagConstraint)
if specific != tc.specific {
t.Fatalf("Given constraint: \"%s\", expected %t, but received %t", tc.tagConstraint, tc.specific, specific)
}
if desiredTag != tc.desiredTag {
t.Fatalf("Given constraint: \"%s\", expected result tag: \"%s\", but received \"%s\"", tc.tagConstraint, tc.desiredTag, desiredTag)
}
}
}

func TestGetLatestAcceptableTagOnEmptyConstraint(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 37cb093

Please sign in to comment.