|
| 1 | +//Package cmd provides command line interface to gounexport tool. |
| 2 | +// |
| 3 | +//Command requires package name. For, example: |
| 4 | +// gounexport github.com/dooman87/gounexport |
| 5 | +// |
| 6 | +//There are next supported flags: |
| 7 | +// |
| 8 | +// -exclude string |
| 9 | +// File with exlude patterns for objects that shouldn't be unexported.Each pattern should be started at new line. Default pattern is Test* to exclude tests methods. |
| 10 | +// -out string |
| 11 | +// Output file. If not set then stdout will be used |
| 12 | +// -rename |
| 13 | +// If set, then all defenitions that will be determined as unused will be renamed in files |
| 14 | +// -verbose |
| 15 | +// Turning on verbose mode |
| 16 | +// |
| 17 | +//Exclude flag is pointing to file with regular expressions to ignore |
| 18 | +//public unexported symbols. Each expression should be starterd |
| 19 | +//with a new line. It's a standard go/regexp package. For example, |
| 20 | +//below we are excluding all Test methods and everything from package |
| 21 | +//public/api/pack/: |
| 22 | +// |
| 23 | +// Test* |
| 24 | +// public/api/packag/* |
| 25 | +// |
| 26 | +//Use -rename flag carefully and check output before. |
| 27 | +// |
| 28 | +//BUG(d): The tool is not analyzing test files if package in the test file is not |
| 29 | +//the same as a base package. For instance, pack/pack_test.go is in package pack_test |
| 30 | +//instead of pack |
| 31 | +package main |
| 32 | + |
| 33 | +import ( |
| 34 | + "flag" |
| 35 | + "fmt" |
| 36 | + "io/ioutil" |
| 37 | + "os" |
| 38 | + "regexp" |
| 39 | + "sort" |
| 40 | + "strings" |
| 41 | + |
| 42 | + "go/ast" |
| 43 | + "go/types" |
| 44 | + |
| 45 | + "github.com/dooman87/gounexport" |
| 46 | + "github.com/dooman87/gounexport/fs" |
| 47 | + "github.com/dooman87/gounexport/util" |
| 48 | +) |
| 49 | + |
| 50 | +type sortableDefinition struct { |
| 51 | + defs []*gounexport.Definition |
| 52 | +} |
| 53 | + |
| 54 | +func (sortDefs *sortableDefinition) Len() int { |
| 55 | + return len(sortDefs.defs) |
| 56 | +} |
| 57 | + |
| 58 | +func (sortDefs *sortableDefinition) Less(i int, j int) bool { |
| 59 | + iDef := sortDefs.defs[i] |
| 60 | + jDef := sortDefs.defs[j] |
| 61 | + if iDef.File != jDef.File { |
| 62 | + return iDef.File < jDef.File |
| 63 | + } |
| 64 | + if iDef.Line != jDef.Line { |
| 65 | + return iDef.Line < jDef.Line |
| 66 | + } |
| 67 | + return iDef.Col < jDef.Col |
| 68 | +} |
| 69 | + |
| 70 | +func (sortDefs *sortableDefinition) Swap(i, j int) { |
| 71 | + temp := sortDefs.defs[i] |
| 72 | + sortDefs.defs[i] = sortDefs.defs[j] |
| 73 | + sortDefs.defs[j] = temp |
| 74 | +} |
| 75 | + |
| 76 | +func main() { |
| 77 | + var err error |
| 78 | + |
| 79 | + rename := flag.Bool("rename", false, |
| 80 | + "If set, then all defenitions "+ |
| 81 | + "that will be determined as unused will be renamed in files") |
| 82 | + verbose := flag.Bool("verbose", false, "Turning on verbose mode") |
| 83 | + out := flag.String("out", "", "Output file. If not set then stdout will be used") |
| 84 | + exclude := flag.String("exclude", "", |
| 85 | + "File with exlude patterns for objects that shouldn't be unexported."+ |
| 86 | + "Each pattern should be started at new line. Default pattern is Test* to exclude tests methods.") |
| 87 | + |
| 88 | + flag.Parse() |
| 89 | + pkg := flag.Arg(0) |
| 90 | + |
| 91 | + //Setup logging |
| 92 | + if *verbose { |
| 93 | + util.Level = "DEBUG" |
| 94 | + } else { |
| 95 | + util.Level = "ERROR" |
| 96 | + } |
| 97 | + |
| 98 | + //Setup excludes |
| 99 | + defaultRegexp, _ := regexp.Compile("Test*") |
| 100 | + excludeRegexps := []*regexp.Regexp{defaultRegexp} |
| 101 | + if len(*exclude) > 0 { |
| 102 | + excludeRegexps, err = readExcludes(*exclude) |
| 103 | + if err != nil { |
| 104 | + util.Fatalf("error while setup logging: %v", err) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + //Looking up for unused definitions, print them and rename |
| 109 | + if len(pkg) > 0 { |
| 110 | + unusedDefinitions, allDefinitions, err := getUnusedDefinitions(pkg, excludeRegexps) |
| 111 | + if err != nil { |
| 112 | + util.Fatalf("error while getting definitions: %v", err) |
| 113 | + } |
| 114 | + if err := printDefinitions(*out, unusedDefinitions); err != nil { |
| 115 | + util.Fatalf("error while printing result: %v", err) |
| 116 | + } |
| 117 | + if *rename { |
| 118 | + renameDefinitions(unusedDefinitions, allDefinitions) |
| 119 | + } |
| 120 | + } else { |
| 121 | + fmt.Printf("Usage: gounexport [OPTIONS] package\n") |
| 122 | + flag.PrintDefaults() |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func readExcludes(file string) ([]*regexp.Regexp, error) { |
| 127 | + bytes, err := ioutil.ReadFile(file) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + |
| 132 | + var result []*regexp.Regexp |
| 133 | + regexpStrings := strings.Split(string(bytes), "\n") |
| 134 | + for _, regexpS := range regexpStrings { |
| 135 | + if len(regexpS) == 0 { |
| 136 | + continue |
| 137 | + } |
| 138 | + var regex *regexp.Regexp |
| 139 | + regex, err = regexp.Compile(regexpS) |
| 140 | + if err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + result = append(result, regex) |
| 144 | + } |
| 145 | + return result, err |
| 146 | +} |
| 147 | + |
| 148 | +func renameDefinitions(unused []*gounexport.Definition, allDefs map[string]*gounexport.Definition) { |
| 149 | + for _, def := range unused { |
| 150 | + gounexport.Unexport(def, allDefs, fs.ReplaceStringInFile) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func getUnusedDefinitions(pkg string, excludes []*regexp.Regexp) ( |
| 155 | + []*gounexport.Definition, map[string]*gounexport.Definition, error) { |
| 156 | + info := types.Info{ |
| 157 | + Types: make(map[ast.Expr]types.TypeAndValue), |
| 158 | + Defs: make(map[*ast.Ident]types.Object), |
| 159 | + Uses: make(map[*ast.Ident]types.Object), |
| 160 | + } |
| 161 | + _, fset, err := gounexport.ParsePackage(pkg, &info) |
| 162 | + if err != nil { |
| 163 | + return nil, nil, err |
| 164 | + } |
| 165 | + defs := gounexport.GetDefinitions(&info, fset) |
| 166 | + return gounexport.GetDefenitionsToHide(pkg, defs, excludes), defs, nil |
| 167 | +} |
| 168 | + |
| 169 | +func printDefinitions(filename string, defs []*gounexport.Definition) error { |
| 170 | + output := definitionsToString(defs) |
| 171 | + if len(filename) > 0 { |
| 172 | + if err := ioutil.WriteFile(filename, []byte(output), os.ModePerm); err != nil { |
| 173 | + return err |
| 174 | + } |
| 175 | + } else { |
| 176 | + fmt.Print(output) |
| 177 | + } |
| 178 | + return nil |
| 179 | +} |
| 180 | + |
| 181 | +func definitionsToString(defs []*gounexport.Definition) string { |
| 182 | + sDef := new(sortableDefinition) |
| 183 | + sDef.defs = defs |
| 184 | + sort.Sort(sDef) |
| 185 | + |
| 186 | + result := "-----------------------------------------------------\n" |
| 187 | + result += fmt.Sprintf("Found %d unused definitions\n", len(defs)) |
| 188 | + for _, def := range defs { |
| 189 | + result += fmt.Sprintf("%s - %s:%d:%d\n", def.Name, def.File, def.Line, def.Col) |
| 190 | + for _, u := range def.Usages { |
| 191 | + result += fmt.Sprintf("\t%s:%d:%d\n", u.Pos.Filename, u.Pos.Line, u.Pos.Column) |
| 192 | + } |
| 193 | + } |
| 194 | + return result |
| 195 | +} |
0 commit comments