github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/pkg/cataloger/golang/config.go (about)

     1  package golang
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  	"strings"
     7  
     8  	"github.com/mitchellh/go-homedir"
     9  
    10  	"github.com/lineaje-labs/syft/internal/log"
    11  )
    12  
    13  const (
    14  	defaultProxies  = "https://proxy.golang.org,direct"
    15  	directProxyOnly = "direct"
    16  )
    17  
    18  var (
    19  	directProxiesOnly = []string{directProxyOnly}
    20  )
    21  
    22  type CatalogerConfig struct {
    23  	SearchLocalModCacheLicenses bool     `yaml:"search-local-mod-cache-licenses" json:"search-local-mod-cache-licenses" mapstructure:"search-local-mod-cache-licenses"`
    24  	LocalModCacheDir            string   `yaml:"local-mod-cache-dir" json:"local-mod-cache-dir" mapstructure:"local-mod-cache-dir"`
    25  	SearchRemoteLicenses        bool     `yaml:"search-remote-licenses" json:"search-remote-licenses" mapstructure:"search-remote-licenses"`
    26  	Proxies                     []string `yaml:"proxies,omitempty" json:"proxies,omitempty" mapstructure:"proxies"`
    27  	NoProxy                     []string `yaml:"no-proxy,omitempty" json:"no-proxy,omitempty" mapstructure:"no-proxy"`
    28  }
    29  
    30  // DefaultCatalogerConfig create a CatalogerConfig with default options, which includes:
    31  // - setting the default remote proxy if none is provided
    32  // - setting the default no proxy if none is provided
    33  // - setting the default local module cache dir if none is provided
    34  func DefaultCatalogerConfig() CatalogerConfig {
    35  	g := CatalogerConfig{}
    36  
    37  	// first process the proxy settings
    38  	if len(g.Proxies) == 0 {
    39  		goProxy := os.Getenv("GOPROXY")
    40  		if goProxy == "" {
    41  			goProxy = defaultProxies
    42  		}
    43  		g = g.WithProxy(goProxy)
    44  	}
    45  
    46  	// next process the gonoproxy settings
    47  	if len(g.NoProxy) == 0 {
    48  		goPrivate := os.Getenv("GOPRIVATE")
    49  		goNoProxy := os.Getenv("GONOPROXY")
    50  		// we only use the env var if it was not set explicitly
    51  		if goPrivate != "" {
    52  			g.NoProxy = append(g.NoProxy, strings.Split(goPrivate, ",")...)
    53  		}
    54  
    55  		// next process the goprivate settings; we always add those
    56  		if goNoProxy != "" {
    57  			g.NoProxy = append(g.NoProxy, strings.Split(goNoProxy, ",")...)
    58  		}
    59  	}
    60  
    61  	if g.LocalModCacheDir == "" {
    62  		goPath := os.Getenv("GOPATH")
    63  
    64  		if goPath == "" {
    65  			homeDir, err := homedir.Dir()
    66  			if err != nil {
    67  				log.Debug("unable to determine user home dir: %v", err)
    68  			} else {
    69  				goPath = path.Join(homeDir, "go")
    70  			}
    71  		}
    72  		if goPath != "" {
    73  			g.LocalModCacheDir = path.Join(goPath, "pkg", "mod")
    74  		}
    75  	}
    76  	return g
    77  }
    78  
    79  func (g CatalogerConfig) WithSearchLocalModCacheLicenses(input bool) CatalogerConfig {
    80  	g.SearchLocalModCacheLicenses = input
    81  	return g
    82  }
    83  
    84  func (g CatalogerConfig) WithLocalModCacheDir(input string) CatalogerConfig {
    85  	if input == "" {
    86  		return g
    87  	}
    88  	g.LocalModCacheDir = input
    89  	return g
    90  }
    91  
    92  func (g CatalogerConfig) WithSearchRemoteLicenses(input bool) CatalogerConfig {
    93  	g.SearchRemoteLicenses = input
    94  	return g
    95  }
    96  
    97  func (g CatalogerConfig) WithProxy(input string) CatalogerConfig {
    98  	if input == "" {
    99  		return g
   100  	}
   101  	if input == "off" {
   102  		input = directProxyOnly
   103  	}
   104  	g.Proxies = strings.Split(input, ",")
   105  	return g
   106  }
   107  
   108  func (g CatalogerConfig) WithNoProxy(input string) CatalogerConfig {
   109  	if input == "" {
   110  		return g
   111  	}
   112  	g.NoProxy = strings.Split(input, ",")
   113  	return g
   114  }