Skip to content

Commit

Permalink
chore(cmd,pkg): allow cli test to disable styling.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Apr 23, 2024
1 parent 8c6a73b commit 589ad27
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 90 deletions.
12 changes: 4 additions & 8 deletions cmd/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd

import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -319,9 +318,9 @@ func run(t *testing.T, test testCase) {
assert.NilError(t, err)
rootOpts, err := NewRootOptions()
assert.NilError(t, err)
var buf bytes.Buffer
configOpts.setOutput(&buf, true)
c := NewRootCmd(configOpts, rootOpts)
b := bytes.NewBufferString("")
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")
}
Expand All @@ -340,11 +339,8 @@ func run(t *testing.T, test testCase) {
assert.Error(t, err, test.expect.err)
}
}
out, err := io.ReadAll(b)
if err != nil {
t.Fatalf("error reading CLI output: %v", err)
}
res := stripansi.Strip(string(out))
out := buf.String()
res := stripansi.Strip(out)
assert.Equal(t, test.expect.out, res)
// Teardown
for k := range test.env {
Expand Down
16 changes: 7 additions & 9 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,23 @@ func NewCompletionCmd(_ *ConfigOptions, _ *RootOptions, _ *pflag.FlagSet) *cobra
Args: validateArgs(),
ValidArgs: cmdArgs,
DisableAutoGenTag: true,
Run: func(c *cobra.Command, args []string) {
RunE: func(c *cobra.Command, args []string) error {
if len(args) == 0 {
c.Help()
return
return c.Help()
}

arg := args[0]
switch arg {
case "bash":
c.Root().GenBashCompletion(os.Stdout)
break
return c.Root().GenBashCompletion(os.Stdout)
case "zsh":
c.Root().GenZshCompletion(os.Stdout)
break
return c.Root().GenZshCompletion(os.Stdout)
case "fish":
c.Root().GenFishCompletion(os.Stdout, true)
return c.Root().GenFishCompletion(os.Stdout, true)
case "help":
c.Help()
return c.Help()
}
return nil
},
}

Expand Down
28 changes: 17 additions & 11 deletions cmd/config_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,38 @@ 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"`
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
logLevel *options.LogLevel
writer io.Writer
logLevel *options.LogLevel
disableStyling bool
}

func (co *ConfigOptions) initPrinter() {
logLevel := co.logLevel.ToPtermLogLevel()
co.Printer = output.NewPrinter(logLevel, pterm.LogFormatterColorful, co.writer)
if co.disableStyling {
pterm.DisableColor()
}
co.Printer = output.NewPrinter(co.logLevel.ToPtermLogLevel(), pterm.LogFormatterColorful, co.writer)
}

func (co *ConfigOptions) SetOutput(writer io.Writer) {
// Called by tests to disable styling and set bytes buffer as output
func (co *ConfigOptions) setOutput(writer io.Writer, disableStyling bool) {
co.writer = writer
co.disableStyling = disableStyling
co.initPrinter()
}

// NewConfigOptions creates an instance of ConfigOptions.
func NewConfigOptions() (*ConfigOptions, error) {
o := &ConfigOptions{
writer: os.Stdout,
logLevel: options.NewLogLevel(),
writer: os.Stdout,
logLevel: options.NewLogLevel(),
disableStyling: false,
}
o.initPrinter()
if err := defaults.Set(o); err != nil {
Expand Down Expand Up @@ -92,8 +98,8 @@ func (co *ConfigOptions) validate() []error {
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.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.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")
}

Expand Down
28 changes: 16 additions & 12 deletions cmd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd
import (
"bytes"
"github.com/falcosecurity/driverkit/pkg/driverbuilder"
"github.com/falcosecurity/driverkit/pkg/driverbuilder/builder"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand All @@ -30,21 +31,24 @@ func NewDockerCmd(configOpts *ConfigOptions, rootOpts *RootOptions, rootFlags *p
configOpts.Printer.Logger.Info("starting build",
configOpts.Printer.Logger.Args("processor", c.Name()))
if !configOpts.dryRun {
if !rootOpts.Output.HasOutputs() {
configOpts.Printer.Logger.Info("no output specified")
return nil
}
// 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
b := rootOpts.ToBuild(configOpts.Printer.WithWriter(&buf))
defer func() {
configOpts.Printer.DefaultText.Print(buf.String())
}()
if !b.HasOutputs() {
return nil
var b *builder.Build
if configOpts.disableStyling {
b = rootOpts.ToBuild(configOpts.Printer)
} else {
var buf bytes.Buffer
b = rootOpts.ToBuild(configOpts.Printer.WithWriter(&buf))
configOpts.Printer.Spinner, _ = configOpts.Printer.Spinner.Start("driver building, it will take a few seconds")
defer func() {
configOpts.Printer.DefaultText.Print(buf.String())
}()
}
configOpts.Printer.Spinner, _ = configOpts.Printer.Spinner.Start("driver building, it will take a few seconds")
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
},
Expand Down
23 changes: 17 additions & 6 deletions cmd/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd

import (
"bytes"
"github.com/falcosecurity/driverkit/pkg/driverbuilder/builder"
"os"

"github.com/olekukonko/tablewriter"
Expand All @@ -28,17 +29,26 @@ func NewImagesCmd(configOpts *ConfigOptions, rootOpts *RootOptions, rootFlags *p
imagesCmd := &cobra.Command{
Use: "images",
Short: "List builder images",
Run: func(c *cobra.Command, args []string) {
RunE: func(c *cobra.Command, args []string) error {
configOpts.Printer.Logger.Info("starting loading images",
configOpts.Printer.Logger.Args("processor", c.Name()))
// 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
b := rootOpts.ToBuild(configOpts.Printer.WithWriter(&buf))
configOpts.Printer.Spinner, _ = configOpts.Printer.Spinner.Start("listing images, it will take a few seconds")
var (
buf bytes.Buffer
b *builder.Build
)
if configOpts.disableStyling {
b = rootOpts.ToBuild(configOpts.Printer)
} else {
b = rootOpts.ToBuild(configOpts.Printer.WithWriter(&buf))
configOpts.Printer.Spinner, _ = configOpts.Printer.Spinner.Start("listing images, it will take a few seconds")
}
b.LoadImages()
_ = configOpts.Printer.Spinner.Stop()
configOpts.Printer.DefaultText.Print(buf.String())
if !configOpts.disableStyling {
_ = configOpts.Printer.Spinner.Stop()
configOpts.Printer.DefaultText.Print(buf.String())
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Image", "Target", "Arch", "GCC"})
Expand All @@ -54,6 +64,7 @@ func NewImagesCmd(configOpts *ConfigOptions, rootOpts *RootOptions, rootFlags *p
table.Append(data)
}
table.Render() // Send output
return nil
},
}
// Add root flags
Expand Down
29 changes: 16 additions & 13 deletions cmd/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,23 @@ func NewKubernetesCmd(configOpts *ConfigOptions, rootOpts *RootOptions, rootFlag
configOpts.Printer.Logger.Info("starting build",
configOpts.Printer.Logger.Args("processor", c.Name()))
if !configOpts.dryRun {
if !rootOpts.Output.HasOutputs() {
configOpts.Printer.Logger.Info("no output specified")
return nil
}
// 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
b := rootOpts.ToBuild(configOpts.Printer.WithWriter(&buf))
defer func() {
configOpts.Printer.DefaultText.Print(buf.String())
}()
if !b.HasOutputs() {
return nil
var b *builder.Build
if configOpts.disableStyling {
b = rootOpts.ToBuild(configOpts.Printer)
} else {
var buf bytes.Buffer
b = rootOpts.ToBuild(configOpts.Printer.WithWriter(&buf))
configOpts.Printer.Spinner, _ = configOpts.Printer.Spinner.Start("driver building, it will take a few seconds")
defer func() {
configOpts.Printer.DefaultText.Print(buf.String())
}()
}
configOpts.Printer.Spinner, _ = configOpts.Printer.Spinner.Start("driver building, it will take a few seconds")
defer func() {
_ = configOpts.Printer.Spinner.Stop()
}()
return kubernetesRun(kubefactory, b, configOpts)
}
return nil
Expand Down Expand Up @@ -104,7 +107,7 @@ func kubernetesRun(kubefactory factory.Factory,
kubernetesOptions.RunAsUser,
kubernetesOptions.Namespace,
kubernetesOptions.ImagePullSecret,
configOpts.timeout,
configOpts.proxyURL)
configOpts.Timeout,
configOpts.ProxyURL)
return buildProcessor.Start(b)
}
29 changes: 16 additions & 13 deletions cmd/kubernetes_in_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,23 @@ func NewKubernetesInClusterCmd(configOpts *ConfigOptions, rootOpts *RootOptions,
configOpts.Printer.Logger.Info("starting build",
configOpts.Printer.Logger.Args("processor", c.Name()))
if !configOpts.dryRun {
if !rootOpts.Output.HasOutputs() {
configOpts.Printer.Logger.Info("no output specified")
return nil
}
// 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
b := rootOpts.ToBuild(configOpts.Printer.WithWriter(&buf))
defer func() {
configOpts.Printer.DefaultText.Print(buf.String())
}()
if !b.HasOutputs() {
return nil
var b *builder.Build
if configOpts.disableStyling {
b = rootOpts.ToBuild(configOpts.Printer)
} else {
var buf bytes.Buffer
b = rootOpts.ToBuild(configOpts.Printer.WithWriter(&buf))
configOpts.Printer.Spinner, _ = configOpts.Printer.Spinner.Start("driver building, it will take a few seconds")
defer func() {
configOpts.Printer.DefaultText.Print(buf.String())
}()
}
configOpts.Printer.Spinner, _ = configOpts.Printer.Spinner.Start("driver building, it will take a few seconds")
defer func() {
_ = configOpts.Printer.Spinner.Stop()
}()
return kubernetesInClusterRun(b, configOpts)
}
return nil
Expand Down Expand Up @@ -85,7 +88,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)
}
28 changes: 16 additions & 12 deletions cmd/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"github.com/falcosecurity/driverkit/pkg/driverbuilder"
"github.com/falcosecurity/driverkit/pkg/driverbuilder/builder"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand All @@ -24,25 +25,28 @@ func NewLocalCmd(configOpts *ConfigOptions, rootOpts *RootOptions, rootFlags *pf
configOpts.Printer.Logger.Info("starting build",
configOpts.Printer.Logger.Args("processor", c.Name()))
if !configOpts.dryRun {
if !rootOpts.Output.HasOutputs() {
configOpts.Printer.Logger.Info("no output specified")
return nil
}
// 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
b := rootOpts.ToBuild(configOpts.Printer.WithWriter(&buf))
defer func() {
configOpts.Printer.DefaultText.Print(buf.String())
}()
if !b.HasOutputs() {
return nil
var b *builder.Build
if configOpts.disableStyling {
b = rootOpts.ToBuild(configOpts.Printer)
} else {
var buf bytes.Buffer
b = rootOpts.ToBuild(configOpts.Printer.WithWriter(&buf))
configOpts.Printer.Spinner, _ = configOpts.Printer.Spinner.Start("driver building, it will take a few seconds")
defer func() {
configOpts.Printer.DefaultText.Print(buf.String())
}()
}
configOpts.Printer.Spinner, _ = configOpts.Printer.Spinner.Start("driver building, it will take a few seconds")
defer func() {
_ = configOpts.Printer.Spinner.Stop()
}()
return driverbuilder.NewLocalBuildProcessor(opts.useDKMS,
opts.downloadHeaders,
opts.srcDir,
opts.envMap,
configOpts.timeout).Start(b)
configOpts.Timeout).Start(b)
}
return nil
},
Expand Down
12 changes: 10 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,14 @@ func NewRootCmd(configOpts *ConfigOptions, rootOpts *RootOptions) *RootCmd {
Args: cobra.OnlyValidArgs,
DisableFlagsInUseLine: true,
DisableAutoGenTag: true,
SilenceUsage: true,
Version: version.String(),
Run: func(c *cobra.Command, args []string) {
RunE: func(c *cobra.Command, args []string) error {
if len(args) == 0 {
configOpts.Printer.Logger.Info("specify a valid processor", configOpts.Printer.Logger.Args("processors", validProcessors))
}
// Fallback to help
_ = c.Help()
return c.Help()
},
}
ret := &RootCmd{
Expand Down Expand Up @@ -210,6 +211,13 @@ func Start() {
configOpts.Printer.Logger.Fatal("error setting driverkit root options defaults",
configOpts.Printer.Logger.Args("err", err.Error()))
}

// Cleanup spinner upon leaving if any
defer func() {
if configOpts.Printer.Spinner != nil {
_ = configOpts.Printer.Spinner.Stop()
}
}()
root := NewRootCmd(configOpts, rootOpts)
if err = root.Execute(); err != nil {
configOpts.Printer.Logger.Fatal("error executing driverkit", configOpts.Printer.Logger.Args("err", err.Error()))
Expand Down
Loading

0 comments on commit 589ad27

Please sign in to comment.