-
Notifications
You must be signed in to change notification settings - Fork 0
/
ote.go
111 lines (97 loc) · 2.44 KB
/
ote.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
// ote: updates a package's go.mod file with a comment next to all dependencies that are test dependencies; identifying them as such.
//
// It maybe useful in places where it is important to audit all dependencies that are going to run in production.
//
// Install:
//
// go install github.com/komuw/ote@latest
//
// Usage:
//
// ote .
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
)
// TODO: better errors
// Usage:
//
// 1.
// go run . -f testdata/modfiles/mod1/ -r
//
// 2.
// export export GOPACKAGESDEBUG=true && \
// go run . -f testdata/modfiles/mod1/ -r
func main() {
f, r, v := cli()
if v {
// show version
fmt.Println(version())
return
}
err := run(f, os.Stdout, r)
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}
func run(fp string, w io.Writer, readonly bool) error {
gomodFile := filepath.Join(fp, "go.mod")
f, errM := getModFile(gomodFile)
if errM != nil {
return errM
}
// `f.Cleanup` will be called inside `writeMod`
trueTestModules, errT := getTestModules(fp)
if errT != nil {
return errT
}
if err := updateMod(trueTestModules, f); err != nil {
return err
}
if err := writeMod(f, gomodFile, w, readonly); err != nil {
return err
}
return nil
}
func cli() (string, bool, bool) {
var v bool
var f string
var r bool
flag.Usage = func() {
_, _ = fmt.Fprintf(flag.CommandLine.Output(),
`ote updates a packages go.mod file with a comment next to all dependencies that are test dependencies; identifying them as such.
Usage:
-f string # path to directory containing the go.mod file. By default, it uses the current directory. (default ".")
-r bool # (readonly) write to stdout instead of updating go.mod file.
-v bool # display version of ote in use.
examples:
ote . # update go.mod in the current directory
ote -f /tmp/someDir # update go.mod in the /tmp/someDir directory
ote -r # (readonly) write to stdout instead of updating go.mod file.
ote -f /tmp/someDir -r # (readonly) write to stdout instead of updating go.mod file in the /tmp/someDir directory.
`)
}
flag.StringVar(
&f,
"f",
".",
"path to directory containing the go.mod file. By default, it uses the current directory.")
flag.BoolVar(
&r,
"r",
false,
"(readonly) display how the updated go.mod file would look like, without actually updating the file.")
flag.BoolVar(
&v,
"v",
false,
"display version of ote in use.")
flag.Parse()
return f, r, v
}