-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (41 loc) · 1.13 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
package main
import (
"os"
"path/filepath"
"github.com/spf13/cobra"
)
// flag variables
var (
modfilePaths []string
verbose bool
)
var rootCmd = &cobra.Command{
Use: "go-mod-cleaner",
Short: "Clean up unused Go modules.",
Long: `Clean up unused Go modules. To be specific, it cleans up all modules within $GOPATH/pkg/mod,
except for currently used modules. To specify the modules in use, you need to indicate them
via go.mod files or directories that contain go.mod files.`,
RunE: func(cmd *cobra.Command, args []string) error {
modCachePath := filepath.Join(os.Getenv("GOPATH"), "pkg", "mod")
cleaner := NewCleaner(modCachePath, modfilePaths, verbose)
return cleaner.Clean()
},
}
func main() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
rootCmd.Flags().StringSliceVarP(
&modfilePaths, "modfile", "m", nil,
`go.mod files or directories that contain go.mod files,
modules referenced by these files are considered in use`,
)
err := rootCmd.MarkFlagRequired("modfile")
if err != nil {
panic(err)
}
rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose mode")
}