Stephen McQuay 1 year ago
commit
7d36ec54f8
Signed by: Stephen McQuay (smcquay) <stephen@mcquay.me> GPG Key ID: 4E4B72F479BA3CE5
6 changed files with 120 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 15
    0
      Gopkg.lock
  3. 26
    0
      Gopkg.toml
  4. 20
    0
      LICENSE
  5. 3
    0
      README.md
  6. 55
    0
      main.go

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
1
+vendor

+ 15
- 0
Gopkg.lock View File

@@ -0,0 +1,15 @@
1
+# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
2
+
3
+
4
+[[projects]]
5
+  name = "github.com/blang/semver"
6
+  packages = ["."]
7
+  revision = "2ee87856327ba09384cabd113bc6b5d174e9ec0f"
8
+  version = "v3.5.1"
9
+
10
+[solve-meta]
11
+  analyzer-name = "dep"
12
+  analyzer-version = 1
13
+  inputs-digest = "cf275d59cebaefe47525b0117f6f4cf189794057612c947e60cfe7e280335ee6"
14
+  solver-name = "gps-cdcl"
15
+  solver-version = 1

+ 26
- 0
Gopkg.toml View File

@@ -0,0 +1,26 @@
1
+
2
+# Gopkg.toml example
3
+#
4
+# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
5
+# for detailed Gopkg.toml documentation.
6
+#
7
+# required = ["github.com/user/thing/cmd/thing"]
8
+# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
9
+#
10
+# [[constraint]]
11
+#   name = "github.com/user/project"
12
+#   version = "1.0.0"
13
+#
14
+# [[constraint]]
15
+#   name = "github.com/user/project2"
16
+#   branch = "dev"
17
+#   source = "github.com/myfork/project2"
18
+#
19
+# [[override]]
20
+#  name = "github.com/x/y"
21
+#  version = "2.4.0"
22
+
23
+
24
+[[constraint]]
25
+  name = "github.com/blang/semver"
26
+  version = "3.5.1"

+ 20
- 0
LICENSE View File

@@ -0,0 +1,20 @@
1
+MIT License
2
+Copyright (c) 2016 Stephen McQuay
3
+
4
+Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+this software and associated documentation files (the "Software"), to deal in
6
+the Software without restriction, including without limitation the rights to
7
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8
+of the Software, and to permit persons to whom the Software is furnished to do
9
+so, subject to the following conditions:
10
+
11
+The above copyright notice and this permission notice shall be included in all
12
+copies or substantial portions of the Software.
13
+
14
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+SOFTWARE.

+ 3
- 0
README.md View File

@@ -0,0 +1,3 @@
1
+# svsort
2
+
3
+Sort lines of text that begin with semver.

+ 55
- 0
main.go View File

@@ -0,0 +1,55 @@
1
+// command svsort sorts lines of text by semver at beginning of line
2
+package main
3
+
4
+import (
5
+	"bufio"
6
+	"fmt"
7
+	"log"
8
+	"os"
9
+	"sort"
10
+	"strings"
11
+
12
+	"github.com/blang/semver"
13
+)
14
+
15
+type line struct {
16
+	v    semver.Version
17
+	line string
18
+}
19
+
20
+type lines []line
21
+
22
+func (l lines) Len() int           { return len(l) }
23
+func (l lines) Swap(i, j int)      { l[i], l[j] = l[j], l[i] }
24
+func (l lines) Less(i, j int) bool { return l[i].v.LT(l[j].v) }
25
+
26
+func main() {
27
+	s := bufio.NewScanner(os.Stdin)
28
+
29
+	ls := lines{}
30
+	for s.Scan() {
31
+		f := strings.Fields(s.Text())
32
+		if len(f) < 2 {
33
+			continue
34
+		}
35
+		v, err := semver.ParseTolerant(f[0])
36
+		if err != nil {
37
+			log.Printf("%v: %q", err, s.Text())
38
+			continue
39
+		}
40
+		l := line{
41
+			v:    v,
42
+			line: s.Text(),
43
+		}
44
+		ls = append(ls, l)
45
+	}
46
+
47
+	if err := s.Err(); err != nil {
48
+		log.Fatalf("scan: %v")
49
+	}
50
+
51
+	sort.Sort(ls)
52
+	for _, l := range ls {
53
+		fmt.Printf("%v\n", l.line)
54
+	}
55
+}

Loading…
Cancel
Save