github.com/shulhan/golangci-lint@v1.10.1/pkg/packages/program.go (about)

     1  package packages
     2  
     3  import (
     4  	"fmt"
     5  	"go/build"
     6  )
     7  
     8  type Program struct {
     9  	packages []Package
    10  
    11  	bctx build.Context
    12  }
    13  
    14  func (p *Program) String() string {
    15  	files := p.Files(true)
    16  	if len(files) == 1 {
    17  		return files[0]
    18  	}
    19  
    20  	return fmt.Sprintf("%s", p.Dirs())
    21  }
    22  
    23  func (p *Program) BuildContext() *build.Context {
    24  	return &p.bctx
    25  }
    26  
    27  func (p Program) Packages() []Package {
    28  	return p.packages
    29  }
    30  
    31  func (p *Program) addPackage(pkg *Package) {
    32  	packagesToAdd := []Package{*pkg}
    33  	if len(pkg.bp.XTestGoFiles) != 0 {
    34  		// create separate package because xtest files have different package name
    35  		xbp := build.Package{
    36  			Dir:            pkg.bp.Dir,
    37  			ImportPath:     pkg.bp.ImportPath + "_test",
    38  			XTestGoFiles:   pkg.bp.XTestGoFiles,
    39  			XTestImportPos: pkg.bp.XTestImportPos,
    40  			XTestImports:   pkg.bp.XTestImports,
    41  		}
    42  		packagesToAdd = append(packagesToAdd, Package{
    43  			bp: &xbp,
    44  		})
    45  		pkg.bp.XTestGoFiles = nil
    46  		pkg.bp.XTestImportPos = nil
    47  		pkg.bp.XTestImports = nil
    48  	}
    49  
    50  	p.packages = append(p.packages, packagesToAdd...)
    51  }
    52  
    53  func (p *Program) Files(includeTest bool) []string {
    54  	var ret []string
    55  	for _, pkg := range p.packages {
    56  		ret = append(ret, pkg.Files(includeTest)...)
    57  	}
    58  
    59  	return ret
    60  }
    61  
    62  func (p *Program) Dirs() []string {
    63  	var ret []string
    64  	for _, pkg := range p.packages {
    65  		if !pkg.isFake {
    66  			ret = append(ret, pkg.Dir())
    67  		}
    68  	}
    69  
    70  	return ret
    71  }