github.com/c0deoo1/golang1.5@v0.0.0-20220525150107-c87c805d4593/src/cmd/go/tool.go (about) 1 // Copyright 2011 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 main 6 7 import ( 8 "fmt" 9 "go/build" 10 "os" 11 "os/exec" 12 "path/filepath" 13 "runtime" 14 "sort" 15 "strings" 16 ) 17 18 var cmdTool = &Command{ 19 Run: runTool, 20 UsageLine: "tool [-n] command [args...]", 21 Short: "run specified go tool", 22 Long: ` 23 Tool runs the go tool command identified by the arguments. 24 With no arguments it prints the list of known tools. 25 26 The -n flag causes tool to print the command that would be 27 executed but not execute it. 28 29 For more about each tool command, see 'go tool command -h'. 30 `, 31 } 32 33 var ( 34 toolGOOS = runtime.GOOS 35 toolGOARCH = runtime.GOARCH 36 toolIsWindows = toolGOOS == "windows" 37 toolDir = build.ToolDir 38 39 toolN bool 40 ) 41 42 func init() { 43 cmdTool.Flag.BoolVar(&toolN, "n", false, "") 44 } 45 46 const toolWindowsExtension = ".exe" 47 48 func tool(toolName string) string { 49 toolPath := filepath.Join(toolDir, toolName) 50 if toolIsWindows { 51 toolPath += toolWindowsExtension 52 } 53 if len(buildToolExec) > 0 { 54 return toolPath 55 } 56 // Give a nice message if there is no tool with that name. 57 if _, err := os.Stat(toolPath); err != nil { 58 if isInGoToolsRepo(toolName) { 59 fmt.Fprintf(os.Stderr, "go tool: no such tool %q; to install:\n\tgo get golang.org/x/tools/cmd/%s\n", toolName, toolName) 60 } else { 61 fmt.Fprintf(os.Stderr, "go tool: no such tool %q\n", toolName) 62 } 63 setExitStatus(3) 64 exit() 65 } 66 return toolPath 67 } 68 69 func isInGoToolsRepo(toolName string) bool { 70 return false 71 } 72 73 func runTool(cmd *Command, args []string) { 74 if len(args) == 0 { 75 listTools() 76 return 77 } 78 toolName := args[0] 79 // The tool name must be lower-case letters, numbers or underscores. 80 for _, c := range toolName { 81 switch { 82 case 'a' <= c && c <= 'z', '0' <= c && c <= '9', c == '_': 83 default: 84 fmt.Fprintf(os.Stderr, "go tool: bad tool name %q\n", toolName) 85 setExitStatus(2) 86 return 87 } 88 } 89 toolPath := tool(toolName) 90 if toolPath == "" { 91 return 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 toolCmd := &exec.Cmd{ 102 Path: toolPath, 103 Args: args, 104 Stdin: os.Stdin, 105 Stdout: os.Stdout, 106 Stderr: os.Stderr, 107 // Set $GOROOT, mainly for go tool dist. 108 Env: mergeEnvLists([]string{"GOROOT=" + goroot}, os.Environ()), 109 } 110 err := toolCmd.Run() 111 if err != nil { 112 // Only print about the exit status if the command 113 // didn't even run (not an ExitError) or it didn't exit cleanly 114 // or we're printing command lines too (-x mode). 115 // Assume if command exited cleanly (even with non-zero status) 116 // it printed any messages it wanted to print. 117 if e, ok := err.(*exec.ExitError); !ok || !e.Exited() || buildX { 118 fmt.Fprintf(os.Stderr, "go tool %s: %s\n", toolName, err) 119 } 120 setExitStatus(1) 121 return 122 } 123 } 124 125 // listTools prints a list of the available tools in the tools directory. 126 func listTools() { 127 f, err := os.Open(toolDir) 128 if err != nil { 129 fmt.Fprintf(os.Stderr, "go tool: no tool directory: %s\n", err) 130 setExitStatus(2) 131 return 132 } 133 defer f.Close() 134 names, err := f.Readdirnames(-1) 135 if err != nil { 136 fmt.Fprintf(os.Stderr, "go tool: can't read directory: %s\n", err) 137 setExitStatus(2) 138 return 139 } 140 141 sort.Strings(names) 142 for _, name := range names { 143 // Unify presentation by going to lower case. 144 name = strings.ToLower(name) 145 // If it's windows, don't show the .exe suffix. 146 if toolIsWindows && strings.HasSuffix(name, toolWindowsExtension) { 147 name = name[:len(name)-len(toolWindowsExtension)] 148 } 149 fmt.Println(name) 150 } 151 }