github.com/lixvbnet/courtney@v0.0.0-20221025031132-0dcb02231211/shared/shared.go (about)

     1  package shared
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/dave/patsy"
     7  	"github.com/dave/patsy/vos"
     8  )
     9  
    10  // Setup holds globals, environment and command line flags for the courtney
    11  // command
    12  type Setup struct {
    13  	Env      vos.Env
    14  	Paths    *patsy.Cache
    15  	Enforce  bool
    16  	Verbose  bool
    17  	Short    bool
    18  	Timeout  string
    19  	Load     string
    20  	Output   string
    21  	TestArgs []string
    22  	Packages []PackageSpec
    23  }
    24  
    25  // PackageSpec identifies a package by dir and path
    26  type PackageSpec struct {
    27  	Dir  string
    28  	Path string
    29  }
    30  
    31  // Parse parses a slice of strings into the Packages slice
    32  func (s *Setup) Parse(args []string) error {
    33  	if len(args) == 0 {
    34  		args = []string{"./..."}
    35  	}
    36  	packages := map[string]string{}
    37  	for _, ppath := range args {
    38  		ppath = strings.TrimSuffix(ppath, "/")
    39  
    40  		paths, err := s.Paths.Dirs(ppath)
    41  		if err != nil {
    42  			return err
    43  		}
    44  
    45  		for importPath, dir := range paths {
    46  			packages[importPath] = dir
    47  		}
    48  	}
    49  	for ppath, dir := range packages {
    50  		s.Packages = append(s.Packages, PackageSpec{Path: ppath, Dir: dir})
    51  	}
    52  	return nil
    53  }