-
Notifications
You must be signed in to change notification settings - Fork 0
/
maven.go
92 lines (84 loc) · 2.31 KB
/
maven.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
package system
import (
"fmt"
"os"
"github.com/clibing/knife/cmd/debug"
"github.com/clibing/knife/internal/maven"
"github.com/clibing/knife/internal/utils"
"github.com/spf13/cobra"
)
// mvnCmd represents the arch command
var mvnCmd = &cobra.Command{
Use: "maven",
Aliases: []string{"mvn"},
Short: "Clean Maven snapshot dependencies",
Example: `
1. 扫描并分析当前目录下的jar包
knife system maven ./
2. 扫描、分析并删除
knife system maven -c ./`,
Run: func(c *cobra.Command, args []string) {
debug := debug.NewDebug(c)
if len(args) == 0 {
debug.Debug("暂无扫描的path")
c.Help()
return
}
clear, _ := c.Flags().GetBool("clear")
var freeUpspace int64
debug.ShowSame("🔵 clean maven starting.")
has := false
for _, p := range args {
data := maven.Doing(debug, p)
for _, v := range data {
for _, w := range v.Snapshot {
has = true
if w.Deleted {
info, err := os.Stat(w.FullName)
if err != nil {
debug.Debug("remove to failed, err: %s, file: %s", err.Error(), w.FullName)
} else {
freeUpspace = freeUpspace + info.Size()
}
if clear {
err = os.Remove(w.FullName)
if err != nil {
debug.Debug("remove to failed, err: %s, file: %s", err.Error(), w.FullName)
}
sha1 := fmt.Sprintf("%s.sha1", w.FullName)
_, err = os.Stat(sha1)
if err == nil || os.IsExist(err) {
err = os.Remove(sha1)
if err != nil {
debug.Debug("remove to failed, err: %s, file: %s", err.Error(), w.FullName)
}
}
debug.ShowSame("✅ successfully removed: %s", w.FullName)
} else {
debug.ShowSame("❓ preview automatically deleted when settings are deleted: %s", w.FullName)
}
} else {
debug.ShowSame("🟡 skip delete: %s", w.FullName)
}
}
}
}
if !has {
debug.ShowSame("🟡 暂无需要处理的文件")
}
if freeUpspace > 0 {
if !clear {
debug.ShowSame("❗ will free disk: %s", utils.BeautifyValue(freeUpspace))
} else {
debug.ShowSame("🟤 free disk: %s", utils.BeautifyValue(freeUpspace))
}
}
debug.ShowSame("🔵 clean maven end")
},
}
func init() {
mvnCmd.Flags().BoolP("clear", "c", false, "是否确认清理过期的snapshot jar")
}
func NewMavenCmd() *cobra.Command {
return mvnCmd
}