rsc.io/go@v0.0.0-20150416155037-e040fd465409/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 switch toolName { 71 case "cover", "vet": 72 return true 73 } 74 return false 75 } 76 77 func runTool(cmd *Command, args []string) { 78 if len(args) == 0 { 79 listTools() 80 return 81 } 82 toolName := args[0] 83 // The tool name must be lower-case letters, numbers or underscores. 84 for _, c := range toolName { 85 switch { 86 case 'a' <= c && c <= 'z', '0' <= c && c <= '9', c == '_': 87 default: 88 fmt.Fprintf(os.Stderr, "go tool: bad tool name %q\n", toolName) 89 setExitStatus(2) 90 return 91 } 92 } 93 toolPath := tool(toolName) 94 if toolPath == "" { 95 return 96 } 97 if toolN { 98 cmd := toolPath 99 if len(args) > 1 { 100 cmd += " " + strings.Join(args[1:], " ") 101 } 102 fmt.Printf("%s\n", cmd) 103 return 104 } 105 toolCmd := &exec.Cmd{ 106 Path: toolPath, 107 Args: args, 108 Stdin: os.Stdin, 109 Stdout: os.Stdout, 110 Stderr: os.Stderr, 111 // Set $GOROOT, mainly for go tool dist. 112 Env: mergeEnvLists([]string{"GOROOT=" + goroot}, os.Environ()), 113 } 114 err := toolCmd.Run() 115 if err != nil { 116 // Only print about the exit status if the command 117 // didn't even run (not an ExitError) or it didn't exit cleanly 118 // or we're printing command lines too (-x mode). 119 // Assume if command exited cleanly (even with non-zero status) 120 // it printed any messages it wanted to print. 121 if e, ok := err.(*exec.ExitError); !ok || !e.Exited() || buildX { 122 fmt.Fprintf(os.Stderr, "go tool %s: %s\n", toolName, err) 123 } 124 setExitStatus(1) 125 return 126 } 127 } 128 129 // listTools prints a list of the available tools in the tools directory. 130 func listTools() { 131 f, err := os.Open(toolDir) 132 if err != nil { 133 fmt.Fprintf(os.Stderr, "go tool: no tool directory: %s\n", err) 134 setExitStatus(2) 135 return 136 } 137 defer f.Close() 138 names, err := f.Readdirnames(-1) 139 if err != nil { 140 fmt.Fprintf(os.Stderr, "go tool: can't read directory: %s\n", err) 141 setExitStatus(2) 142 return 143 } 144 145 sort.Strings(names) 146 for _, name := range names { 147 // Unify presentation by going to lower case. 148 name = strings.ToLower(name) 149 // If it's windows, don't show the .exe suffix. 150 if toolIsWindows && strings.HasSuffix(name, toolWindowsExtension) { 151 name = name[:len(name)-len(toolWindowsExtension)] 152 } 153 fmt.Println(name) 154 } 155 }