-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
171 lines (138 loc) · 3.48 KB
/
main.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"flag"
"fmt"
"io"
"os"
"strings"
"golang.org/x/mod/modfile"
)
var Version = "v0.0.0"
const usage = `
NAME
go-mod-what - get the version of a package in a go.mod file
SYNOPSIS
go-mod-what [options] <package> [<package> ...]
OPTIONS
`
const usageExample = `
EXAMPLES
To get the version of a package:
$ go-mod-what github.com/gorilla/mux
github.com/gorilla/mux v1.8.0
To get the version of multiple packages:
$ go-mod-what github.com/gorilla/mux github.com/gorilla/schema
github.com/gorilla/context v1.1.1
github.com/gorilla/mux v1.8.0
To get the version of multiple packages with a wildcard:
$ go-mod-what github.com/gorilla/*
github.com/gorilla/context v1.1.1
github.com/gorilla/mux v1.8.0
To get the version of a package with a custom go.mod file path:
$ go-mod-what -modfile ../go.mod github.com/gorilla/mux
github.com/gorilla/mux v1.8.0
To get the version of a package with only the version:
$ go-mod-what -only-version github.com/gorilla/mux
v1.8.0
`
type Package struct {
Path string
Version string
}
func main() {
modfilePath := flag.String("modfile", "./go.mod", "path to go.mod file")
help := flag.Bool("help", false, "show help")
version := flag.Bool("version", false, "show version")
onlyVersion := flag.Bool("only-version", false, "only print the version")
flag.Parse()
if *help {
flag.Usage = printUsage(os.Stdout)
flag.Usage()
return
}
if *version {
fmt.Fprint(os.Stdout, Version+"\n")
return
}
if len(flag.Args()) == 0 {
printError("no package provided", nil, true)
}
if *modfilePath == "" {
printError("go.mod file not provided", nil, true)
}
b, err := os.ReadFile(*modfilePath)
if err != nil {
printError("failed to read go.mod file", err)
return
}
m, err := modfile.Parse(*modfilePath, b, nil)
if err != nil {
printError("failed to parse go.mod file", err)
}
pkgs := flag.Args()
pkgVers, pkgFound := findPackages(m, pkgs)
for _, p := range pkgVers {
if *onlyVersion {
fmt.Fprintln(os.Stdout, p.Version)
continue
}
fmt.Fprintln(os.Stdout, p.Path+" "+p.Version)
}
for i, f := range pkgFound {
if !f {
fmt.Fprintf(os.Stderr, "%s not found\n", pkgs[i])
}
}
}
func findPackages(m *modfile.File, pkgs []string) ([]Package, []bool) {
var pkgVers []Package
pkgFound := make([]bool, len(pkgs))
for _, r := range m.Require {
for i, p := range pkgs {
if !compareRequire(p, r.Mod.Path) {
continue
}
pkgVers = append(pkgVers, Package{Path: r.Mod.Path, Version: r.Mod.Version})
pkgFound[i] = true
}
}
return pkgVers, pkgFound
}
// compareRequire compares module path with a string
func compareRequire(a string, b string) bool {
// exact match
if strings.Compare(a, b) == 0 {
return true
}
// wildcard
if strings.Contains(a, "*") && strings.HasPrefix(b, strings.TrimSuffix(a, "*")) {
return true
}
return false
}
func printUsage(w io.Writer) func() {
return func() {
flag.CommandLine.SetOutput(w)
fmt.Fprint(w, usage)
flag.PrintDefaults()
fmt.Fprint(w, usageExample)
}
}
// printError prints an error message and exits
func printError(s string, err error, with ...bool) {
if err == nil {
fmt.Fprint(os.Stderr, s+"\n\n")
} else {
fmt.Fprintf(os.Stderr, s+": %s\n\n", err)
}
if len(with) > 0 && with[0] {
flag.Usage = printUsage(os.Stderr)
flag.Usage()
}
// panic if in test mode to simulate os.Exit
if os.Getenv("TEST_MODE") == "true" {
panic(s)
}
// exit with status 1
os.Exit(1)
}