github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/go/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  	"go/build"
    10  	"os"
    11  	"path/filepath"
    12  
    13  	"github.com/go-asm/go/cmd/go/cfg"
    14  	"github.com/go-asm/go/cmd/go/par"
    15  )
    16  
    17  // Tool returns the path to the named tool (for example, "vet").
    18  // If the tool cannot be found, Tool exits the process.
    19  func Tool(toolName string) string {
    20  	toolPath, err := ToolPath(toolName)
    21  	if err != nil && len(cfg.BuildToolexec) == 0 {
    22  		// Give a nice message if there is no tool with that name.
    23  		fmt.Fprintf(os.Stderr, "go: no such tool %q\n", toolName)
    24  		SetExitStatus(2)
    25  		Exit()
    26  	}
    27  	return toolPath
    28  }
    29  
    30  // Tool returns the path at which we expect to find the named tool
    31  // (for example, "vet"), and the error (if any) from statting that path.
    32  func ToolPath(toolName string) (string, error) {
    33  	toolPath := filepath.Join(build.ToolDir, toolName) + cfg.ToolExeSuffix()
    34  	err := toolStatCache.Do(toolPath, func() error {
    35  		_, err := os.Stat(toolPath)
    36  		return err
    37  	})
    38  	return toolPath, err
    39  }
    40  
    41  var toolStatCache par.Cache[string, error]