1
2
3
4
5
6 package tool
7
8 import (
9 "context"
10 "encoding/json"
11 "flag"
12 "fmt"
13 "go/build"
14 "internal/platform"
15 "os"
16 "os/exec"
17 "os/signal"
18 "sort"
19 "strings"
20
21 "cmd/go/internal/base"
22 "cmd/go/internal/cfg"
23 )
24
25 var CmdTool = &base.Command{
26 Run: runTool,
27 UsageLine: "go tool [-n] command [args...]",
28 Short: "run specified go tool",
29 Long: `
30 Tool runs the go tool command identified by the arguments.
31 With no arguments it prints the list of known tools.
32
33 The -n flag causes tool to print the command that would be
34 executed but not execute it.
35
36 For more about each tool command, see 'go doc cmd/<command>'.
37 `,
38 }
39
40 var toolN bool
41
42
43
44
45 func isGccgoTool(tool string) bool {
46 switch tool {
47 case "cgo", "fix", "cover", "godoc", "vet":
48 return true
49 }
50 return false
51 }
52
53 func init() {
54 base.AddChdirFlag(&CmdTool.Flag)
55 CmdTool.Flag.BoolVar(&toolN, "n", false, "")
56 }
57
58 func runTool(ctx context.Context, cmd *base.Command, args []string) {
59 if len(args) == 0 {
60 listTools()
61 return
62 }
63 toolName := args[0]
64
65 for _, c := range toolName {
66 switch {
67 case 'a' <= c && c <= 'z', '0' <= c && c <= '9', c == '_':
68 default:
69 fmt.Fprintf(os.Stderr, "go: bad tool name %q\n", toolName)
70 base.SetExitStatus(2)
71 return
72 }
73 }
74
75 toolPath, err := base.ToolPath(toolName)
76 if err != nil {
77 if toolName == "dist" && len(args) > 1 && args[1] == "list" {
78
79
80
81
82
83
84 if impersonateDistList(args[2:]) {
85 return
86 }
87 }
88
89
90 _ = base.Tool(toolName)
91 }
92
93 if toolN {
94 cmd := toolPath
95 if len(args) > 1 {
96 cmd += " " + strings.Join(args[1:], " ")
97 }
98 fmt.Printf("%s\n", cmd)
99 return
100 }
101 args[0] = toolPath
102 toolCmd := &exec.Cmd{
103 Path: toolPath,
104 Args: args,
105 Stdin: os.Stdin,
106 Stdout: os.Stdout,
107 Stderr: os.Stderr,
108 }
109 err = toolCmd.Start()
110 if err == nil {
111 c := make(chan os.Signal, 100)
112 signal.Notify(c)
113 go func() {
114 for sig := range c {
115 toolCmd.Process.Signal(sig)
116 }
117 }()
118 err = toolCmd.Wait()
119 signal.Stop(c)
120 close(c)
121 }
122 if err != nil {
123
124
125
126
127
128 if e, ok := err.(*exec.ExitError); !ok || !e.Exited() || cfg.BuildX {
129 fmt.Fprintf(os.Stderr, "go tool %s: %s\n", toolName, err)
130 }
131 base.SetExitStatus(1)
132 return
133 }
134 }
135
136
137 func listTools() {
138 f, err := os.Open(build.ToolDir)
139 if err != nil {
140 fmt.Fprintf(os.Stderr, "go: no tool directory: %s\n", err)
141 base.SetExitStatus(2)
142 return
143 }
144 defer f.Close()
145 names, err := f.Readdirnames(-1)
146 if err != nil {
147 fmt.Fprintf(os.Stderr, "go: can't read tool directory: %s\n", err)
148 base.SetExitStatus(2)
149 return
150 }
151
152 sort.Strings(names)
153 for _, name := range names {
154
155
156 name = strings.TrimSuffix(strings.ToLower(name), cfg.ToolExeSuffix())
157
158
159
160 if cfg.BuildToolchainName == "gccgo" && !isGccgoTool(name) {
161 continue
162 }
163 fmt.Println(name)
164 }
165 }
166
167 func impersonateDistList(args []string) (handled bool) {
168 fs := flag.NewFlagSet("go tool dist list", flag.ContinueOnError)
169 jsonFlag := fs.Bool("json", false, "produce JSON output")
170 brokenFlag := fs.Bool("broken", false, "include broken ports")
171
172
173
174
175 _ = fs.Bool("v", false, "emit extra information")
176
177 if err := fs.Parse(args); err != nil || len(fs.Args()) > 0 {
178
179
180 return false
181 }
182
183 if !*jsonFlag {
184 for _, p := range platform.List {
185 if !*brokenFlag && platform.Broken(p.GOOS, p.GOARCH) {
186 continue
187 }
188 fmt.Println(p)
189 }
190 return true
191 }
192
193 type jsonResult struct {
194 GOOS string
195 GOARCH string
196 CgoSupported bool
197 FirstClass bool
198 Broken bool `json:",omitempty"`
199 }
200
201 var results []jsonResult
202 for _, p := range platform.List {
203 broken := platform.Broken(p.GOOS, p.GOARCH)
204 if broken && !*brokenFlag {
205 continue
206 }
207 if *jsonFlag {
208 results = append(results, jsonResult{
209 GOOS: p.GOOS,
210 GOARCH: p.GOARCH,
211 CgoSupported: platform.CgoSupported(p.GOOS, p.GOARCH),
212 FirstClass: platform.FirstClass(p.GOOS, p.GOARCH),
213 Broken: broken,
214 })
215 }
216 }
217 out, err := json.MarshalIndent(results, "", "\t")
218 if err != nil {
219 return false
220 }
221
222 os.Stdout.Write(out)
223 return true
224 }
225
View as plain text