(t *testing.T)
| 14 | ) |
| 15 | |
| 16 | func TestGenerateXlog(t *testing.T) { |
| 17 | workDir := panelWorkDir(t) |
| 18 | fset := token.NewFileSet() |
| 19 | |
| 20 | apiDirs := []string{ |
| 21 | filepath.Join(workDir, "agent/app/api/v2"), |
| 22 | filepath.Join(workDir, "core/app/api/v2"), |
| 23 | filepath.Join(workDir, "agent/xpack/app/api/v2"), |
| 24 | filepath.Join(workDir, "core/xpack/app/api/v2"), |
| 25 | filepath.Join(workDir, "agent/enterprise/app/api/v2"), |
| 26 | filepath.Join(workDir, "core/enterprise/app/api/v2"), |
| 27 | } |
| 28 | |
| 29 | xlogMap := make(map[string]operationJson) |
| 30 | for _, dir := range apiDirs { |
| 31 | entries, _ := os.ReadDir(dir) |
| 32 | for _, info := range entries { |
| 33 | if info.IsDir() { |
| 34 | continue |
| 35 | } |
| 36 | fileItem, err := parser.ParseFile(fset, filepath.Join(dir, info.Name()), nil, parser.ParseComments) |
| 37 | if err != nil { |
| 38 | continue |
| 39 | } |
| 40 | for _, decl := range fileItem.Decls { |
| 41 | switch d := decl.(type) { |
| 42 | case *ast.FuncDecl: |
| 43 | if d.Doc != nil { |
| 44 | routerContent := "" |
| 45 | logContent := "" |
| 46 | for _, comment := range d.Doc.List { |
| 47 | if strings.HasPrefix(comment.Text, "// @Router") { |
| 48 | routerContent = replaceStr(comment.Text, "// @Router", "[post]", "[get]") |
| 49 | } |
| 50 | if strings.HasPrefix(comment.Text, "// @x-panel-log") { |
| 51 | logContent = replaceStr(comment.Text, "// @x-panel-log") |
| 52 | } |
| 53 | } |
| 54 | if len(routerContent) != 0 && len(logContent) != 0 { |
| 55 | var item operationJson |
| 56 | if err := json.Unmarshal([]byte(logContent), &item); err != nil { |
| 57 | panic(fmt.Sprintf("json unmarshal failed, err: %v", err)) |
| 58 | } |
| 59 | xlogMap[routerContent] = item |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | newJson, err := json.MarshalIndent(xlogMap, "", "\t") |
| 68 | if err != nil { |
| 69 | panic(fmt.Sprintf("json marshal for new file failed, err: %v", err)) |
| 70 | } |
| 71 | if err := os.WriteFile("x-log.json", newJson, 0640); err != nil { |
| 72 | panic(fmt.Sprintf("write core x-log.json failed, err: %v", err)) |
| 73 | } |
nothing calls this directly
no test coverage detected