Skip to content

Commit

Permalink
Breaking change but its only been out for a few hours
Browse files Browse the repository at this point in the history
sorts by default, no need for a constraint
adds reverse, and by default sorts the same way unix sort does, least to greatest
  • Loading branch information
samrees committed Jul 29, 2021
1 parent a85f50b commit b526d6d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,44 @@ This only exists because more people dont adopt: https://samver.org/ which is so

## Usage

sorts versions you pass to it over a pipe.

```
Usage of ./semversort:
-constraint string
list versions greatest to least, if versions pass given constraint.
list versions only if versions pass given constraint
-greatest
display the greatest version for a given list
-least
display the least version for a given list
-reverse
lists verions greatest to least
```

example:
examples:

`echo -e "1.2.3\n3.4.5" | ./semversort -constraint='>=0.0.0'`
```
[~]> echo -e "1.2.3\n4.5.6\n2.9.100+woot\n0.3.1"
1.2.3
4.5.6
2.9.100+woot
0.3.1
[~]> echo -e "1.2.3\n4.5.6\n2.9.100+woot\n0.3.1" | ./semversort
0.3.1
1.2.3
2.9.100+woot
4.5.6
[~]> echo -e "1.2.3\n4.5.6\n2.9.100+woot\n0.3.1" | ./semversort -constraint='>=2.0.0'
2.9.100+woot
4.5.6
[~]> echo -e "1.2.3\n4.5.6\n2.9.100+woot\n0.3.1" | ./semversort -reverse
4.5.6
2.9.100+woot
1.2.3
0.3.1
[~]> echo -e "1.2.3\n4.5.6\n2.9.100+woot\n0.3.1" | ./semversort -greatest
4.5.6
```

more info on semver constraints:

Expand Down
2 changes: 2 additions & 0 deletions bin/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GOOS=linux GOARCH=amd64 go build -o semversort-linux-amd64
GOOS=darwin GOARCH=amd64 go build -o semversort-darwin-amd64
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
30 changes: 18 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@ func main() {
greatestFlag bool
leastFlag bool
constraint string
reverseFlag bool
)

flag.BoolVar(&greatestFlag, "greatest", false, "display the greatest version for a given list")
flag.BoolVar(&leastFlag, "least", false, "display the least version for a given list")
flag.StringVar(&constraint, "constraint", "", "list versions greatest to least, if versions pass given constraint.")
flag.StringVar(&constraint, "constraint", "", "list versions only if versions pass given constraint")
flag.BoolVar(&reverseFlag, "reverse", false, "lists verions greatest to least")

flag.Parse()

if !greatestFlag && !leastFlag && constraint == "" {
flag.Usage()
os.Exit(0)
}

reader := bufio.NewReader(os.Stdin)
var rawVersions []string

Expand Down Expand Up @@ -56,32 +53,41 @@ func main() {
versions[i] = v
}

// greatest to least
sort.Sort(sort.Reverse(semver.Collection(versions)))
// least to greatest
sort.Sort(semver.Collection(versions))

if greatestFlag {
fmt.Println(versions[0])
fmt.Println(versions[len(versions)-1])
os.Exit(0)
}

if leastFlag {
fmt.Println(versions[len(versions)-1])
fmt.Println(versions[0])
os.Exit(0)
}

//greatest to least
if reverseFlag {
sort.Sort(sort.Reverse(semver.Collection(versions)))
}

if constraint != "" {
c, err := semver.NewConstraint(constraint)
if err != nil {
fmt.Printf("Error parsing constraint '%s': %s\n", constraint, err.Error())
os.Exit(3)
}

for _, v := range versions {
status, _ := c.Validate(v)
if status {
fmt.Println(v)
}
}
os.Exit(0)
} else {
for _, v := range versions {
fmt.Println(v)
}
}

os.Exit(0)
}

0 comments on commit b526d6d

Please sign in to comment.