/
gomodctl.go
147 lines (122 loc) · 3.94 KB
/
gomodctl.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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"runtime/debug"
"syscall"
"github.com/beatlabs/gomodctl/internal/cmd/check"
"github.com/beatlabs/gomodctl/internal/cmd/info"
licensecmd "github.com/beatlabs/gomodctl/internal/cmd/license"
scancmd "github.com/beatlabs/gomodctl/internal/cmd/scan"
"github.com/beatlabs/gomodctl/internal/cmd/search"
updatecmd "github.com/beatlabs/gomodctl/internal/cmd/update"
"github.com/beatlabs/gomodctl/internal/godoc"
"github.com/beatlabs/gomodctl/internal/license"
"github.com/beatlabs/gomodctl/internal/module"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var ro RootOptions
var version string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "gomodctl",
Short: "Search, Check and Update Go modules.",
Long: `gomodctl is a Go tool that provides interactive search, check and update features for Go modules.
Example:
gomodctl search mongo
This command will search in all public Go packages and return matching results for term "mongo".`,
Version: version,
}
// RootOptions is exported.
type RootOptions struct {
config string
registry string
json bool
path string
}
// Execute is exported.
func Execute() {
// Since this application is a one-shot execution there is no point of making GC available and slow down the application.
// https://www.dotconferences.com/2019/03/bryan-boreham-go-tune-your-memory
// https://www.youtube.com/watch?v=uyifh6F_7WM
debug.SetGCPercent(-1)
ctx, cancel := context.WithCancel(context.Background())
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go func() {
select {
case <-signals:
cancel()
case <-ctx.Done():
}
}()
ro.config = viper.GetString("config")
ro.registry = viper.GetString("registry")
gd := godoc.NewClient(ctx)
checker := module.Checker{Ctx: ctx}
updater := module.Updater{Ctx: ctx}
licenseChecker, err := license.NewChecker(ctx)
scanner := module.Scanner{Ctx: ctx}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Add sub-commands
rootCmd.AddCommand(search.NewCmdSearch(gd))
rootCmd.AddCommand(info.NewCmdInfo(gd))
rootCmd.AddCommand(check.NewCmdCheck(&checker))
rootCmd.AddCommand(updatecmd.NewCmdUpdate(&updater))
rootCmd.AddCommand(licensecmd.NewCmdLicense(licenseChecker))
rootCmd.AddCommand(scancmd.NewCmdScan(&scanner))
if err := rootCmd.ExecuteContext(ctx); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&ro.config, "config", "", "config file (default is $HOME/gomodctl.yml)")
rootCmd.PersistentFlags().StringVar(&ro.registry, "registry", "", "URI of the registry to be used for search")
rootCmd.PersistentFlags().BoolVar(&ro.json, "json", false, "Print JSON result")
rootCmd.PersistentFlags().StringVar(&ro.path, "path", "", "Optional go.mod parent directory")
viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
viper.BindPFlag("registry", rootCmd.PersistentFlags().Lookup("registry"))
viper.BindPFlag("json", rootCmd.PersistentFlags().Lookup("json"))
viper.BindPFlag("path", rootCmd.PersistentFlags().Lookup("path"))
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
viper.SetConfigType("yaml")
if ro.config != "" {
// Use config file from the flag.
viper.SetConfigFile(ro.config)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if ro.path != "" {
viper.AddConfigPath(ro.path)
}
viper.AddConfigPath(".")
viper.AddConfigPath(home)
viper.SetConfigName("gomodctl")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
log.Println("Using config file:", viper.ConfigFileUsed())
} else {
log.Println(err)
}
}
func main() {
Execute()
}