/
generate.go
101 lines (87 loc) · 2.57 KB
/
generate.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
// Copyright 2020 Chaos Mesh Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"go/ast"
"go/parser"
"go/token"
"log"
"strings"
"github.com/YangKeao/thaterror/pkg/filemanager"
"github.com/YangKeao/thaterror/pkg/impl"
zglob "github.com/mattn/go-zglob"
"github.com/spf13/cobra"
)
// GenerateCmd is the cobra command to generate error handling codes
var GenerateCmd = &cobra.Command{
Use: "generate",
Short: "generate codes for error handling",
Run: generateCmd,
}
var (
generateFilter string
outputFileName string
)
func init() {
GenerateCmd.PersistentFlags().StringVar(&generateFilter, "filter", "**/error.go", "only files matching the pattern will be walked")
GenerateCmd.PersistentFlags().StringVarP(&outputFileName, "output", "o", "zz_generated.thaterror.go", "the output filename of generated file")
}
func generateCmd(cmd *cobra.Command, args []string) {
files, err := zglob.Glob(generateFilter)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
log.Printf("iterating file: %s\n", file)
fset := token.NewFileSet()
goFile, err := parser.ParseFile(fset, file, nil, parser.ParseComments)
if err != nil {
log.Fatal(err)
}
importMap := make(map[string]string)
for _, impt := range goFile.Imports {
if impt.Name != nil {
importMap[impt.Name.Name] = impt.Path.Value
} else {
// TODO: get the last section of Path
importMap[impt.Path.Value] = impt.Path.Value
}
}
types := []*impl.UnintializedErrorType{}
cmap := ast.NewCommentMap(fset, goFile, goFile.Comments)
for node, commentGroups := range cmap {
comments := []*ast.Comment{}
related := false
for _, commentGroup := range commentGroups {
for _, comment := range commentGroup.List {
if strings.Contains(comment.Text, "+thaterror") {
related = true
}
comments = append(comments, comment)
}
}
if related {
errType := &impl.UnintializedErrorType{
Node: node,
Comments: comments,
}
types = append(types, errType)
}
}
impl.Pkg(file, goFile.Name.Name, importMap, types, outputFileName)
}
err = filemanager.Render()
if err != nil {
log.Fatal(err)
}
}