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

feat: mimic how tgenv searches for a version file #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 22 additions & 12 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,31 @@ func CreateRecentFile(requestedVersion string) {
}

// ValidVersionFormat : returns valid version format
/* For example: 0.1.2 = valid
// For example: 0.1.2-beta1 = valid
// For example: 0.1.2-alpha = valid
// For example: a.1.2 = invalid
// For example: 0.1. 2 = invalid
/*
For example:
- 0.1.2 = valid
- latest = valid
- latest:^0.30 = valid
- a.1.2 = invalid
- 0.1. 2 = invalid
*/
func ValidVersionFormat(version string) bool {

// Getting versions from body; should return match /X.X.X-@/ where X is a number,@ is a word character between a-z or A-Z
// Follow https://semver.org/spec/v1.0.0-beta.html
// Check regular expression at https://rubular.com/r/ju3PxbaSBALpJB
semverRegex := regexp.MustCompile(`^(\d+\.\d+\.\d+)(-[a-zA-z]+\d*)?$`)

if !semverRegex.MatchString(version) {
fmt.Println("Invalid terragrunt version format. Format should be #.#.# or #.#.#-@# where # is numbers and @ is word characters. For example, 0.11.7 and 0.11.9-beta1 are valid versions")
semverRegex := regexp.MustCompile(`^(?:\d+\.){2}\d+$`)
latestRegex := regexp.MustCompile(`^latest\:(.*)$`)

if version == "latest" {
return true
} else if latestRegex.MatchString(version) {
return true
} else if semverRegex.MatchString(version) {
return true
} else {
fmt.Printf("Invalid terragrunt version format %q. Format should be #.#.#, latest, or latest:<regex>. For example, 0.11.7 and latest:^0.34 are valid versions", version)
return false
}

return semverRegex.MatchString(version)
Expand Down Expand Up @@ -227,7 +237,7 @@ func Install(tgversion string, usrBinPath string, mirrorURL string) string {

/* set symlink to desired version */
CreateSymlink(installFileVersionPath, binPath)
fmt.Printf("Switched terragrunt to version %q \n", tgversion)
fmt.Printf("Switched terragrunt to version %q\n", tgversion)
//AddRecent(tgversion) //add to recent file for faster lookup
os.Exit(0)
return ""
Expand All @@ -246,15 +256,15 @@ func InstallableBinLocation(userBinPath string) string {
binDir := Path(userBinPath) //get path directory from binary path
binPathExist := CheckDirExist(binDir) //the default is /usr/local/bin but users can provide custom bin locations

if binPathExist == true { //if bin path exist - check if we can write to to it
if binPathExist { //if bin path exist - check if we can write to to it

binPathWritable := false //assume bin path is not writable
if runtime.GOOS != "windows" {
binPathWritable = CheckDirWritable(binDir) //check if is writable on ( only works on LINUX)
}

// IF: "/usr/local/bin" or `custom bin path` provided by user is non-writable, (binPathWritable == false), we will attempt to install terragrunt at the ~/bin location. See ELSE
if binPathWritable == false {
if !binPathWritable {

homeBinExist := CheckDirExist(filepath.Join(usr.HomeDir, "bin")) //check to see if ~/bin exist
if homeBinExist { //if ~/bin exist, install at ~/bin/terragrunt
Expand Down
19 changes: 8 additions & 11 deletions lib/list_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ func VersionExist(val interface{}, array interface{}) (exists bool) {
s := reflect.ValueOf(array)

for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
if reflect.DeepEqual(val, s.Index(i).Interface()) {
exists = true
return exists
}
}
}

if !exists {
fmt.Println("Requested version does not exist")
fmt.Printf("Requested version %q does not exist\n", val)
}

return exists
Expand All @@ -41,8 +40,8 @@ func RemoveDuplicateVersions(elements []string) []string {
result := []string{}

for _, val := range elements {
versionOnly := strings.Trim(val, " *recent")
if encountered[versionOnly] == true {
versionOnly := strings.TrimSuffix(val, " *recent")
if encountered[versionOnly] {
// Do not add duplicate.
} else {
// Record this element as an encountered element.
Expand All @@ -58,7 +57,7 @@ func RemoveDuplicateVersions(elements []string) []string {
func GetAppList(gruntURLPage string) []string {

gswitch := http.Client{
Timeout: time.Second * 10, // Maximum of 10 secs [decresing this seem to fail]
Timeout: time.Second * 10, // Maximum of 10 secs [decreasing this seems to fail]
}

req, err := http.NewRequest(http.MethodGet, gruntURLPage, nil)
Expand All @@ -70,20 +69,18 @@ func GetAppList(gruntURLPage string) []string {

res, getErr := gswitch.Do(req)
if getErr != nil {
log.Fatal("Unable to make request Please try again.")
log.Fatal("Unable to make request. Please try again.")
}

body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Println("Unable to get release from repo ", string(body))
log.Fatal(readErr)
log.Fatalf("Unable to get release from repo %s:\n%s", body, readErr)
}

var repo ListVersion
jsonErr := json.Unmarshal(body, &repo)
if jsonErr != nil {
log.Println("Unable to get release from repo ", string(body))
log.Fatal(jsonErr)
log.Fatalf("Unable to get release from repo %s:\n%s", body, jsonErr)
}

return repo.Versions
Expand Down
17 changes: 3 additions & 14 deletions lib/list_versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestValidVersionFormat(t *testing.T) {
log.Fatalf("Failed to verify version format: %s\n", version)
}

version = "1.11.9-beta1"
version = "latest"

valid = lib.ValidVersionFormat(version)

Expand All @@ -90,7 +90,7 @@ func TestValidVersionFormat(t *testing.T) {
log.Fatalf("Failed to verify version format: %s\n", version)
}

version = "0.12.0-rc2"
version = "latest:^0.35"

valid = lib.ValidVersionFormat(version)

Expand All @@ -100,7 +100,7 @@ func TestValidVersionFormat(t *testing.T) {
log.Fatalf("Failed to verify version format: %s\n", version)
}

version = "1.11.4-boom"
version = "latest:^0.35.13"

valid = lib.ValidVersionFormat(version)

Expand All @@ -109,15 +109,4 @@ func TestValidVersionFormat(t *testing.T) {
} else {
log.Fatalf("Failed to verify version format: %s\n", version)
}

version = "1.11.4-1"

valid = lib.ValidVersionFormat(version)

if valid == false {
t.Logf("Invalid version format : %s (expected)", version)
} else {
log.Fatalf("Failed to verify version format: %s\n", version)
}

}