use SYFT_LOG_FILE env var (#805)

* 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>
This commit is contained in:
Jonas Xavier 2022-02-09 10:04:08 -08:00 committed by GitHub
parent 8f96adacfb
commit ca081ae5e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 0 deletions

View File

@ -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 { func bindPackagesConfigOptions(flags *pflag.FlagSet) error {
// Formatting & Input options ////////////////////////////////////////////// // Formatting & Input options //////////////////////////////////////////////

View File

@ -15,4 +15,5 @@ type logging struct {
func (cfg logging) loadDefaultValues(v *viper.Viper) { func (cfg logging) loadDefaultValues(v *viper.Viper) {
v.SetDefault("log.structured", false) v.SetDefault("log.structured", false)
v.SetDefault("log.file", "")
} }

View File

@ -1,10 +1,13 @@
package cli package cli
import ( import (
"os"
"path/filepath"
"strings" "strings"
"testing" "testing"
"github.com/sergi/go-diff/diffmatchpatch" "github.com/sergi/go-diff/diffmatchpatch"
"github.com/stretchr/testify/assert"
) )
func TestRootCmdAliasesToPackagesSubcommand(t *testing.T) { func TestRootCmdAliasesToPackagesSubcommand(t *testing.T) {
@ -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, " "))
}
})
}
}