github.com/euank/go@v0.0.0-20160829210321-495514729181/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 args[0] = toolPath // in case the tool wants to re-exec itself, e.g. cmd/dist 102 toolCmd := &exec.Cmd{ 103 Path: toolPath, 104 Args: args, 105 Stdin: os.Stdin, 106 Stdout: os.Stdout, 107 Stderr: os.Stderr, 108 // Set $GOROOT, mainly for go tool dist. 109 Env: mergeEnvLists([]string{"GOROOT=" + goroot}, os.Environ()), 110 } 111 err := toolCmd.Run() 112 if err != nil { 113 // Only print about the exit status if the command 114 // didn't even run (not an ExitError) or it didn't exit cleanly 115 // or we're printing command lines too (-x mode). 116 // Assume if command exited cleanly (even with non-zero status) 117 // it printed any messages it wanted to print. 118 if e, ok := err.(*exec.ExitError); !ok || !e.Exited() || buildX { 119 fmt.Fprintf(os.Stderr, "go tool %s: %s\n", toolName, err) 120 } 121 setExitStatus(1) 122 return 123 } 124 } 125 126 // listTools prints a list of the available tools in the tools directory. 127 func listTools() { 128 f, err := os.Open(toolDir) 129 if err != nil { 130 fmt.Fprintf(os.Stderr, "go tool: no tool directory: %s\n", err) 131 setExitStatus(2) 132 return 133 } 134 defer f.Close() 135 names, err := f.Readdirnames(-1) 136 if err != nil { 137 fmt.Fprintf(os.Stderr, "go tool: can't read directory: %s\n", err) 138 setExitStatus(2) 139 return 140 } 141 142 sort.Strings(names) 143 for _, name := range names { 144 // Unify presentation by going to lower case. 145 name = strings.ToLower(name) 146 // If it's windows, don't show the .exe suffix. 147 if toolIsWindows && strings.HasSuffix(name, toolWindowsExtension) { 148 name = name[:len(name)-len(toolWindowsExtension)] 149 } 150 fmt.Println(name) 151 } 152 }