Debian Codegraph @

src/cmd/go/internal/base/tool.go

raw
1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package base
6
7 import (
8 "fmt"
9 "go/build"
10 "os"
11 "path/filepath"
12
13 "cmd/go/internal/cfg"
14 "cmd/internal/par"
15 )
16
17 // Tool returns the path to the named builtin tool (for example, "vet").
18 // If the tool cannot be found, Tool exits the process.
19 func Tool(toolName string) string {
20 toolPath, err := ToolPath(toolName)
21 if err != nil && len(cfg.BuildToolexec) == 0 {
22 // Give a nice message if there is no tool with that name.
23 fmt.Fprintf(os.Stderr, "go: no such tool %q\n", toolName)
24 SetExitStatus(2)
25 Exit()
26 }
27 return toolPath
28 }
29
30 // ToolPath returns the path at which we expect to find the named tool
31 // (for example, "vet"), and the error (if any) from statting that path.
32 func ToolPath(toolName string) (string, error) {
33 if !ValidToolName(toolName) {
34 return "", fmt.Errorf("bad tool name: %q", toolName)
35 }
36 toolPath := filepath.Join(build.ToolDir, toolName) + cfg.ToolExeSuffix()
37 err := toolStatCache.Do(toolPath, func() error {
38 _, err := os.Stat(toolPath)
39 return err
40 })
41 return toolPath, err
42 }
43
44 func ValidToolName(toolName string) bool {
45 for _, c := range toolName {
46 switch {
47 case 'a' <= c && c <= 'z', '0' <= c && c <= '9', c == '_':
48 default:
49 return false
50 }
51 }
52 return true
53 }
54
55 var toolStatCache par.Cache[string, error]