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

     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  	"runtime"
    13  
    14  	"cmd/go/internal/cfg"
    15  )
    16  
    17  // Configuration for finding tool binaries.
    18  var (
    19  	ToolGOOS      = runtime.GOOS
    20  	ToolGOARCH    = runtime.GOARCH
    21  	ToolIsWindows = ToolGOOS == "windows"
    22  	ToolDir       = build.ToolDir
    23  )
    24  
    25  const ToolWindowsExtension = ".exe"
    26  
    27  // Tool returns the path to the named tool (for example, "vet").
    28  // If the tool cannot be found, Tool exits the process.
    29  func Tool(toolName string) string {
    30  	toolPath := filepath.Join(ToolDir, toolName)
    31  	if ToolIsWindows {
    32  		toolPath += ToolWindowsExtension
    33  	}
    34  	if len(cfg.BuildToolexec) > 0 {
    35  		return toolPath
    36  	}
    37  	// Give a nice message if there is no tool with that name.
    38  	if _, err := os.Stat(toolPath); err != nil {
    39  		fmt.Fprintf(os.Stderr, "go: no such tool %q\n", toolName)
    40  		SetExitStatus(2)
    41  		Exit()
    42  	}
    43  	return toolPath
    44  }
    45  

View as plain text