This repository has been archived by the owner on Oct 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.go
153 lines (129 loc) · 3.07 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"regexp"
"strings"
"github.com/urfave/cli"
version "github.com/hashicorp/go-version"
)
var (
booleanMode bool
)
func processError(err error) {
if err != nil {
if booleanMode {
fmt.Print("false")
os.Exit(1)
} else {
log.Fatal(err)
}
}
}
// ExtractVersion parse string and extract version number
func ExtractVersion(str string) (string, error) {
patterns := [...]string{"version (\\d+.\\d+.d\\+)", "version (\\d[\\d\\.]*)", "(\\d[\\d\\.]*)"}
for _, pattern := range patterns {
re := regexp.MustCompile(pattern)
match := re.FindStringSubmatch(str)
if len(match) != 0 {
return match[1], nil
}
}
return "", fmt.Errorf("Can't extract version from \"%s\"", str)
}
func readFromPipe() (string, error) {
info, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
if info.Mode()&os.ModeCharDevice != 0 || info.Size() <= 0 {
return "", fmt.Errorf("The command is intended to work with pipes\nUsage: go version | version -b \">=1.9\"")
}
reader := bufio.NewReader(os.Stdin)
var result []string
for {
input, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
}
result = append(result, fmt.Sprintf("%c", input))
}
return strings.Join(result, ""), nil
}
func main() {
log.SetFlags(0)
app := cli.NewApp()
app.Name = "version"
app.Usage = "CLI command to verify versions and version constraints."
app.Version = "1.0.4"
app.Authors = []cli.Author{
cli.Author{
Name: "Ivan Diachenko",
Email: "[email protected]",
},
}
app.UsageText = "version [global options] constraints [version]"
app.Commands = []cli.Command{
{
Name: "parse",
Aliases: []string{"p"},
Usage: "parse version from arguments or pipe",
Action: func(c *cli.Context) error {
var versionString string
var err error
if c.NArg() > 0 {
versionString = strings.Join(c.Args(), "")
} else {
versionString, err = readFromPipe()
processError(err)
}
parsedVersion, err := ExtractVersion(versionString)
processError(err)
fmt.Println(parsedVersion)
return nil
},
},
}
app.Action = func(c *cli.Context) error {
var versionString string
var err error
if c.NArg() > 1 {
versionString = strings.Join(c.Args().Tail(), "")
} else {
versionString, err = readFromPipe()
processError(err)
}
checkVersion, err := ExtractVersion(versionString)
processError(err)
ver, err := version.NewVersion(checkVersion)
processError(err)
constraints, err := version.NewConstraint(c.Args().First())
processError(err)
if constraints.Check(ver) {
if !booleanMode {
log.Printf("%s satisfies constraints %s", ver, constraints)
} else {
fmt.Print("true")
os.Exit(0)
}
} else {
processError(fmt.Errorf("%s doesn't satisfies constraints %s", ver, constraints))
}
return nil
}
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "boolean, b",
Usage: "boolean mode return '1' or '0' and always exit with 0",
Destination: &booleanMode,
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}