Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for basic version requirements #63

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/aws/aws-sdk-go v1.16.23
github.com/blang/semver v3.5.1+incompatible
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.7.0
github.com/ghodss/yaml v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ github.com/aws/aws-sdk-go v0.0.0-20190104231327-923b7b1b0525 h1:qc2jx43HwaJSlY5U
github.com/aws/aws-sdk-go v0.0.0-20190104231327-923b7b1b0525/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.16.23 h1:MwBOBeez0XEFVh6DCc888X+nHVBCjUDLnnWXSGGWUgM=
github.com/aws/aws-sdk-go v1.16.23/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
50 changes: 47 additions & 3 deletions iamy.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/blang/semver"
"gopkg.in/alecthomas/kingpin.v2"
)

const versionTooOldError = `Your version of IAMy (%s) is out of date compared to what the local
project expects. You should upgrade to %s to use this project.`

var (
Version string = "dev"
defaultDir string
dryRun *bool
Version string = "dev"
defaultDir string
dryRun *bool
versionFileName string = ".iamy-version"
)

type logWriter struct{ *log.Logger }
Expand Down Expand Up @@ -60,6 +68,8 @@ func main() {
log.SetOutput(ioutil.Discard)
}

performVersionChecks()

switch cmd {
case push.FullCommand():
PushCommand(ui, PushCommandInput{
Expand All @@ -85,3 +95,37 @@ func init() {
}
defaultDir = filepath.Clean(dir)
}

func performVersionChecks() {
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
currentIAMyVersion, _ := semver.Make(strings.TrimPrefix(Version, "v"))
log.Printf("current version is %s", currentIAMyVersion)

fileBytes, err := ioutil.ReadFile(versionFileName)
if err != nil {
if !os.IsNotExist(err) {
log.Printf("could not read version file %s, ignoring: %s", versionFileName, err)
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
}
return
}

log.Printf("%s detected", versionFileName)
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved

if len(fileBytes) <= 1 {
log.Printf("%s is empty, skipping.", versionFileName)
return
}

fileContents := string(fileBytes)
re := regexp.MustCompile(`\d\.\d+\.\d`)
match := re.FindStringSubmatch(fileContents)
localDesiredVersion, _ := semver.Make(match[0])
log.Printf("local project wants version %s", localDesiredVersion)
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved

// We don't want to notify users if the `Version` is "dev" as it's not
// actually too old. It could be that they are running non-released
// versions.
if Version != "dev" && currentIAMyVersion.LE(localDesiredVersion) {
fmt.Printf(versionTooOldError, currentIAMyVersion, localDesiredVersion)
os.Exit(1)
}
}