Skip to content

Commit

Permalink
use SYFT_LOG_FILE env var (#805)
Browse files Browse the repository at this point in the history
* use SYFT_LOG_FILE

Signed-off-by: Jonas Galvão Xavier <jonas.agx@gmail.com>

* enable debug logs when SYFT_LOG_FILE is set

Signed-off-by: Jonas Galvão Xavier <jonas.agx@gmail.com>

* set log.file and add tests

Signed-off-by: Jonas Galvão Xavier <jonas.agx@gmail.com>

* test log file in temp directory

Signed-off-by: Jonas Galvão Xavier <jonas.agx@gmail.com>

* add note on binding refactor

Signed-off-by: Jonas Galvão Xavier <jonas.agx@gmail.com>

* remove unused function

Signed-off-by: Jonas Galvão Xavier <jonas.agx@gmail.com>
  • Loading branch information
jonasagx committed Feb 9, 2022
1 parent 8f96ada commit ca081ae
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func setPackageFlags(flags *pflag.FlagSet) {
)
}

// NOTE(alex): Write a helper for the binding operation, which can be used to perform the binding but also double check that the intended effect was had or else return an error. Another thought is to somehow provide zero-valued defaults for all values in our config struct (maybe with reflection?). There may be a mechanism that already exists in viper that protects against this that I'm not aware of. ref: https://github.com/anchore/syft/pull/805#discussion_r801931192
func bindPackagesConfigOptions(flags *pflag.FlagSet) error {
// Formatting & Input options //////////////////////////////////////////////

Expand Down
1 change: 1 addition & 0 deletions internal/config/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ type logging struct {

func (cfg logging) loadDefaultValues(v *viper.Viper) {
v.SetDefault("log.structured", false)
v.SetDefault("log.file", "")
}
47 changes: 47 additions & 0 deletions test/cli/root_cmd_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cli

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/sergi/go-diff/diffmatchpatch"
"github.com/stretchr/testify/assert"
)

func TestRootCmdAliasesToPackagesSubcommand(t *testing.T) {
Expand Down Expand Up @@ -114,3 +117,47 @@ func TestPersistentFlags(t *testing.T) {
})
}
}

func TestLogFile(t *testing.T) {
request := "docker-archive:" + getFixtureImage(t, "image-pkg-coverage")

envLogFile := filepath.Join(os.TempDir(), "a-pretty-log-file.log")
t.Logf("log file path: %s", envLogFile)

tests := []struct {
name string
args []string
env map[string]string
assertions []traitAssertion
cleanup func()
}{
{
name: "env-var-log-file-name",
args: []string{"-vv", request},
env: map[string]string{"SYFT_LOG_FILE": envLogFile},
assertions: []traitAssertion{
func(tb testing.TB, stdout, stderr string, rc int) {
tb.Helper()
_, err := os.Stat(envLogFile)
assert.NoError(t, err)
},
},
cleanup: func() { assert.NoError(t, os.Remove(envLogFile)) },
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Cleanup(test.cleanup)

cmd, stdout, stderr := runSyft(t, test.env, test.args...)
for _, traitFn := range test.assertions {
traitFn(t, stdout, stderr, cmd.ProcessState.ExitCode())
}
if t.Failed() {
t.Log("STDOUT:\n", stdout)
t.Log("STDERR:\n", stderr)
t.Log("COMMAND:", strings.Join(cmd.Args, " "))
}
})
}
}

0 comments on commit ca081ae

Please sign in to comment.