5
5
"flag"
6
6
"fmt"
7
7
"os"
8
+ "path/filepath"
8
9
"sort"
10
+ "strings"
9
11
)
10
12
11
13
const (
@@ -19,6 +21,7 @@ const (
19
21
var (
20
22
printVersion bool
21
23
inputFile string
24
+ recurse bool
22
25
sortOutput bool
23
26
silent bool
24
27
printTree bool // for debugging
28
31
func init () {
29
32
flag .BoolVar (& printVersion , "v" , false , "print version" )
30
33
flag .StringVar (& inputFile , "L" , "" , "source file names are read from the specified file." )
34
+ flag .BoolVar (& recurse , "R" , false , "recurse into directories in the file list" )
31
35
flag .BoolVar (& sortOutput , "sort" , true , "sort tags" )
32
36
flag .BoolVar (& silent , "silent" , false , "do not produce any output on error" )
33
37
flag .BoolVar (& printTree , "tree" , false , "print syntax tree (debugging)" )
@@ -39,11 +43,37 @@ func init() {
39
43
}
40
44
}
41
45
42
- func getFileNames () ([]string , error ) {
43
- var names []string
46
+ func walkDir (names []string , dir string ) ([]string , error ) {
47
+ e := filepath .Walk (dir , func (path string , _ os.FileInfo , err error ) error {
48
+ if err != nil {
49
+ return err
50
+ }
51
+ if strings .HasSuffix (path , ".go" ) {
52
+ names = append (names , path )
53
+ }
54
+ return nil
55
+ })
44
56
45
- names = append (names , flag .Args ()... )
57
+ return names , e
58
+ }
46
59
60
+ func recurseNames (names []string ) ([]string , error ) {
61
+ var ret []string
62
+ for _ , name := range names {
63
+ info , e := os .Stat (name )
64
+ if e != nil || info == nil || ! info .IsDir () {
65
+ ret = append (ret , name ) // defer the error handling to the scanner
66
+ } else {
67
+ ret , e = walkDir (ret , name )
68
+ if e != nil {
69
+ return names , e
70
+ }
71
+ }
72
+ }
73
+ return ret , nil
74
+ }
75
+
76
+ func readNames (names []string ) ([]string , error ) {
47
77
if len (inputFile ) == 0 {
48
78
return names , nil
49
79
}
@@ -71,6 +101,25 @@ func getFileNames() ([]string, error) {
71
101
return names , nil
72
102
}
73
103
104
+ func getFileNames () ([]string , error ) {
105
+ var names []string
106
+
107
+ names = append (names , flag .Args ()... )
108
+ names , err := readNames (names )
109
+ if err != nil {
110
+ return nil , err
111
+ }
112
+
113
+ if recurse {
114
+ names , err = recurseNames (names )
115
+ if err != nil {
116
+ return nil , err
117
+ }
118
+ }
119
+
120
+ return names , nil
121
+ }
122
+
74
123
func main () {
75
124
flag .Parse ()
76
125
0 commit comments