-
Notifications
You must be signed in to change notification settings - Fork 74
/
main.go
248 lines (216 loc) · 5.48 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
)
// Contants used for the meta tags
const (
Version = "1.4.1"
Name = "gotags"
URL = "https://github.com/jstemmer/gotags"
AuthorName = "Joel Stemmer"
AuthorEmail = "[email protected]"
)
var (
printVersion bool
inputFile string
outputFile string
recurse bool
sortOutput bool
silent bool
relative bool
listLangs bool
fields string
extraSymbols string
)
// ignore unknown flags
var flags = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
// Initialize flags.
func init() {
flags.BoolVar(&printVersion, "v", false, "print version.")
flags.StringVar(&inputFile, "L", "", `source file names are read from the specified file. If file is "-", input is read from standard in.`)
flags.StringVar(&outputFile, "f", "", `write output to specified file. If file is "-", output is written to standard out.`)
flags.BoolVar(&recurse, "R", false, "recurse into directories in the file list.")
flags.BoolVar(&sortOutput, "sort", true, "sort tags.")
flags.BoolVar(&silent, "silent", false, "do not produce any output on error.")
flags.BoolVar(&relative, "tag-relative", false, "file paths should be relative to the directory containing the tag file.")
flags.BoolVar(&listLangs, "list-languages", false, "list supported languages.")
flags.StringVar(&fields, "fields", "", "include selected extension fields (only +l).")
flags.StringVar(&extraSymbols, "extra", "", "include additional tags with package and receiver name prefixes (+q)")
flags.Usage = func() {
fmt.Fprintf(os.Stderr, "gotags version %s\n\n", Version)
fmt.Fprintf(os.Stderr, "Usage: %s [options] file(s)\n\n", os.Args[0])
flags.PrintDefaults()
}
}
func walkDir(names []string, dir string) ([]string, error) {
e := filepath.Walk(dir, func(path string, finfo os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, ".go") && !finfo.IsDir() {
names = append(names, path)
}
return nil
})
return names, e
}
func recurseNames(names []string) ([]string, error) {
var ret []string
for _, name := range names {
info, e := os.Stat(name)
if e != nil || info == nil || !info.IsDir() {
ret = append(ret, name) // defer the error handling to the scanner
} else {
ret, e = walkDir(ret, name)
if e != nil {
return names, e
}
}
}
return ret, nil
}
func readNames(names []string) ([]string, error) {
if len(inputFile) == 0 {
return names, nil
}
var scanner *bufio.Scanner
if inputFile != "-" {
in, err := os.Open(inputFile)
if err != nil {
return nil, err
}
defer in.Close()
scanner = bufio.NewScanner(in)
} else {
scanner = bufio.NewScanner(os.Stdin)
}
for scanner.Scan() {
names = append(names, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return names, nil
}
func getFileNames() ([]string, error) {
var names []string
names = append(names, flags.Args()...)
names, err := readNames(names)
if err != nil {
return nil, err
}
if recurse {
names, err = recurseNames(names)
if err != nil {
return nil, err
}
}
return names, nil
}
func main() {
if err := flags.Parse(os.Args[1:]); err == flag.ErrHelp {
return
}
if printVersion {
fmt.Printf("gotags version %s\n", Version)
return
}
if listLangs {
fmt.Println("Go")
return
}
files, err := getFileNames()
if err != nil {
fmt.Fprintf(os.Stderr, "cannot get specified files\n\n")
flags.Usage()
os.Exit(1)
}
if len(files) == 0 && len(inputFile) == 0 {
fmt.Fprintf(os.Stderr, "no file specified\n\n")
flags.Usage()
os.Exit(1)
}
var basedir string
if relative {
basedir, err = filepath.Abs(filepath.Dir(outputFile))
if err != nil {
if !silent {
fmt.Fprintf(os.Stderr, "could not determine absolute path: %s\n", err)
}
os.Exit(1)
}
}
fieldSet, err := parseFields(fields)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n\n", err)
flags.Usage()
os.Exit(1)
}
symbolSet, err := parseExtraSymbols(extraSymbols)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n\n", err)
flags.Usage()
os.Exit(1)
}
tags := []Tag{}
for _, file := range files {
ts, err := Parse(file, relative, basedir, symbolSet)
if err != nil {
if !silent {
fmt.Fprintf(os.Stderr, "parse error: %s\n\n", err)
}
continue
}
tags = append(tags, ts...)
}
output := createMetaTags()
for _, tag := range tags {
if fieldSet.Includes(Language) {
tag.Fields[Language] = "Go"
}
output = append(output, tag.String())
}
if sortOutput {
sort.Sort(sort.StringSlice(output))
}
var out io.Writer
if len(outputFile) == 0 || outputFile == "-" {
// For compatibility with older gotags versions, also write to stdout
// when outputFile is not specified.
out = os.Stdout
} else {
file, err := os.Create(outputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "could not create output file: %s\n", err)
os.Exit(1)
}
out = file
defer file.Close()
}
for _, s := range output {
fmt.Fprintln(out, s)
}
}
// createMetaTags returns a list of meta tags.
func createMetaTags() []string {
var sorted int
if sortOutput {
sorted = 1
}
return []string{
"!_TAG_FILE_FORMAT\t2",
fmt.Sprintf("!_TAG_FILE_SORTED\t%d\t/0=unsorted, 1=sorted/", sorted),
fmt.Sprintf("!_TAG_PROGRAM_AUTHOR\t%s\t/%s/", AuthorName, AuthorEmail),
fmt.Sprintf("!_TAG_PROGRAM_NAME\t%s", Name),
fmt.Sprintf("!_TAG_PROGRAM_URL\t%s", URL),
fmt.Sprintf("!_TAG_PROGRAM_VERSION\t%s\t/%s/", Version, runtime.Version()),
}
}