github.com/bir3/gocompiler@v0.3.205/src/cmd/gocmd/internal/base/tool.go (about) 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 "os" 10 "os/exec" 11 "path/filepath" 12 "strings" 13 14 "github.com/bir3/gocompiler/src/go/build" 15 16 "github.com/bir3/gocompiler/src/cmd/gocmd/internal/cfg" 17 ) 18 19 // Tool returns the path to the named tool (for example, "vet"). 20 // If the tool cannot be found, Tool exits the process. 21 func Tool(toolName string) string { 22 toolPath := filepath.Join(build.ToolDir, toolName) + cfg.ToolExeSuffix() 23 if len(cfg.BuildToolexec) > 0 { 24 return toolPath 25 } 26 // Give a nice message if there is no tool with that name. 27 28 s := "gocompiler:" + toolName + ":" + toolPath 29 return s 30 } 31 32 func ToolCommand(exe string, args ...string) *exec.Cmd { 33 var cmd *exec.Cmd 34 if strings.HasPrefix(exe, "gocompiler:") { 35 // format: "gocompiler:<tool>:<original-path-is-ignored>" 36 s := exe[len("gocompiler:"):] 37 k := strings.Index(s, ":") 38 tool := s[0:k] 39 exe, err := os.Executable() 40 if err != nil { 41 panic(fmt.Sprintf("ToolCommand[gocompiler] failed to get self executable: %s", err)) 42 } 43 cmd = exec.Command(exe, args...) 44 cmd.Env = append(cmd.Environ(), "GOCOMPILER_TOOL="+tool) 45 } else { 46 cmd = exec.Command(exe, args...) 47 } 48 return cmd 49 }