-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmain.go
More file actions
57 lines (52 loc) · 1.5 KB
/
main.go
File metadata and controls
57 lines (52 loc) · 1.5 KB
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
package main
import (
"flag"
"fmt"
"github.com/3rf/codecoroner/unused"
"go/build"
"golang.org/x/tools/go/buildutil"
"os"
"sort"
"strings"
)
func main() {
var ignoreList string
ucf := unused.NewUnusedCodeFinder()
flag.BoolVar(&(ucf.Verbose), "v", false,
"prints extra information during execution to stderr")
flag.BoolVar(&(ucf.IncludeTests), "tests", false, "include tests in the analysis")
flag.StringVar(&(ignoreList), "ignore", "",
"don't read files that contain the given comma-separated strings (use to avoid /testdata, etc) ")
// hack for testing code with build flags
flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", "a list of build tags")
flag.Parse()
// handle ignore list
ucf.Ignore = strings.Split(ignoreList, ",")
if len(ucf.Ignore) > 0 && ucf.Ignore[0] == "" {
ucf.Ignore = nil
}
if len(flag.Args()) == 0 {
fmt.Println("Must specify either 'funcs' or 'idents' command. Run with -help for more info.")
os.Exit(2)
}
command := flag.Arg(0)
switch command {
case "funcs", "functions":
ucf.Idents = false
case "idents", "identifiers":
ucf.Idents = true
default:
fmt.Println("Must specify either 'funcs' or 'idents' command. Run with -help for more info.")
os.Exit(2)
}
unusedObjects, err := ucf.Run(flag.Args()[1:])
if err != nil {
fmt.Println("ERROR:", err)
os.Exit(1)
}
ucf.Logf("") // ensure a newline before printing results if -v is on
sort.Sort(unused.ByPosition(unusedObjects))
for _, o := range unusedObjects {
fmt.Printf("%s\n", o)
}
}