commit 7d36ec54f8bf5ac6407ad34aea8a150f349f6c7a Author: Stephen McQuay (smcquay) Date: Sun Nov 26 09:29:04 2017 -0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22d0d82 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..725d703 --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,15 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + name = "github.com/blang/semver" + packages = ["."] + revision = "2ee87856327ba09384cabd113bc6b5d174e9ec0f" + version = "v3.5.1" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "cf275d59cebaefe47525b0117f6f4cf189794057612c947e60cfe7e280335ee6" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..138183d --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,26 @@ + +# Gopkg.toml example +# +# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" + + +[[constraint]] + name = "github.com/blang/semver" + version = "3.5.1" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f1caddd --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +MIT License +Copyright (c) 2016 Stephen McQuay + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..62a9ea4 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# svsort + +Sort lines of text that begin with semver. diff --git a/main.go b/main.go new file mode 100644 index 0000000..05681d3 --- /dev/null +++ b/main.go @@ -0,0 +1,55 @@ +// command svsort sorts lines of text by semver at beginning of line +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "sort" + "strings" + + "github.com/blang/semver" +) + +type line struct { + v semver.Version + line string +} + +type lines []line + +func (l lines) Len() int { return len(l) } +func (l lines) Swap(i, j int) { l[i], l[j] = l[j], l[i] } +func (l lines) Less(i, j int) bool { return l[i].v.LT(l[j].v) } + +func main() { + s := bufio.NewScanner(os.Stdin) + + ls := lines{} + for s.Scan() { + f := strings.Fields(s.Text()) + if len(f) < 2 { + continue + } + v, err := semver.ParseTolerant(f[0]) + if err != nil { + log.Printf("%v: %q", err, s.Text()) + continue + } + l := line{ + v: v, + line: s.Text(), + } + ls = append(ls, l) + } + + if err := s.Err(); err != nil { + log.Fatalf("scan: %v") + } + + sort.Sort(ls) + for _, l := range ls { + fmt.Printf("%v\n", l.line) + } +}