forked from robustirc/robustirc
/
generrors.go
61 lines (49 loc) · 1.11 KB
/
generrors.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
// +build ignore
package main
import (
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"strings"
)
func main() {
output, err := os.Create("errors.go")
if err != nil {
log.Fatal(err)
}
defer output.Close()
fmt.Fprintf(output, `package main
import "github.com/sorcix/irc"
var errorCodes = make(map[string]bool)
func init() {
`)
// Iterate over all top-level declarations (including constants) of
// github.com/sorcix/irc and put ERR_ constants into a map. This map can
// then be used to judge whether an IRC message represents an error or not.
pkg, err := build.Import("github.com/sorcix/irc", "", build.ImportComment)
if err != nil {
log.Fatal(err)
}
fset := token.NewFileSet()
for _, name := range pkg.GoFiles {
path := filepath.Join(pkg.Dir, name)
f, err := parser.ParseFile(fset, path, nil, 0)
if err != nil {
log.Fatal(err)
}
ast.FilterFile(f, func(ident string) bool {
if !strings.HasPrefix(ident, "ERR_") {
return false
}
fmt.Fprintf(output, "\terrorCodes[irc.%s] = true\n", ident)
return true
})
}
fmt.Fprintf(output, `}
`)
}