Projects STRLCPY syft Commits f11a7b5e
🤬
Revision indexing in progress... (symbol navigation in revisions will be accurate after indexed)
  • ■ ■ ■ ■ ■ ■
    internal/config/application.go
    skipped 2 lines
    3 3  import (
    4 4   "errors"
    5 5   "fmt"
     6 + "os"
    6 7   "path"
    7 8   "reflect"
    8 9   "sort"
    skipped 199 lines
    208 209   return string(appaStr)
    209 210  }
    210 211   
     212 +// nolint:funlen
    211 213  func loadConfig(v *viper.Viper, configPath string) error {
    212 214   var err error
    213 215   // use explicitly the given user config
    skipped 9 lines
    223 225   
    224 226   // start searching for valid configs in order...
    225 227   // 1. look for .<appname>.yaml (in the current directory)
     228 + confFilePath := "." + internal.ApplicationName
     229 + 
     230 + // TODO: Remove this before v1.0.0
     231 + // See syft #1634
    226 232   v.AddConfigPath(".")
    227  - v.SetConfigName("." + internal.ApplicationName)
    228  - if err = v.ReadInConfig(); err == nil {
    229  - v.Set("config", v.ConfigFileUsed())
    230  - return nil
    231  - } else if !errors.As(err, &viper.ConfigFileNotFoundError{}) {
    232  - return fmt.Errorf("unable to parse config=%q: %w", v.ConfigFileUsed(), err)
     233 + v.SetConfigName(confFilePath)
     234 + 
     235 + // check if config.yaml exists in the current directory
     236 + // DEPRECATED: this will be removed in v1.0.0
     237 + if _, err := os.Stat("config.yaml"); err == nil {
     238 + log.Warn("DEPRECATED: ./config.yaml as a configuration file is deprecated and will be removed as an option in v1.0.0, please rename to .syft.yaml")
     239 + }
     240 + 
     241 + if _, err := os.Stat(confFilePath + ".yaml"); err == nil {
     242 + if err = v.ReadInConfig(); err == nil {
     243 + v.Set("config", v.ConfigFileUsed())
     244 + return nil
     245 + } else if !errors.As(err, &viper.ConfigFileNotFoundError{}) {
     246 + return fmt.Errorf("unable to parse config=%q: %w", v.ConfigFileUsed(), err)
     247 + }
    233 248   }
    234 249   
    235 250   // 2. look for .<appname>/config.yaml (in the current directory)
    skipped 19 lines
    255 270   }
    256 271   }
    257 272   
    258  - // 4. look for <appname>/config.yaml in xdg locations (starting with xdg home config dir, then moving upwards)
    259  - v.AddConfigPath(path.Join(xdg.ConfigHome, internal.ApplicationName))
     273 + // 4. look for .<appname>/config.yaml in xdg locations (starting with xdg home config dir, then moving upwards)
     274 + 
     275 + v.SetConfigName("config")
     276 + configPath = path.Join(xdg.ConfigHome, "."+internal.ApplicationName)
     277 + v.AddConfigPath(configPath)
    260 278   for _, dir := range xdg.ConfigDirs {
    261  - v.AddConfigPath(path.Join(dir, internal.ApplicationName))
     279 + v.AddConfigPath(path.Join(dir, "."+internal.ApplicationName))
    262 280   }
    263  - v.SetConfigName("config")
    264 281   if err = v.ReadInConfig(); err == nil {
    265 282   v.Set("config", v.ConfigFileUsed())
    266 283   return nil
    skipped 6 lines
  • ■ ■ ■ ■ ■ ■
    internal/config/application_test.go
     1 +package config
     2 + 
     3 +import (
     4 + "os"
     5 + "path"
     6 + "testing"
     7 + 
     8 + "github.com/adrg/xdg"
     9 + "github.com/spf13/viper"
     10 + "github.com/stretchr/testify/assert"
     11 +)
     12 + 
     13 +// TODO: set negative case when config.yaml is no longer a valid option
     14 +func TestApplicationConfig(t *testing.T) {
     15 + // config is picked up at desired configuration paths
     16 + // VALID: .syft.yaml, .syft/config.yaml, ~/.syft.yaml, <XDG_CONFIG_HOME>/syft/config.yaml
     17 + // DEPRECATED: config.yaml is currently supported by
     18 + tests := []struct {
     19 + name string
     20 + setup func(t *testing.T) string
     21 + assertions func(t *testing.T, app *Application)
     22 + Cleanup func(t *testing.T)
     23 + }{
     24 + {
     25 + name: "explicit config",
     26 + setup: func(t *testing.T) string {
     27 + return "./test-fixtures/.syft.yaml"
     28 + }, // no-op for explicit config
     29 + assertions: func(t *testing.T, app *Application) {
     30 + assert.Equal(t, "test-explicit-config", app.File)
     31 + },
     32 + Cleanup: func(t *testing.T) {},
     33 + },
     34 + {
     35 + name: "current working directory named config",
     36 + setup: func(t *testing.T) string {
     37 + err := os.Chdir("./test-fixtures/config-wd-file") // change application cwd to test-fixtures
     38 + if err != nil {
     39 + t.Fatalf("%s failed to change cwd: %+v", t.Name(), err)
     40 + }
     41 + return ""
     42 + },
     43 + assertions: func(t *testing.T, app *Application) {
     44 + assert.Equal(t, "test-wd-named-config", app.File)
     45 + },
     46 + Cleanup: func(t *testing.T) {},
     47 + },
     48 + {
     49 + name: "current working directory syft dir config",
     50 + setup: func(t *testing.T) string {
     51 + err := os.Chdir("./test-fixtures/config-dir-test") // change application cwd to test-fixtures
     52 + if err != nil {
     53 + t.Fatalf("%s failed to change cwd: %+v", t.Name(), err)
     54 + }
     55 + return ""
     56 + },
     57 + assertions: func(t *testing.T, app *Application) {
     58 + assert.Equal(t, "test-dir-config", app.File)
     59 + },
     60 + Cleanup: func(t *testing.T) {},
     61 + },
     62 + {
     63 + name: "home directory file config",
     64 + setup: func(t *testing.T) string {
     65 + // Because Setenv affects the whole process, it cannot be used in parallel tests or
     66 + // tests with parallel ancestors: see separate XDG test for consequence of this
     67 + t.Setenv("HOME", "./test-fixtures/config-home-test")
     68 + err := os.Link("./test-fixtures/config-home-test/config-file/.syft.yaml", "./test-fixtures/config-home-test/.syft.yaml")
     69 + if err != nil {
     70 + t.Fatalf("%s failed to link home config: %+v", t.Name(), err)
     71 + }
     72 + return ""
     73 + },
     74 + assertions: func(t *testing.T, app *Application) {
     75 + assert.Equal(t, "test-home-config", app.File)
     76 + },
     77 + Cleanup: func(t *testing.T) {
     78 + err := os.Remove("./test-fixtures/config-home-test/.syft.yaml") //
     79 + if err != nil {
     80 + t.Fatalf("%s failed to remove home config link: %+v", t.Name(), err)
     81 + }
     82 + },
     83 + },
     84 + {
     85 + name: "XDG file config",
     86 + setup: func(t *testing.T) string {
     87 + wd, err := os.Getwd()
     88 + if err != nil {
     89 + t.Fatalf("%s: failed to get working directory: %+v", t.Name(), err)
     90 + }
     91 + configDir := path.Join(wd, "./test-fixtures/config-home-test") // set HOME to testdata
     92 + t.Setenv("XDG_CONFIG_DIRS", configDir)
     93 + xdg.Reload()
     94 + return ""
     95 + },
     96 + assertions: func(t *testing.T, app *Application) {
     97 + assert.Equal(t, "test-home-XDG-config", app.File)
     98 + },
     99 + Cleanup: func(t *testing.T) {},
     100 + },
     101 + }
     102 + for _, test := range tests {
     103 + t.Run(test.name, func(t *testing.T) {
     104 + defer test.Cleanup(t)
     105 + wd, err := os.Getwd()
     106 + if err != nil {
     107 + t.Fatalf("failed to get working directory: %+v", err)
     108 + }
     109 + defer os.Chdir(wd) // reset working directory after test
     110 + application := &Application{}
     111 + viperInstance := viper.New()
     112 + 
     113 + configPath := test.setup(t)
     114 + err = application.LoadAllValues(viperInstance, configPath)
     115 + if err != nil {
     116 + t.Fatalf("failed to load application config: %+v", err)
     117 + }
     118 + test.assertions(t, application)
     119 + })
     120 + }
     121 +}
     122 + 
  • ■ ■ ■ ■ ■ ■
    internal/config/test-fixtures/.syft.yaml
     1 +# same as --file; write output report to a file (default is to write to stdout)
     2 +file: "test-explicit-config"
     3 +package:
     4 + cataloger:
     5 + scope: "squashed"
     6 + 
     7 + # same as --scope; limit the scope of the cataloger to only the specified types
  • ■ ■ ■ ■ ■
    internal/config/test-fixtures/config-dir-test/.syft/config.yaml
     1 +# same as --file; write output report to a file (default is to write to stdout)
     2 +file: "test-dir-config"
     3 +package:
     4 + cataloger:
     5 + scope: "squashed"
  • ■ ■ ■ ■ ■
    internal/config/test-fixtures/config-home-test/.syft/config.yaml
     1 +# same as --file; write output report to a file (default is to write to stdout)
     2 +file: "test-home-XDG-config"
     3 +package:
     4 + cataloger:
     5 + scope: "squashed"
  • ■ ■ ■ ■ ■
    internal/config/test-fixtures/config-home-test/config-file/.syft.yaml
     1 +# same as --file; write output report to a file (default is to write to stdout)
     2 +file: "test-home-config"
     3 +package:
     4 + cataloger:
     5 + scope: "squashed"
  • ■ ■ ■ ■ ■
    internal/config/test-fixtures/config-wd-file/.syft.yaml
     1 +# same as --file; write output report to a file (default is to write to stdout)
     2 +file: "test-wd-named-config"
     3 +package:
     4 + cataloger:
     5 + scope: "squashed"
Please wait...
Page is in error, reload to recover