-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrack.go
More file actions
87 lines (70 loc) · 2.05 KB
/
track.go
File metadata and controls
87 lines (70 loc) · 2.05 KB
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
package cmd
import (
"encoding/json"
"github.com/java-sec/class-version-tracker/pkg/domain"
"github.com/java-sec/class-version-tracker/pkg/track"
"github.com/spf13/cobra"
"os"
"path/filepath"
"strings"
)
var (
// 要追踪的组ID
groupId string
// 要追踪的文档ID
artifactId string
// 要追踪的类
class string
// 结果的输出位置
output string
)
func init() {
trackCmd.Flags().StringVarP(&groupId, "groupId", "g", "", "Group ID for track, example: org.apache.dubbo")
trackCmd.Flags().StringVarP(&artifactId, "artifactId", "a", "", "Artifact ID for track, example: dubbo")
trackCmd.Flags().StringVarP(&class, "class", "c", "", "Class for track, example: org.springframework.web.servlet.mvc.method.RequestMappingInfo")
trackCmd.Flags().StringVarP(&output, "output", "o", "", "result output directory, default ./output")
rootCmd.AddCommand(trackCmd)
}
var trackCmd = &cobra.Command{
Use: "track",
Short: "track for class",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
if output == "" {
output = "./output"
}
report, err := track.Track(cmd.Context(), groupId, artifactId, class)
if err != nil {
return err
}
return saveReport(output, report)
},
}
// 把报告保存到给定的文件夹
func saveReport(directory string, report *domain.Report) error {
// 确保输出目录存在
classDumpDirectory := filepath.Join(directory, strings.ReplaceAll(report.GroupId, ".", "."), report.ArtifactId, report.Class)
err := os.MkdirAll(classDumpDirectory, os.ModePerm)
if err != nil {
return err
}
// 把报告导出一份JSON格式的
marshal, err := json.Marshal(report)
if err != nil {
return err
}
reportPath := filepath.Join(classDumpDirectory, "report.json")
err = os.WriteFile(reportPath, marshal, 0644)
if err != nil {
return err
}
// 把每一组的类导出
for _, group := range report.Groups {
classFileName := filepath.Join(classDumpDirectory, group.Versions[0]+".class")
err := os.WriteFile(classFileName, group.Bytes, 0644)
if err != nil {
return err
}
}
return nil
}