github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/pkg/cataloger/golang/config.go (about)

     1  package golang
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/mitchellh/go-homedir"
     9  
    10  	"github.com/anchore/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  	MainModuleVersion           MainModuleVersionConfig `yaml:"main-module-version" json:"main-module-version" mapstructure:"main-module-version"`
    29  }
    30  
    31  type MainModuleVersionConfig struct {
    32  	FromLDFlags       bool `yaml:"from-ld-flags" json:"from-ld-flags" mapstructure:"from-ld-flags"`
    33  	FromContents      bool `yaml:"from-contents" json:"from-contents" mapstructure:"from-contents"`
    34  	FromBuildSettings bool `yaml:"from-build-settings" json:"from-build-settings" mapstructure:"from-build-settings"`
    35  }
    36  
    37  // DefaultCatalogerConfig create a CatalogerConfig with default options, which includes:
    38  // - setting the default remote proxy if none is provided
    39  // - setting the default no proxy if none is provided
    40  // - setting the default local module cache dir if none is provided
    41  func DefaultCatalogerConfig() CatalogerConfig {
    42  	g := CatalogerConfig{
    43  		MainModuleVersion: DefaultMainModuleVersionConfig(),
    44  	}
    45  
    46  	// first process the proxy settings
    47  	if len(g.Proxies) == 0 {
    48  		goProxy := os.Getenv("GOPROXY")
    49  		if goProxy == "" {
    50  			goProxy = defaultProxies
    51  		}
    52  		g = g.WithProxy(goProxy)
    53  	}
    54  
    55  	// next process the gonoproxy settings
    56  	if len(g.NoProxy) == 0 {
    57  		goPrivate := os.Getenv("GOPRIVATE")
    58  		goNoProxy := os.Getenv("GONOPROXY")
    59  		// we only use the env var if it was not set explicitly
    60  		if goPrivate != "" {
    61  			g.NoProxy = append(g.NoProxy, strings.Split(goPrivate, ",")...)
    62  		}
    63  
    64  		// next process the goprivate settings; we always add those
    65  		if goNoProxy != "" {
    66  			g.NoProxy = append(g.NoProxy, strings.Split(goNoProxy, ",")...)
    67  		}
    68  	}
    69  
    70  	if g.LocalModCacheDir == "" {
    71  		goPath := os.Getenv("GOPATH")
    72  
    73  		if goPath == "" {
    74  			homeDir, err := homedir.Dir()
    75  			if err != nil {
    76  				log.Debug("unable to determine user home dir: %v", err)
    77  			} else {
    78  				goPath = filepath.Join(homeDir, "go")
    79  			}
    80  		}
    81  		if goPath != "" {
    82  			g.LocalModCacheDir = filepath.Join(goPath, "pkg", "mod")
    83  		}
    84  	}
    85  	return g
    86  }
    87  
    88  func DefaultMainModuleVersionConfig() MainModuleVersionConfig {
    89  	return MainModuleVersionConfig{
    90  		FromLDFlags:       true,
    91  		FromContents:      true,
    92  		FromBuildSettings: true,
    93  	}
    94  }
    95  
    96  func (g CatalogerConfig) WithSearchLocalModCacheLicenses(input bool) CatalogerConfig {
    97  	g.SearchLocalModCacheLicenses = input
    98  	return g
    99  }
   100  
   101  func (g CatalogerConfig) WithLocalModCacheDir(input string) CatalogerConfig {
   102  	if input == "" {
   103  		return g
   104  	}
   105  	g.LocalModCacheDir = input
   106  	return g
   107  }
   108  
   109  func (g CatalogerConfig) WithSearchRemoteLicenses(input bool) CatalogerConfig {
   110  	g.SearchRemoteLicenses = input
   111  	return g
   112  }
   113  
   114  func (g CatalogerConfig) WithProxy(input string) CatalogerConfig {
   115  	if input == "" {
   116  		return g
   117  	}
   118  	if input == "off" {
   119  		input = directProxyOnly
   120  	}
   121  	g.Proxies = strings.Split(input, ",")
   122  	return g
   123  }
   124  
   125  func (g CatalogerConfig) WithNoProxy(input string) CatalogerConfig {
   126  	if input == "" {
   127  		return g
   128  	}
   129  	g.NoProxy = strings.Split(input, ",")
   130  	return g
   131  }
   132  
   133  func (g CatalogerConfig) WithMainModuleVersion(input MainModuleVersionConfig) CatalogerConfig {
   134  	g.MainModuleVersion = input
   135  	return g
   136  }
   137  
   138  func (g MainModuleVersionConfig) WithFromLDFlags(input bool) MainModuleVersionConfig {
   139  	g.FromLDFlags = input
   140  	return g
   141  }
   142  
   143  func (g MainModuleVersionConfig) WithFromContents(input bool) MainModuleVersionConfig {
   144  	g.FromContents = input
   145  	return g
   146  }
   147  
   148  func (g MainModuleVersionConfig) WithFromBuildSettings(input bool) MainModuleVersionConfig {
   149  	g.FromBuildSettings = input
   150  	return g
   151  }