mirror of
https://github.com/go-gitea/gitea
synced 2024-11-05 17:54:26 +00:00
d1353e1f7c
* update code.gitea.io/sdk/gitea v0.13.1 -> v0.13.2 * update github.com/go-swagger/go-swagger v0.25.0 -> v0.26.0 * update github.com/google/uuid v1.1.2 -> v1.2.0 * update github.com/klauspost/compress v1.11.3 -> v1.11.7 * update github.com/lib/pq 083382b7e6fc -> v1.9.0 * update github.com/markbates/goth v1.65.0 -> v1.66.1 * update github.com/mattn/go-sqlite3 v1.14.4 -> v1.14.6 * update github.com/mgechev/revive 246eac737dc7 -> v1.0.3 * update github.com/minio/minio-go/v7 v7.0.6 -> v7.0.7 * update github.com/niklasfasching/go-org v1.3.2 -> v1.4.0 * update github.com/olivere/elastic/v7 v7.0.21 -> v7.0.22 * update github.com/pquerna/otp v1.2.0 -> v1.3.0 * update github.com/xanzy/go-gitlab v0.39.0 -> v0.42.0 * update github.com/yuin/goldmark v1.2.1 -> v1.3.1
102 lines
3.4 KiB
Go
Vendored
102 lines
3.4 KiB
Go
Vendored
// 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 (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
exec "golang.org/x/sys/execabs"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// 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 findExternalDriver(cfg *Config) driver {
|
|
const toolPrefix = "GOPACKAGESDRIVER="
|
|
tool := ""
|
|
for _, env := range cfg.Env {
|
|
if val := strings.TrimPrefix(env, toolPrefix); val != env {
|
|
tool = val
|
|
}
|
|
}
|
|
if tool != "" && tool == "off" {
|
|
return nil
|
|
}
|
|
if tool == "" {
|
|
var err error
|
|
tool, err = exec.LookPath("gopackagesdriver")
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
}
|
|
return func(cfg *Config, words ...string) (*driverResponse, error) {
|
|
req, err := json.Marshal(driverRequest{
|
|
Mode: cfg.Mode,
|
|
Env: cfg.Env,
|
|
BuildFlags: cfg.BuildFlags,
|
|
Tests: cfg.Tests,
|
|
Overlay: cfg.Overlay,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to encode message to driver tool: %v", err)
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
stderr := new(bytes.Buffer)
|
|
cmd := exec.CommandContext(cfg.Context, tool, words...)
|
|
cmd.Dir = cfg.Dir
|
|
cmd.Env = cfg.Env
|
|
cmd.Stdin = bytes.NewReader(req)
|
|
cmd.Stdout = buf
|
|
cmd.Stderr = stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
return nil, fmt.Errorf("%v: %v: %s", tool, err, cmd.Stderr)
|
|
}
|
|
if len(stderr.Bytes()) != 0 && os.Getenv("GOPACKAGESPRINTDRIVERERRORS") != "" {
|
|
fmt.Fprintf(os.Stderr, "%s stderr: <<%s>>\n", cmdDebugStr(cmd), stderr)
|
|
}
|
|
|
|
var response driverResponse
|
|
if err := json.Unmarshal(buf.Bytes(), &response); err != nil {
|
|
return nil, err
|
|
}
|
|
return &response, nil
|
|
}
|
|
}
|