From 5779770b0b572e31c66984226b80f84a85479b1f Mon Sep 17 00:00:00 2001 From: Dr Nic Williams Date: Sat, 25 Apr 2015 12:46:22 -0700 Subject: [PATCH] [in] create /version file based on git tag name --- README.md | 5 ++++- in_command.go | 7 +++++++ in_command_test.go | 12 +++++++++++- versions.go | 10 ++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 versions.go diff --git a/README.md b/README.md index 0583482..84ae43e 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,10 @@ Fetches artifacts from the given release version. If the version is not specified, the latest version is chosen using [semver](http://semver.org) semantics. -Creates a file `tag` containing the tag name/version of the release being fetched. +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 diff --git a/in_command.go b/in_command.go index f5fee94..0d15631 100644 --- a/in_command.go +++ b/in_command.go @@ -65,6 +65,13 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) { 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 ca64477..668ace8 100644 --- a/in_command_test.go +++ b/in_command_test.go @@ -236,7 +236,7 @@ var _ = Describe("In Command", func() { )) }) - It("stores tag in a file", func() { + It("stores git tag in a file", func() { _, err := os.Stat(filepath.Join(destDir, "tag")) Ω(err).ShouldNot(HaveOccurred()) @@ -246,6 +246,16 @@ var _ = Describe("In Command", func() { Ω(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/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] +}