mirror of
https://github.com/anchore/syft.git
synced 2025-11-18 17:03:17 +01:00
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:
parent
8f96adacfb
commit
ca081ae5e0
@ -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 //////////////////////////////////////////////
|
||||
|
||||
|
||||
@ -15,4 +15,5 @@ type logging struct {
|
||||
|
||||
func (cfg logging) loadDefaultValues(v *viper.Viper) {
|
||||
v.SetDefault("log.structured", false)
|
||||
v.SetDefault("log.file", "")
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
@ -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, " "))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user