Skip to content

Commit

Permalink
fix: correct version detection for > 10
Browse files Browse the repository at this point in the history
Fixes #76
  • Loading branch information
markelog committed Oct 19, 2022
1 parent 01c9338 commit 353e381
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bin/ec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/markelog/eclectica/cmd/commands/install"
"github.com/markelog/eclectica/cmd/commands/ls"
"github.com/markelog/eclectica/cmd/commands/path"
"github.com/markelog/eclectica/cmd/commands/remove-everything"
removeEverything "github.com/markelog/eclectica/cmd/commands/remove-everything"
"github.com/markelog/eclectica/cmd/commands/rm"
"github.com/markelog/eclectica/cmd/commands/version"
)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/blang/semver v3.5.1+incompatible
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9
github.com/creack/pty v1.1.15 // indirect
github.com/davecgh/go-spew v1.1.1
github.com/dustin/go-humanize v1.0.0
github.com/emirpasic/gods v1.12.0
github.com/fatih/color v1.12.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
github.com/creack/pty v1.1.15 h1:cKRCLMj3Ddm54bKSpemfQ8AtYFBhAI2MPmdys22fBdc=
github.com/creack/pty v1.1.15/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
Expand Down
25 changes: 15 additions & 10 deletions versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"sort"
"strings"

"github.com/go-errors/errors"
"github.com/blang/semver"
"github.com/go-errors/errors"
hversion "github.com/hashicorp/go-version"
)

Expand Down Expand Up @@ -54,7 +54,7 @@ func ComposeMajors(versions []string) map[string][]string {
// ComposeMinors composes minor version to map object of arrays from array
func ComposeMinors(versions []string) map[string][]string {
result := map[string][]string{}
firstPart := regexp.MustCompile("(\\d)+\\.(\\d+)")
firstPart := regexp.MustCompile("(\\d+)\\.(\\d+)")

for _, version := range versions {
checkVersions := firstPart.FindAllStringSubmatch(version, 1)
Expand All @@ -78,9 +78,12 @@ func ComposeMinors(versions []string) map[string][]string {
}

// GetKeys returns array of keys
// map[string][]string{"4.x": []string{}, "0.x": []string{"0.8.2"}}
//
// map[string][]string{"4.x": []string{}, "0.x": []string{"0.8.2"}}
//
// gets you:
// string{"0.x", "4.x"}
//
// string{"0.x", "4.x"}
func GetKeys(versions map[string][]string) []string {
result := []string{}
compare := map[string]string{}
Expand Down Expand Up @@ -118,11 +121,14 @@ func GetKeys(versions map[string][]string) []string {
}

// GetElements gets all elements for provided range of version in sorted semver format:
// map[string][]string{
// "1.x": string{1.1, 1.1-beta}
// }
//
// map[string][]string{
// "1.x": string{1.1, 1.1-beta}
// }
//
// Will return:
// [1.1.0, 1.1.0-beta]
//
// [1.1.0, 1.1.0-beta]
func GetElements(key string, versions map[string][]string) []string {
for version := range versions {
if version == key {
Expand All @@ -143,11 +149,10 @@ func Complete(version string, vers []string) (string, error) {
if len(vers) == 0 {
return "", errors.New("No versions available")
}

return Latest(version, vers)
}

// IsPartial checks if provided version is full semver version
// IsPartial checks if provided version is not full semver version
func IsPartial(version string) bool {
if version == "latest" {
return true
Expand Down
11 changes: 11 additions & 0 deletions versions/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ var _ = Describe("versions", func() {
Expect(test).To(Equal("6.4.2"))
})

It("support for partial minor version > 10", func() {
version := "16.4"
versions := []string{
"16.1.0", "15.2.0", "16.2.0", "16.8.3", "16.4.2", "16.4.0",
}

test, _ := Complete(version, versions)

Expect(test).To(Equal("16.4.2"))
})

It("shouldn't do anything for full version", func() {
version := "6.1.1"
versions := []string{
Expand Down

0 comments on commit 353e381

Please sign in to comment.