Skip to content

Commit 46483a9

Browse files
committed
init
1 parent 232971e commit 46483a9

20 files changed

+1201
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
*.exe
3+
/output/*
4+
/logs/*

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Usage:
2+
3+
```bash
4+
./class-version-tracker.exe track -g org.apache.dubbo -a dubbo -c org.apache.dubbo.rpc.protocol.DelegateExporterMap -o output
5+
./class-version-tracker.exe track -g org.apache.dubbo -a dubbo -c org.apache.dubbo.rpc.protocol.AbstractProtocol -o output
6+
./class-version-tracker.exe track -g org.apache.dubbo -a dubbo -c org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol -o output
7+
./class-version-tracker.exe track -g org.apache.dubbo -a dubbo -c org.apache.dubbo.common.extension.ExtensionLoader -o output
8+
9+
./class-version-tracker.exe track -g com.alibaba -a dubbo -c com.alibaba.dubbo.rpc.protocol.AbstractProtocol -o output
10+
./class-version-tracker.exe track -g com.alibaba -a dubbo -c com.alibaba.dubbo.common.extension.ExtensionLoader -o output
11+
./class-version-tracker.exe track -g com.alibaba -a dubbo -c com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol -o output
12+
```
13+
14+
# TODO
15+
16+
- 支持指定是使用本地mvn配置的镜像仓库还是直接连中央仓库,因为某些镜像仓库有点坑,会有那种版本空洞,冷不丁给你来一下
17+

cmd/root.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/fatih/color"
6+
cc "github.com/ivanpirog/coloredcobra"
7+
"github.com/spf13/cobra"
8+
"os"
9+
)
10+
11+
var rootCmd = &cobra.Command{
12+
Use: "class-version-tracker",
13+
Short: "",
14+
Long: ``,
15+
Run: func(cmd *cobra.Command, args []string) {
16+
_ = cmd.Help()
17+
},
18+
}
19+
20+
func Execute() {
21+
22+
cc.Init(&cc.Config{
23+
RootCmd: rootCmd,
24+
Headings: cc.HiCyan + cc.Bold + cc.Underline,
25+
Commands: cc.HiYellow + cc.Bold,
26+
Example: cc.Italic,
27+
ExecName: cc.Bold,
28+
Flags: cc.Bold,
29+
})
30+
// 官方文档没提,但是似乎不加这一句会乱码...
31+
rootCmd.SetOut(color.Output)
32+
33+
if err := rootCmd.Execute(); err != nil {
34+
_, _ = fmt.Fprintln(os.Stderr, err)
35+
os.Exit(1)
36+
}
37+
}

cmd/track.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
"github.com/java-sec/class-version-tracker/pkg/domain"
6+
"github.com/java-sec/class-version-tracker/pkg/track"
7+
"github.com/spf13/cobra"
8+
"os"
9+
"path/filepath"
10+
"strings"
11+
)
12+
13+
var (
14+
15+
// 要追踪的组ID
16+
groupId string
17+
18+
// 要追踪的文档ID
19+
artifactId string
20+
21+
// 要追踪的类
22+
class string
23+
24+
// 结果的输出位置
25+
output string
26+
)
27+
28+
func init() {
29+
30+
trackCmd.Flags().StringVarP(&groupId, "groupId", "g", "", "Group ID for track")
31+
trackCmd.Flags().StringVarP(&artifactId, "artifactId", "a", "", "Artifact ID for track")
32+
trackCmd.Flags().StringVarP(&class, "class", "c", "", "class for track, example: org.springframework.web.servlet.mvc.method.RequestMappingInfo")
33+
trackCmd.Flags().StringVarP(&output, "output", "o", "", "result output directory, default current directory")
34+
35+
rootCmd.AddCommand(trackCmd)
36+
}
37+
38+
var trackCmd = &cobra.Command{
39+
Use: "track",
40+
Short: "track for class",
41+
Long: ``,
42+
RunE: func(cmd *cobra.Command, args []string) error {
43+
report, err := track.Track(cmd.Context(), groupId, artifactId, class)
44+
if err != nil {
45+
return err
46+
}
47+
return saveReport(output, report)
48+
},
49+
}
50+
51+
// 把报告保存到给定的文件夹
52+
func saveReport(directory string, report *domain.Report) error {
53+
54+
// 确保输出目录存在
55+
classDumpDirectory := filepath.Join(directory, strings.ReplaceAll(report.GroupId, ".", "."), report.ArtifactId, report.Class)
56+
err := os.MkdirAll(classDumpDirectory, os.ModePerm)
57+
if err != nil {
58+
return err
59+
}
60+
61+
// 把报告导出一份JSON格式的
62+
marshal, err := json.Marshal(report)
63+
if err != nil {
64+
return err
65+
}
66+
reportPath := filepath.Join(classDumpDirectory, "report.json")
67+
err = os.WriteFile(reportPath, marshal, 0644)
68+
if err != nil {
69+
return err
70+
}
71+
72+
// 把每一组的类导出
73+
for _, group := range report.Groups {
74+
classFileName := filepath.Join(classDumpDirectory, group.Versions[0]+".class")
75+
err := os.WriteFile(classFileName, group.Bytes, 0644)
76+
if err != nil {
77+
return err
78+
}
79+
}
80+
81+
return nil
82+
}

cmd/version.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cmd
2+
3+
import (
4+
"github.com/fatih/color"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
const Version = "v0.0.1"
9+
10+
func init() {
11+
rootCmd.AddCommand(versionCmd)
12+
}
13+
14+
var versionCmd = &cobra.Command{
15+
Use: "version",
16+
Short: "Show the version number of class version tracker",
17+
Long: ``,
18+
Run: func(cmd *cobra.Command, args []string) {
19+
color.HiGreen("version %s", Version)
20+
},
21+
}

config.yaml

Whitespace-only changes.

go.mod

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module github.com/java-sec/class-version-tracker
2+
3+
go 1.19
4+
5+
require (
6+
github.com/compression-algorithm-research-lab/go-unzip v0.0.0-20230602073303-1217108c498f
7+
github.com/fatih/color v1.15.0
8+
github.com/ivanpirog/coloredcobra v1.0.1
9+
github.com/scagogogo/maven-crawler v0.0.0-20230619030815-61998034bc24
10+
github.com/scagogogo/mvn-sdk v0.0.0-20230620031453-2d99a2094f36
11+
github.com/spf13/cobra v1.7.0
12+
github.com/stretchr/testify v1.8.4
13+
)
14+
15+
require (
16+
github.com/PuerkitoBio/goquery v1.8.1 // indirect
17+
github.com/aliyun/aliyun-oss-go-sdk v2.2.7+incompatible // indirect
18+
github.com/andybalholm/cascadia v1.3.2 // indirect
19+
github.com/basgys/goxml2json v1.1.0 // indirect
20+
github.com/crawler-go-go-go/go-requests v0.0.0-20230525030146-0f17843cff2c // indirect
21+
github.com/davecgh/go-spew v1.1.1 // indirect
22+
github.com/emirpasic/gods v1.18.1 // indirect
23+
github.com/fsnotify/fsnotify v1.6.0 // indirect
24+
github.com/go-redis/redis v6.15.9+incompatible // indirect
25+
github.com/go-sql-driver/mysql v1.7.1 // indirect
26+
github.com/golang-infrastructure/go-compare-anything v0.0.2-0.20230108071748-35501d697475 // indirect
27+
github.com/golang-infrastructure/go-gtypes v0.0.1 // indirect
28+
github.com/golang-infrastructure/go-how-run v0.0.0-20230107060855-56163adc7748 // indirect
29+
github.com/golang-infrastructure/go-pointer v0.0.4 // indirect
30+
github.com/golang-infrastructure/go-project-root-directory v0.0.1 // indirect
31+
github.com/golang-infrastructure/go-reflect-utils v0.0.0-20221130143747-965ef2eb09c3 // indirect
32+
github.com/golang-infrastructure/go-stack v0.0.2 // indirect
33+
github.com/golang-infrastructure/go-tuple v0.0.0-20221215155811-4ed54fe7d579 // indirect
34+
github.com/google/licensecheck v0.3.1 // indirect
35+
github.com/hashicorp/hcl v1.0.0 // indirect
36+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
37+
github.com/jmoiron/sqlx v1.3.5 // indirect
38+
github.com/lestrrat-go/strftime v1.0.6 // indirect
39+
github.com/magiconair/properties v1.8.7 // indirect
40+
github.com/mattn/go-colorable v0.1.13 // indirect
41+
github.com/mattn/go-isatty v0.0.19 // indirect
42+
github.com/mitchellh/go-homedir v1.1.0 // indirect
43+
github.com/mitchellh/mapstructure v1.5.0 // indirect
44+
github.com/nxadm/tail v1.4.8 // indirect
45+
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
46+
github.com/pkg/errors v0.9.1 // indirect
47+
github.com/pmezard/go-difflib v1.0.0 // indirect
48+
github.com/scagogogo/maven-pom-xml-render v0.0.0-20230522042741-86dd7c7d0e1f // indirect
49+
github.com/scagogogo/versions v0.0.0-20230616073442-f7ffd332b5e8 // indirect
50+
github.com/spf13/afero v1.9.5 // indirect
51+
github.com/spf13/cast v1.5.1 // indirect
52+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
53+
github.com/spf13/pflag v1.0.5 // indirect
54+
github.com/spf13/viper v1.16.0 // indirect
55+
github.com/subosito/gotenv v1.4.2 // indirect
56+
github.com/valyala/fastjson v1.6.4 // indirect
57+
go.uber.org/atomic v1.11.0 // indirect
58+
go.uber.org/multierr v1.11.0 // indirect
59+
go.uber.org/zap v1.24.0 // indirect
60+
golang.org/x/net v0.11.0 // indirect
61+
golang.org/x/sys v0.9.0 // indirect
62+
golang.org/x/text v0.10.0 // indirect
63+
golang.org/x/time v0.3.0 // indirect
64+
gopkg.in/ini.v1 v1.67.0 // indirect
65+
gopkg.in/yaml.v2 v2.4.0 // indirect
66+
gopkg.in/yaml.v3 v3.0.1 // indirect
67+
)

0 commit comments

Comments
 (0)