mirror of
https://github.com/golang/go
synced 2024-11-19 00:54:42 -07:00
go/packages: fix internal go list function signatures
After a discussion in cl/125535 this is a proposal to clean up some internal function signatures that were becomming unwieldy. Change-Id: I37a9e525ab18dface9ea9fb0b0ec68ac942ee632 Reviewed-on: https://go-review.googlesource.com/125659 Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
parent
4d8a0ac9f6
commit
14e571052b
@ -8,7 +8,6 @@ package packages
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@ -31,7 +30,7 @@ type GoTooOldError struct {
|
|||||||
// golistPackages uses the "go list" command to expand the
|
// golistPackages uses the "go list" command to expand the
|
||||||
// pattern words and return metadata for the specified packages.
|
// pattern words and return metadata for the specified packages.
|
||||||
// dir may be "" and env may be nil, as per os/exec.Command.
|
// dir may be "" and env may be nil, as per os/exec.Command.
|
||||||
func golistPackages(ctx context.Context, dir string, env []string, export, tests, deps bool, words []string) ([]*loaderPackage, error) {
|
func golistPackages(cfg *rawConfig, words ...string) ([]*loaderPackage, error) {
|
||||||
// Fields must match go list;
|
// Fields must match go list;
|
||||||
// see $GOROOT/src/cmd/go/internal/load/pkg.go.
|
// see $GOROOT/src/cmd/go/internal/load/pkg.go.
|
||||||
type jsonPackage struct {
|
type jsonPackage struct {
|
||||||
@ -53,7 +52,6 @@ func golistPackages(ctx context.Context, dir string, env []string, export, tests
|
|||||||
ForTest string // q in a "p [q.test]" package, else ""
|
ForTest string // q in a "p [q.test]" package, else ""
|
||||||
DepOnly bool
|
DepOnly bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// go list uses the following identifiers in ImportPath and Imports:
|
// go list uses the following identifiers in ImportPath and Imports:
|
||||||
//
|
//
|
||||||
// "p" -- importable package or main (command)
|
// "p" -- importable package or main (command)
|
||||||
@ -68,7 +66,7 @@ func golistPackages(ctx context.Context, dir string, env []string, export, tests
|
|||||||
// Run "go list" for complete
|
// Run "go list" for complete
|
||||||
// information on the specified packages.
|
// information on the specified packages.
|
||||||
|
|
||||||
buf, err := golist(ctx, dir, env, export, tests, deps, words)
|
buf, err := golist(cfg, words)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -184,24 +182,15 @@ func absJoin(dir string, fileses ...[]string) (res []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// golist returns the JSON-encoded result of a "go list args..." query.
|
// golist returns the JSON-encoded result of a "go list args..." query.
|
||||||
func golist(ctx context.Context, dir string, env []string, export, tests, deps bool, args []string) (*bytes.Buffer, error) {
|
func golist(cfg *rawConfig, args []string) (*bytes.Buffer, error) {
|
||||||
out := new(bytes.Buffer)
|
out := new(bytes.Buffer)
|
||||||
cmd := exec.CommandContext(ctx, "go", append([]string{
|
fullargs := []string{"list", "-e", "-json", "-cgo=true"}
|
||||||
"list",
|
fullargs = append(fullargs, cfg.Flags()...)
|
||||||
"-e",
|
fullargs = append(fullargs, "--")
|
||||||
fmt.Sprintf("-cgo=%t", true),
|
fullargs = append(fullargs, args...)
|
||||||
fmt.Sprintf("-test=%t", tests),
|
cmd := exec.CommandContext(cfg.Context, "go", fullargs...)
|
||||||
fmt.Sprintf("-export=%t", export),
|
cmd.Env = cfg.Env
|
||||||
fmt.Sprintf("-deps=%t", deps),
|
cmd.Dir = cfg.Dir
|
||||||
"-json",
|
|
||||||
"--",
|
|
||||||
}, args...)...)
|
|
||||||
|
|
||||||
if env == nil {
|
|
||||||
env = os.Environ()
|
|
||||||
}
|
|
||||||
cmd.Env = env
|
|
||||||
cmd.Dir = dir
|
|
||||||
cmd.Stdout = out
|
cmd.Stdout = out
|
||||||
cmd.Stderr = new(bytes.Buffer)
|
cmd.Stderr = new(bytes.Buffer)
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
@ -222,7 +211,7 @@ func golist(ctx context.Context, dir string, env []string, export, tests, deps b
|
|||||||
// If that build fails, errors appear on stderr
|
// If that build fails, errors appear on stderr
|
||||||
// (despite the -e flag) and the Export field is blank.
|
// (despite the -e flag) and the Export field is blank.
|
||||||
// Do not fail in that case.
|
// Do not fail in that case.
|
||||||
if !export {
|
if !cfg.Export {
|
||||||
return nil, fmt.Errorf("go list: %s: %s", exitErr, cmd.Stderr)
|
return nil, fmt.Errorf("go list: %s: %s", exitErr, cmd.Stderr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -266,9 +266,8 @@ func (ld *loader) load(patterns ...string) ([]*Package, error) {
|
|||||||
|
|
||||||
// Do the metadata query and partial build.
|
// Do the metadata query and partial build.
|
||||||
// TODO(adonovan): support alternative build systems at this seam.
|
// TODO(adonovan): support alternative build systems at this seam.
|
||||||
export := ld.Mode > LoadImports && ld.Mode < LoadAllSyntax
|
rawCfg := newRawConfig(&ld.Config)
|
||||||
deps := ld.Mode >= LoadImports
|
list, err := golistPackages(rawCfg, patterns...)
|
||||||
list, err := golistPackages(ld.Context, ld.Dir, ld.Env, export, ld.Tests, deps, patterns)
|
|
||||||
if _, ok := err.(GoTooOldError); ok {
|
if _, ok := err.(GoTooOldError); ok {
|
||||||
return loaderFallback(ld.Dir, ld.Env, patterns)
|
return loaderFallback(ld.Dir, ld.Env, patterns)
|
||||||
}
|
}
|
||||||
|
48
go/packages/raw.go
Normal file
48
go/packages/raw.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
package packages
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// This file contains the structs needed at the seam between the packages
|
||||||
|
// loader and the underlying build tool
|
||||||
|
|
||||||
|
// rawConfig specifies details about what raw package information is needed
|
||||||
|
// and how the underlying build tool should load package data.
|
||||||
|
type rawConfig struct {
|
||||||
|
Context context.Context
|
||||||
|
Dir string
|
||||||
|
Env []string
|
||||||
|
Export bool
|
||||||
|
Tests bool
|
||||||
|
Deps bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRawConfig(cfg *Config) *rawConfig {
|
||||||
|
rawCfg := &rawConfig{
|
||||||
|
Context: cfg.Context,
|
||||||
|
Dir: cfg.Dir,
|
||||||
|
Env: cfg.Env,
|
||||||
|
Export: cfg.Mode > LoadImports && cfg.Mode < LoadAllSyntax,
|
||||||
|
Tests: cfg.Tests,
|
||||||
|
Deps: cfg.Mode >= LoadImports,
|
||||||
|
}
|
||||||
|
if rawCfg.Env == nil {
|
||||||
|
rawCfg.Env = os.Environ()
|
||||||
|
}
|
||||||
|
return rawCfg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *rawConfig) Flags() []string {
|
||||||
|
return []string{
|
||||||
|
fmt.Sprintf("-test=%t", cfg.Tests),
|
||||||
|
fmt.Sprintf("-export=%t", cfg.Export),
|
||||||
|
fmt.Sprintf("-deps=%t", cfg.Deps),
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user