// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This file enables an external tool to intercept package requests.
// If the tool is present then its results are used in preference to
// the go list command.

package packages

import (
	
	
	
	exec 
	
	
)

// The Driver Protocol
//
// The driver, given the inputs to a call to Load, returns metadata about the packages specified.
// This allows for different build systems to support go/packages by telling go/packages how the
// packages' source is organized.
// The driver is a binary, either specified by the GOPACKAGESDRIVER environment variable or in
// the path as gopackagesdriver. It's given the inputs to load in its argv. See the package
// documentation in doc.go for the full description of the patterns that need to be supported.
// A driver receives as a JSON-serialized driverRequest struct in standard input and will
// produce a JSON-serialized driverResponse (see definition in packages.go) in its standard output.

// driverRequest is used to provide the portion of Load's Config that is needed by a driver.
type driverRequest struct {
	Mode LoadMode `json:"mode"`
	// Env specifies the environment the underlying build system should be run in.
	Env []string `json:"env"`
	// BuildFlags are flags that should be passed to the underlying build system.
	BuildFlags []string `json:"build_flags"`
	// Tests specifies whether the patterns should also return test packages.
	Tests bool `json:"tests"`
	// Overlay maps file paths (relative to the driver's working directory) to the byte contents
	// of overlay files.
	Overlay map[string][]byte `json:"overlay"`
}

// findExternalDriver returns the file path of a tool that supplies
// the build system package structure, or "" if not found."
// If GOPACKAGESDRIVER is set in the environment findExternalTool returns its
// value, otherwise it searches for a binary named gopackagesdriver on the PATH.
func ( *Config) driver {
	const  = "GOPACKAGESDRIVER="
	 := ""
	for ,  := range .Env {
		if  := strings.TrimPrefix(, );  !=  {
			 = 
		}
	}
	if  != "" &&  == "off" {
		return nil
	}
	if  == "" {
		var  error
		,  = exec.LookPath("gopackagesdriver")
		if  != nil {
			return nil
		}
	}
	return func( *Config,  ...string) (*driverResponse, error) {
		,  := json.Marshal(driverRequest{
			Mode:       .Mode,
			Env:        .Env,
			BuildFlags: .BuildFlags,
			Tests:      .Tests,
			Overlay:    .Overlay,
		})
		if  != nil {
			return nil, fmt.Errorf("failed to encode message to driver tool: %v", )
		}

		 := new(bytes.Buffer)
		 := new(bytes.Buffer)
		 := exec.CommandContext(.Context, , ...)
		.Dir = .Dir
		.Env = .Env
		.Stdin = bytes.NewReader()
		.Stdout = 
		.Stderr = 

		if  := .Run();  != nil {
			return nil, fmt.Errorf("%v: %v: %s", , , .Stderr)
		}
		if len(.Bytes()) != 0 && os.Getenv("GOPACKAGESPRINTDRIVERERRORS") != "" {
			fmt.Fprintf(os.Stderr, "%s stderr: <<%s>>\n", cmdDebugStr(), )
		}

		var  driverResponse
		if  := json.Unmarshal(.Bytes(), &);  != nil {
			return nil, 
		}
		return &, nil
	}
}