-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (93 loc) · 2.45 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
package main
import (
"fmt"
"io"
"os"
"strings"
"time"
"github.com/Nivl/check-go-deps/modutil"
flag "github.com/spf13/pflag"
)
// Flags represents all the flags accepted by the CLI
type Flags struct {
CheckOldPkgs bool
CheckIndirect bool
IgnoredPkgs []string
}
// https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html
const (
ExitSuccess = 0
ExitFailure = 1
)
func main() {
os.Exit(run(os.Args, os.Stderr))
}
func run(args []string, out io.Writer) (exitStatus int) {
flags, err := parseFlags(args)
if err != nil {
fmt.Fprintln(out, fmt.Sprintf("could not parse the flags: %s", err.Error()))
return ExitFailure
}
modules, err := modutil.ParseCwd()
if err != nil {
fmt.Fprintln(out, fmt.Sprintf("could not parse the go.mod file: %s", err.Error()))
return ExitFailure
}
res := parseModules(flags, modules)
res.print(out)
if res.HasModules() {
return ExitFailure
}
return ExitSuccess
}
func parseFlags(args []string) (*Flags, error) {
flags := &Flags{}
fset := flag.NewFlagSet(args[0], flag.ContinueOnError)
fset.BoolVar(&flags.CheckOldPkgs, "old", false, "check for modules without updates for the last 6 months")
fset.BoolVar(&flags.CheckIndirect, "indirect", false, "check indirect modules")
fset.StringSliceVarP(&flags.IgnoredPkgs, "ignore", "i", []string{}, "coma separated list of packages to ignore")
return flags, fset.Parse(args)
}
func parseModules(f *Flags, modules []*modutil.Module) *Results {
res := &Results{}
for _, m := range modules {
// skip ignored packages
isIgnored := false
for _, pkg := range f.IgnoredPkgs {
if strings.HasPrefix(m.Path, pkg) {
isIgnored = true
break
}
}
if isIgnored {
continue
}
// skip indirects if we don't want them
if m.Indirect && !f.CheckIndirect {
continue
}
// Report if the package has been replaced
if m.Replace != nil {
res.Replaced = append(res.Replaced, m)
continue
}
// Report if the package has an update available
if m.Update != nil {
// It's possible that a tag appears as an "update" from a commit, even
// if that tag is older
if m.Time == nil || m.Update.Time == nil || m.Time.Before(*m.Update.Time) {
res.Updated = append(res.Updated, m)
continue
}
}
// Report if the package hasn't been updated in 6 months
if f.CheckOldPkgs && m.Time != nil {
sixMonths := 6 * 30 * 24 * time.Hour
if time.Since(*m.Time) >= sixMonths {
res.Old = append(res.Old, m)
continue
}
}
}
return res
}