diff --git a/cmd/cli_test.go b/cmd/cli_test.go index b639d06d..9b734c98 100644 --- a/cmd/cli_test.go +++ b/cmd/cli_test.go @@ -315,9 +315,13 @@ var tests = []testCase{ func run(t *testing.T, test testCase) { // Setup - c := NewRootCmd() + configOpts, err := NewConfigOptions() + assert.NilError(t, err) + rootOpts, err := NewRootOptions() + assert.NilError(t, err) + c := NewRootCmd(configOpts, rootOpts) b := bytes.NewBufferString("") - c.SetOutput(b) + configOpts.SetOutput(b) if len(test.args) == 0 || (test.args[0] != "__complete" && test.args[0] != "__completeNoDesc" && test.args[0] != "help" && test.args[0] != "completion") { test.args = append(test.args, "--dryrun") } @@ -328,7 +332,7 @@ func run(t *testing.T, test testCase) { } } // Test - err := c.Execute() + err = c.Execute() if err != nil { if test.expect.err == "" { t.Fatalf("error executing CLI: %v", err) diff --git a/cmd/config_options.go b/cmd/config_options.go index 76affe18..6173c2d6 100644 --- a/cmd/config_options.go +++ b/cmd/config_options.go @@ -37,27 +37,32 @@ var aliasProcessors = []string{"docker", "k8s", "k8s-ic"} // ConfigOptions represent the persistent configuration flags of driverkit. type ConfigOptions struct { - ConfigFile string - Timeout int `validate:"number,min=30" default:"120" name:"timeout"` - ProxyURL string `validate:"omitempty,proxy" name:"proxy url"` - DryRun bool + configFile string + timeout int `validate:"number,min=30" default:"120" name:"timeout"` + proxyURL string `validate:"omitempty,proxy" name:"proxy url"` + dryRun bool // Printer used by all commands to output messages. Printer *output.Printer - // Writer is used to write the output of the printer. - Writer io.Writer + // writer is used to write the output of the printer. + writer io.Writer logLevel *options.LogLevel } func (co *ConfigOptions) initPrinter() { logLevel := co.logLevel.ToPtermLogLevel() - co.Printer = output.NewPrinter(logLevel, pterm.LogFormatterColorful, co.Writer) + co.Printer = output.NewPrinter(logLevel, pterm.LogFormatterColorful, co.writer) +} + +func (co *ConfigOptions) SetOutput(writer io.Writer) { + co.writer = writer + co.initPrinter() } // NewConfigOptions creates an instance of ConfigOptions. func NewConfigOptions() (*ConfigOptions, error) { o := &ConfigOptions{ - Writer: os.Stdout, + writer: os.Stdout, logLevel: options.NewLogLevel(), } o.initPrinter() @@ -85,11 +90,11 @@ func (co *ConfigOptions) validate() []error { // AddFlags registers the common flags. func (co *ConfigOptions) AddFlags(flags *pflag.FlagSet) { - flags.StringVarP(&co.ConfigFile, "config", "c", co.ConfigFile, "config file path (default $HOME/.driverkit.yaml if exists)") + flags.StringVarP(&co.configFile, "config", "c", co.configFile, "config file path (default $HOME/.driverkit.yaml if exists)") flags.VarP(co.logLevel, "loglevel", "l", "Set level for logs "+co.logLevel.Allowed()) - flags.IntVar(&co.Timeout, "timeout", co.Timeout, "timeout in seconds") - flags.StringVar(&co.ProxyURL, "proxy", co.ProxyURL, "the proxy to use to download data") - flags.BoolVar(&co.DryRun, "dryrun", co.DryRun, "do not actually perform the action") + flags.IntVar(&co.timeout, "timeout", co.timeout, "timeout in seconds") + flags.StringVar(&co.proxyURL, "proxy", co.proxyURL, "the proxy to use to download data") + flags.BoolVar(&co.dryRun, "dryrun", co.dryRun, "do not actually perform the action") } // Init reads in config file and ENV variables if set. @@ -102,8 +107,8 @@ func (co *ConfigOptions) Init() bool { } configErr = true } - if co.ConfigFile != "" { - viper.SetConfigFile(co.ConfigFile) + if co.configFile != "" { + viper.SetConfigFile(co.configFile) } else { // Find home directory. home, err := homedir.Dir() diff --git a/cmd/docker.go b/cmd/docker.go index ac737a13..9a50cd20 100644 --- a/cmd/docker.go +++ b/cmd/docker.go @@ -29,7 +29,7 @@ func NewDockerCmd(configOpts *ConfigOptions, rootOpts *RootOptions, rootFlags *p RunE: func(c *cobra.Command, args []string) error { configOpts.Printer.Logger.Info("starting build", configOpts.Printer.Logger.Args("processor", c.Name())) - if !configOpts.DryRun { + if !configOpts.dryRun { // Since we use a spinner, cache log data to a bytesbuffer; // we will later print it once we stop the spinner. var buf bytes.Buffer @@ -44,7 +44,7 @@ func NewDockerCmd(configOpts *ConfigOptions, rootOpts *RootOptions, rootFlags *p defer func() { _ = configOpts.Printer.Spinner.Stop() }() - return driverbuilder.NewDockerBuildProcessor(configOpts.Timeout, configOpts.ProxyURL).Start(b) + return driverbuilder.NewDockerBuildProcessor(configOpts.timeout, configOpts.proxyURL).Start(b) } return nil }, diff --git a/cmd/kubernetes.go b/cmd/kubernetes.go index 53d1f7d1..69bf2295 100644 --- a/cmd/kubernetes.go +++ b/cmd/kubernetes.go @@ -60,7 +60,7 @@ func NewKubernetesCmd(configOpts *ConfigOptions, rootOpts *RootOptions, rootFlag kubernetesCmd.RunE = func(c *cobra.Command, args []string) error { configOpts.Printer.Logger.Info("starting build", configOpts.Printer.Logger.Args("processor", c.Name())) - if !configOpts.DryRun { + if !configOpts.dryRun { // Since we use a spinner, cache log data to a bytesbuffer; // we will later print it once we stop the spinner. var buf bytes.Buffer @@ -104,7 +104,7 @@ func kubernetesRun(kubefactory factory.Factory, kubernetesOptions.RunAsUser, kubernetesOptions.Namespace, kubernetesOptions.ImagePullSecret, - configOpts.Timeout, - configOpts.ProxyURL) + configOpts.timeout, + configOpts.proxyURL) return buildProcessor.Start(b) } diff --git a/cmd/kubernetes_in_cluster.go b/cmd/kubernetes_in_cluster.go index d1d57310..310dc677 100644 --- a/cmd/kubernetes_in_cluster.go +++ b/cmd/kubernetes_in_cluster.go @@ -43,7 +43,7 @@ func NewKubernetesInClusterCmd(configOpts *ConfigOptions, rootOpts *RootOptions, kubernetesInClusterCmd.RunE = func(c *cobra.Command, args []string) error { configOpts.Printer.Logger.Info("starting build", configOpts.Printer.Logger.Args("processor", c.Name())) - if !configOpts.DryRun { + if !configOpts.dryRun { // Since we use a spinner, cache log data to a bytesbuffer; // we will later print it once we stop the spinner. var buf bytes.Buffer @@ -85,7 +85,7 @@ func kubernetesInClusterRun(b *builder.Build, configOpts *ConfigOptions) error { kubernetesOptions.RunAsUser, kubernetesOptions.Namespace, kubernetesOptions.ImagePullSecret, - configOpts.Timeout, - configOpts.ProxyURL) + configOpts.timeout, + configOpts.proxyURL) return buildProcessor.Start(b) } diff --git a/cmd/local.go b/cmd/local.go index 1d48e215..b16fbb72 100644 --- a/cmd/local.go +++ b/cmd/local.go @@ -23,7 +23,7 @@ func NewLocalCmd(configOpts *ConfigOptions, rootOpts *RootOptions, rootFlags *pf RunE: func(c *cobra.Command, args []string) error { configOpts.Printer.Logger.Info("starting build", configOpts.Printer.Logger.Args("processor", c.Name())) - if !configOpts.DryRun { + if !configOpts.dryRun { // Since we use a spinner, cache log data to a bytesbuffer; // we will later print it once we stop the spinner. var buf bytes.Buffer @@ -42,7 +42,7 @@ func NewLocalCmd(configOpts *ConfigOptions, rootOpts *RootOptions, rootFlags *pf opts.downloadHeaders, opts.srcDir, opts.envMap, - configOpts.Timeout).Start(b) + configOpts.timeout).Start(b) } return nil }, diff --git a/pkg/driverbuilder/builder/image_test.go b/pkg/driverbuilder/builder/image_test.go index a1d848e4..be3d7b5e 100644 --- a/pkg/driverbuilder/builder/image_test.go +++ b/pkg/driverbuilder/builder/image_test.go @@ -15,6 +15,8 @@ limitations under the License. package builder import ( + "github.com/falcosecurity/falcoctl/pkg/output" + "github.com/pterm/pterm" "io" "net/http" "os" @@ -236,6 +238,8 @@ images: } func TestFileImagesLister(t *testing.T) { + printer := output.NewPrinter(pterm.LogLevelInfo, pterm.LogFormatterColorful, os.Stdout) + // setup images file f, err := os.CreateTemp(t.TempDir(), "imagetest") if err != nil { @@ -269,11 +273,13 @@ func TestFileImagesLister(t *testing.T) { t.Fatal(err) } - assert.DeepEqual(t, test.expected, lister.LoadImages()) + assert.DeepEqual(t, test.expected, lister.LoadImages(printer)) } } func TestRepoImagesLister(t *testing.T) { + printer := output.NewPrinter(pterm.LogLevelInfo, pterm.LogFormatterColorful, os.Stdout) + mock, err := registry.NewMock(t) assert.NilError(t, err) defer mock.Close() @@ -300,6 +306,6 @@ func TestRepoImagesLister(t *testing.T) { mock.RegisterHandler("/v2/foo/test/tags/list", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(test.jsonData)) }) - assert.DeepEqual(t, test.expected, lister.LoadImages()) + assert.DeepEqual(t, test.expected, lister.LoadImages(printer)) } }