-
Notifications
You must be signed in to change notification settings - Fork 6
/
tool.go
151 lines (139 loc) · 2.88 KB
/
tool.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package commands
import (
"fmt"
"github.com/fatih/color"
"go/build"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"time"
)
// Tool is kratos tool.
type Tool struct {
Name string `json:"name"`
Alias string `json:"alias"`
BuildTime time.Time `json:"build_time"`
Install string `json:"install"`
Dir string `json:"dir"`
Summary string `json:"summary"`
Platform []string `json:"platform"`
Author string `json:"author"`
URL string `json:"url"`
}
func toolList() (tools []*Tool) {
return toolIndexs
}
func (t Tool) needUpdated() bool {
if !t.supportOS() || t.Install == "" {
return false
}
if f, err := os.Stat(t.toolPath()); err == nil {
if t.BuildTime.After(f.ModTime()) {
return true
}
}
return false
}
func (t Tool) toolPath() string {
name := t.Alias
if name == "" {
name = t.Name
}
gobin := Getenv("GOBIN")
if runtime.GOOS == "windows" {
name += ".exe"
}
if gobin != "" {
return filepath.Join(gobin, name)
}
return filepath.Join(gopath(), "bin", name)
}
func (t Tool) installed() bool {
_, err := os.Stat(t.toolPath())
return err == nil
}
func (t Tool) supportOS() bool {
for _, p := range t.Platform {
if strings.ToLower(p) == runtime.GOOS {
return true
}
}
return false
}
func (t Tool) install() {
if t.Install == "" {
fmt.Fprintf(os.Stderr, color.RedString("%s: 安装失败\n", t.Name))
return
}
fmt.Println(t.Install)
cmds := strings.Split(t.Install, " ")
if len(cmds) > 0 {
if err := runTool(t.Name, path.Dir(t.toolPath()), cmds[0], cmds[1:]); err == nil {
color.Green("%s: 安装成功!", t.Name)
}
}
}
func (t Tool) updated() bool {
if !t.supportOS() || t.Install == "" {
return false
}
if f, err := os.Stat(t.toolPath()); err == nil {
if t.BuildTime.After(f.ModTime()) {
return true
}
}
return false
}
func gopath() (gp string) {
gopaths := strings.Split(Getenv("GOPATH"), string(filepath.ListSeparator))
if len(gopaths) == 1 && gopaths[0] != "" {
return gopaths[0]
}
pwd, err := os.Getwd()
if err != nil {
return
}
abspwd, err := filepath.Abs(pwd)
if err != nil {
return
}
for _, gopath := range gopaths {
if gopath == "" {
continue
}
absgp, err := filepath.Abs(gopath)
if err != nil {
return
}
if strings.HasPrefix(abspwd, absgp) {
return absgp
}
}
return build.Default.GOPATH
}
func runTool(name, dir, cmd string, args []string) (err error) {
toolCmd := &exec.Cmd{
Path: cmd,
Args: append([]string{cmd}, args...),
Dir: dir,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Env: os.Environ(),
}
if filepath.Base(cmd) == cmd {
var lp string
if lp, err = exec.LookPath(cmd); err == nil {
toolCmd.Path = lp
}
}
if err = toolCmd.Run(); err != nil {
if e, ok := err.(*exec.ExitError); !ok || !e.Exited() {
fmt.Fprintf(os.Stderr, "运行 %s 出错: %v\n", name, err)
}
}
return
}