gopkg.in/tools/godep.v21@v21.0.0-20151104013723-2cf1d6e3f557/pkg.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"os"
     7  	"os/exec"
     8  )
     9  
    10  // Package represents a Go package.
    11  type Package struct {
    12  	Dir        string
    13  	Root       string
    14  	ImportPath string
    15  	Deps       []string
    16  	Standard   bool
    17  
    18  	GoFiles        []string
    19  	CgoFiles       []string
    20  	IgnoredGoFiles []string
    21  
    22  	TestGoFiles  []string
    23  	TestImports  []string
    24  	XTestGoFiles []string
    25  	XTestImports []string
    26  
    27  	Error struct {
    28  		Err string
    29  	}
    30  }
    31  
    32  // LoadPackages loads the named packages using go list -json.
    33  // Unlike the go tool, an empty argument list is treated as
    34  // an empty list; "." must be given explicitly if desired.
    35  func LoadPackages(name ...string) (a []*Package, err error) {
    36  	if len(name) == 0 {
    37  		return nil, nil
    38  	}
    39  	args := []string{"list", "-e", "-json"}
    40  	cmd := exec.Command("go", append(args, name...)...)
    41  	r, err := cmd.StdoutPipe()
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	cmd.Stderr = os.Stderr
    46  	err = cmd.Start()
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	d := json.NewDecoder(r)
    51  	for {
    52  		info := new(Package)
    53  		err = d.Decode(info)
    54  		if err == io.EOF {
    55  			break
    56  		}
    57  		if err != nil {
    58  			info.Error.Err = err.Error()
    59  		}
    60  		a = append(a, info)
    61  	}
    62  	err = cmd.Wait()
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	return a, nil
    67  }
    68  
    69  func (p *Package) allGoFiles() (a []string) {
    70  	a = append(a, p.GoFiles...)
    71  	a = append(a, p.CgoFiles...)
    72  	a = append(a, p.TestGoFiles...)
    73  	a = append(a, p.XTestGoFiles...)
    74  	a = append(a, p.IgnoredGoFiles...)
    75  	return a
    76  }