diff --git a/README.md b/README.md index 6ff426c..84ae43e 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,11 @@ Fetches artifacts from the given release version. If the version is not specified, the latest version is chosen using [semver](http://semver.org) semantics. +Also creates the following files: + +* `tag` containing the git tag name of the release being fetched. +* `version` containing the version determined by the git tag of the release being fetched. + #### Parameters * `globs`: *Optional.* A list of globs for files that will be downloaded from diff --git a/check_command.go b/check_command.go index ae6cb83..4731e93 100644 --- a/check_command.go +++ b/check_command.go @@ -72,12 +72,12 @@ func (r byVersion) Less(i, j int) bool { return false } - first, err := semver.New(dropLeadingAlpha(*r[i].TagName)) + first, err := semver.New(determineVersionFromTag(*r[i].TagName)) if err != nil { return true } - second, err := semver.New(dropLeadingAlpha(*r[j].TagName)) + second, err := semver.New(determineVersionFromTag(*r[j].TagName)) if err != nil { return false } diff --git a/in_command.go b/in_command.go index 9da0ec4..0d15631 100644 --- a/in_command.go +++ b/in_command.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "io" + "io/ioutil" "net/http" "os" "path/filepath" @@ -58,6 +59,19 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) { return InResponse{}, fmt.Errorf("could not find release with tag: %s", request.Version.Tag) } + tagPath := filepath.Join(destDir, "tag") + err = ioutil.WriteFile(tagPath, []byte(*foundRelease.TagName), 0644) + if err != nil { + return InResponse{}, err + } + + version := determineVersionFromTag(*foundRelease.TagName) + versionPath := filepath.Join(destDir, "version") + err = ioutil.WriteFile(versionPath, []byte(version), 0644) + if err != nil { + return InResponse{}, err + } + assets, err := c.github.ListReleaseAssets(foundRelease) if err != nil { return InResponse{}, err diff --git a/in_command_test.go b/in_command_test.go index 8cc0473..668ace8 100644 --- a/in_command_test.go +++ b/in_command_test.go @@ -236,6 +236,26 @@ var _ = Describe("In Command", func() { )) }) + It("stores git tag in a file", func() { + _, err := os.Stat(filepath.Join(destDir, "tag")) + Ω(err).ShouldNot(HaveOccurred()) + + tag, err := ioutil.ReadFile(filepath.Join(destDir, "tag")) + Ω(err).ShouldNot(HaveOccurred()) + + Ω(string(tag)).Should(Equal("v0.35.0")) + }) + + It("stores version in a file", func() { + _, err := os.Stat(filepath.Join(destDir, "version")) + Ω(err).ShouldNot(HaveOccurred()) + + version, err := ioutil.ReadFile(filepath.Join(destDir, "version")) + Ω(err).ShouldNot(HaveOccurred()) + + Ω(string(version)).Should(Equal("0.35.0")) + }) + It("fetches from the latest release", func() { _, err := os.Stat(filepath.Join(destDir, "example.txt")) Ω(err).ShouldNot(HaveOccurred()) diff --git a/strings.go b/strings.go deleted file mode 100644 index fe9025f..0000000 --- a/strings.go +++ /dev/null @@ -1,13 +0,0 @@ -package resource - -import "unicode" - -func dropLeadingAlpha(s string) string { - for i, r := range s { - if !unicode.IsLetter(r) { - return s[i:] - } - } - - return "" -} diff --git a/versions.go b/versions.go new file mode 100644 index 0000000..47d3448 --- /dev/null +++ b/versions.go @@ -0,0 +1,10 @@ +package resource + +import "regexp" + +// determineVersionFromTag converts git tags v1.2.3 into semver 1.2.3 values +func determineVersionFromTag(tag string) string { + re := regexp.MustCompile("v?([^v].*)") + matches := re.FindStringSubmatch(tag) + return matches[1] +}