Skip to content

Commit

Permalink
sort version by semantic (#73)
Browse files Browse the repository at this point in the history
Signed-off-by: Paco Xu <[email protected]>

Signed-off-by: Paco Xu <[email protected]>
  • Loading branch information
pacoxu authored Nov 4, 2022
1 parent 74cf170 commit 0521279
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion corefile-tool/cmd/validversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestNewValidVersionsCmd(t *testing.T) {
{
name: "Works without error",
expectedOutput: `The following are valid CoreDNS versions:
1.1.3, 1.1.4, 1.10.0, 1.2.0, 1.2.1, 1.2.2, 1.2.3, 1.2.4, 1.2.5, 1.2.6, 1.3.0, 1.3.1, 1.4.0, 1.5.0, 1.5.1, 1.5.2, 1.6.0, 1.6.1, 1.6.2, 1.6.3, 1.6.4, 1.6.5, 1.6.6, 1.6.7, 1.6.9, 1.7.0, 1.7.1, 1.8.0, 1.8.3, 1.8.4, 1.8.5, 1.8.6, 1.8.7, 1.9.0, 1.9.1, 1.9.2, 1.9.3, 1.9.4
1.1.3, 1.1.4, 1.2.0, 1.2.1, 1.2.2, 1.2.3, 1.2.4, 1.2.5, 1.2.6, 1.3.0, 1.3.1, 1.4.0, 1.5.0, 1.5.1, 1.5.2, 1.6.0, 1.6.1, 1.6.2, 1.6.3, 1.6.4, 1.6.5, 1.6.6, 1.6.7, 1.6.9, 1.7.0, 1.7.1, 1.8.0, 1.8.3, 1.8.4, 1.8.5, 1.8.6, 1.8.7, 1.9.0, 1.9.1, 1.9.2, 1.9.3, 1.9.4, 1.10.0
`,
expectedError: false,
},
Expand Down
23 changes: 22 additions & 1 deletion migration/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"

"github.com/coredns/corefile-migration/migration/corefile"
)
Expand Down Expand Up @@ -420,7 +422,26 @@ func ValidVersions() []string {
for vStr := range Versions {
vStrs = append(vStrs, vStr)
}
sort.Strings(vStrs)
sort.Slice(vStrs, func(i, j int) bool {
iSegs := strings.Split(vStrs[i], ".")
jSegs := strings.Split(vStrs[j], ".")
for k, iSeg := range iSegs {
if iSeg == jSegs[k] {
continue
}
iInt, err := strconv.Atoi(iSeg)
if err != nil {
panic(err)
}
jInt, err := strconv.Atoi(jSegs[k])
if err != nil {
panic(err)
}
return iInt < jInt
}
return false
})

return vStrs
}

Expand Down

0 comments on commit 0521279

Please sign in to comment.