Skip to content

Commit

Permalink
log: add silent mode
Browse files Browse the repository at this point in the history
  • Loading branch information
k3rn31 committed Aug 3, 2022
1 parent 2f8ac76 commit 5835320
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 5 deletions.
6 changes: 6 additions & 0 deletions cmd/gremlins.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/spf13/cobra"

"github.com/go-gremlins/gremlins/cmd/internal/flags"
"github.com/go-gremlins/gremlins/configuration"
"github.com/go-gremlins/gremlins/pkg/log"
)
Expand Down Expand Up @@ -80,6 +81,11 @@ and friends.
}
cmd.AddCommand(uc.cmd)

flag := flags.Flag{Name: "silent", CfgKey: configuration.GremlinsSilentKey, Shorthand: "s", DefaultV: false, Usage: "suppress output and run in silent mode"}
if err := flags.SetPersistent(cmd, flag); err != nil {
return nil, err
}

return &gremlinsCmd{
cmd: cmd,
}, nil
Expand Down
19 changes: 14 additions & 5 deletions cmd/internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,26 @@ type Flag struct {
// Set is a "generic" function used to set flags on cobra.Command and bind
// them to viper.Viper.
func Set(cmd *cobra.Command, flag Flag) error {
flags := cmd.Flags()
flagSet := cmd.Flags()
return setFlags(flag, flagSet)
}

func SetPersistent(cmd *cobra.Command, flag Flag) error {
flagSet := cmd.PersistentFlags()
return setFlags(flag, flagSet)
}

func setFlags(flag Flag, fs *pflag.FlagSet) error {
switch dv := flag.DefaultV.(type) {
// TODO: add a case for all the supported types
case bool:
setBool(flag, flags, dv)
setBool(flag, fs, dv)
case string:
setString(flag, flags, dv)
setString(flag, fs, dv)
case float64:
setFloat64(flag, flags, dv)
setFloat64(flag, fs, dv)
}
err := viper.BindPFlag(flag.CfgKey, flags.Lookup(flag.Name))
err := viper.BindPFlag(flag.CfgKey, fs.Lookup(flag.Name))
if err != nil {
return err
}
Expand Down
17 changes: 17 additions & 0 deletions cmd/internal/flags/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ func TestSet(t *testing.T) {
if (tc.expectError && err == nil) || (!tc.expectError && err != nil) {
t.Fatal("error not expected")
}
if !tc.expectError {
if cmd.Flags().Lookup(tc.flag.Name) == nil {
t.Errorf("expected flag to be present")
}
}

tc.flag.Name = tc.flag.Name + "_pers"
err = SetPersistent(cmd, tc.flag)
if (tc.expectError && err == nil) || (!tc.expectError && err != nil) {
t.Fatal("error not expected")
}
if !tc.expectError {
if cmd.Flag(tc.flag.Name) == nil {
t.Errorf("expected flag to be present")
}
}

})
}
}
1 change: 1 addition & 0 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

// This is the list of the keys available in config files and ad flags.
const (
GremlinsSilentKey = "silent"
UnleashDryRunKey = "unleash.dry-run"
UnleashTagsKey = "unleash.tags"
UnleashThresholdEfficacyKey = "unleash.threshold.efficacy"
Expand Down
15 changes: 15 additions & 0 deletions docs/docs/usage/commands/gremlins.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,18 @@ Overrides the configuration file.
```shell
gremlins <command> --config=config.yml
```

### Silent

:material-flag:`--silent`/`-s` · :material-sign-direction: Default: false

Makes Gremlins work in _silent mode_, which means only errors will be reported on STDOUT. This is useful in CI runs
when you don't want to clutter the log, but just read the results from a file or check the exit error code in
combination with a threshold configuration.

!!! warning
Note that Gremlins will be completely silent if there aren't errors, it doesn't mean it is unresponsive.

```shell
gremlins <command> --silent
```
1 change: 1 addition & 0 deletions docs/docs/usage/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ gremlins unleash --config=myConfig.yaml
Here is a complete configuration file with all the properties set to their defaults:

```yaml
silent: false
unleash:
dry-run: false
tags: ""
Expand Down
7 changes: 7 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"sync"

"github.com/fatih/color"
"github.com/spf13/viper"
)

var fgRed = color.New(color.FgRed).SprintFunc()
Expand Down Expand Up @@ -93,10 +94,16 @@ type log struct {
}

func (l *log) writef(f string, args ...any) {
if viper.GetBool("silent") {
return
}
_, _ = fmt.Fprintf(l.out, f, args...)
}

func (l *log) writeln(a any) {
if viper.GetBool("silent") {
return
}
_, _ = fmt.Fprintln(l.out, a)
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"bytes"
"testing"

"github.com/spf13/viper"

"github.com/go-gremlins/gremlins/pkg/log"
)

Expand Down Expand Up @@ -114,3 +116,28 @@ func TestLogError(t *testing.T) {
}
})
}

func TestSilentMode(t *testing.T) {
viper.Set("silent", true)
defer viper.Reset()

sOut := &bytes.Buffer{}
defer sOut.Reset()
eOut := &bytes.Buffer{}
defer eOut.Reset()
log.Init(sOut, eOut)
defer log.Reset()

log.Infof("%s", "test")
log.Infoln("test")
log.Errorf("%s\n", "test")
log.Errorln("test")

if sOut.String() != "" {
t.Errorf("expected empty string")
}
if eOut.String() != "ERROR: test\nERROR: test\n" {
t.Log(eOut.String())
t.Errorf("expected errors to be reported")
}
}

0 comments on commit 5835320

Please sign in to comment.