From 397cbb10d61a6623e30358934e0ababa783c4575 Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Thu, 14 Nov 2024 10:30:44 +0100 Subject: [PATCH] =?UTF-8?q?Revert=20"=F0=9F=90=9B=20Load=20provider=20flag?= =?UTF-8?q?s=20from=20environment=20variables=20(#4847)"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 0b4c64129cfbe532a481eefef5c1907230553907. --- cli/providers/providers.go | 31 +++++++++++++++++-------- test/providers/os_test.go | 47 +++----------------------------------- 2 files changed, 25 insertions(+), 53 deletions(-) diff --git a/cli/providers/providers.go b/cli/providers/providers.go index fb32d2000f..5e93066ac6 100644 --- a/cli/providers/providers.go +++ b/cli/providers/providers.go @@ -5,6 +5,8 @@ package providers import ( "encoding/json" + "go.mondoo.com/cnquery/v11/utils/piped" + "go.mondoo.com/ranger-rpc/status" "os" "strings" @@ -19,8 +21,6 @@ import ( "go.mondoo.com/cnquery/v11/providers-sdk/v1/plugin" "go.mondoo.com/cnquery/v11/providers-sdk/v1/recording" "go.mondoo.com/cnquery/v11/types" - "go.mondoo.com/cnquery/v11/utils/piped" - "go.mondoo.com/ranger-rpc/status" ) type Command struct { @@ -318,22 +318,35 @@ func attachFlags(flagset *pflag.FlagSet, flags []plugin.Flag) { } } -func getFlagValue(flag plugin.Flag) *llx.Primitive { +func getFlagValue(flag plugin.Flag, cmd *cobra.Command) *llx.Primitive { switch flag.Type { case plugin.FlagType_Bool: - return llx.BoolPrimitive(viper.GetBool(flag.Long)) + v, err := cmd.Flags().GetBool(flag.Long) + if err == nil { + return llx.BoolPrimitive(v) + } + log.Warn().Err(err).Msg("failed to get flag " + flag.Long) case plugin.FlagType_Int: - return llx.IntPrimitive(viper.GetInt64(flag.Long)) + if v, err := cmd.Flags().GetInt(flag.Long); err == nil { + return llx.IntPrimitive(int64(v)) + } case plugin.FlagType_String: - return llx.StringPrimitive(viper.GetString(flag.Long)) + if v, err := cmd.Flags().GetString(flag.Long); err == nil { + return llx.StringPrimitive(v) + } case plugin.FlagType_List: - return llx.ArrayPrimitiveT(viper.GetStringSlice(flag.Long), llx.StringPrimitive, types.String) + if v, err := cmd.Flags().GetStringSlice(flag.Long); err == nil { + return llx.ArrayPrimitiveT(v, llx.StringPrimitive, types.String) + } case plugin.FlagType_KeyValue: - return llx.MapPrimitiveT(viper.GetStringMapString(flag.Long), llx.StringPrimitive, types.String) + if v, err := cmd.Flags().GetStringToString(flag.Long); err == nil { + return llx.MapPrimitiveT(v, llx.StringPrimitive, types.String) + } default: log.Warn().Msg("unknown flag type for " + flag.Long) return nil } + return nil } func setConnector(provider *plugin.Provider, connector *plugin.Connector, run func(*cobra.Command, *providers.Runtime, *plugin.ParseCLIRes), cmd *cobra.Command) { @@ -408,7 +421,7 @@ func setConnector(provider *plugin.Provider, connector *plugin.Connector, run fu continue } - if v := getFlagValue(flag); v != nil { + if v := getFlagValue(flag, cmd); v != nil { flagVals[flag.Long] = v } } diff --git a/test/providers/os_test.go b/test/providers/os_test.go index 05d69b1722..cc029498c6 100644 --- a/test/providers/os_test.go +++ b/test/providers/os_test.go @@ -4,16 +4,15 @@ package providers import ( + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.mondoo.com/cnquery/v11/test" "log" "os" "os/exec" "path/filepath" "sync" "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "go.mondoo.com/cnquery/v11/test" ) var once sync.Once @@ -186,43 +185,3 @@ func TestOsProviderSharedTests(t *testing.T) { } } } - -func TestProvidersEnvVarsLoading(t *testing.T) { - t.Run("command WITHOUT path should not find any package", func(t *testing.T) { - r := test.NewCliTestRunner("./cnquery", "run", "fs", "-c", mqlPackagesQuery, "-j") - err := r.Run() - require.NoError(t, err) - assert.Equal(t, 0, r.ExitCode()) - assert.NotNil(t, r.Stdout()) - assert.NotNil(t, r.Stderr()) - - var c mqlPackages - err = r.Json(&c) - assert.NoError(t, err) - - // No packages - assert.Empty(t, c) - }) - t.Run("command WITH path should find packages", func(t *testing.T) { - os.Setenv("MONDOO_PATH", "./testdata/fs") - defer os.Unsetenv("MONDOO_PATH") - // Note we are not passing the flag "--path ./testdata/fs" - r := test.NewCliTestRunner("./cnquery", "run", "fs", "-c", mqlPackagesQuery, "-j") - err := r.Run() - require.NoError(t, err) - assert.Equal(t, 0, r.ExitCode()) - assert.NotNil(t, r.Stdout()) - assert.NotNil(t, r.Stderr()) - - var c mqlPackages - err = r.Json(&c) - assert.NoError(t, err) - - // Should have packages - if assert.NotEmpty(t, c) { - x := c[0] - assert.NotNil(t, x.Packages) - assert.True(t, len(x.Packages) > 0) - } - }) -}