Skip to content

Commit

Permalink
feat: show all ignores (#1220)
Browse files Browse the repository at this point in the history
* feat: add ignore show --all flag

* docs: update for new ignore show --all flag
  • Loading branch information
elsapet authored Aug 28, 2023
1 parent d88b1de commit e1d001a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
3 changes: 3 additions & 0 deletions docs/_data/bearer_ignore_show.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: ' ignore show'
synopsis: Show an ignored fingerprint
usage: ' ignore show <fingerprint> [flags]'
options:
- name: all
default_value: "false"
usage: Show all ignored fingerprints.
- name: bearer-ignore-file
default_value: bearer.ignore
usage: Load bearer.ignore file from the specified path.
Expand Down
34 changes: 22 additions & 12 deletions pkg/commands/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ Examples:

func newIgnoreShowCommand() *cobra.Command {
var IgnoreShowFlags = &flag.Flags{
IgnoreFlagGroup: flag.NewIgnoreFlagGroup(),
IgnoreFlagGroup: flag.NewIgnoreFlagGroup(),
IgnoreShowFlagGroup: flag.NewIgnoreShowFlagGroup(),
}
cmd := &cobra.Command{
Use: "show <fingerprint>",
Expand All @@ -73,15 +74,15 @@ $ bearer ignore show <fingerprint>`,
return fmt.Errorf("flag bind error: %w", err)
}

if len(args) == 0 {
return cmd.Help()
}

options, err := IgnoreShowFlags.ToOptions(args)
if err != nil {
return fmt.Errorf("flag error: %s", err)
}

if len(args) == 0 && !options.IgnoreShowOptions.All {
return cmd.Help()
}

ignoredFingerprints, fileExists, err := ignore.GetIgnoredFingerprints(options.IgnoreOptions.BearerIgnoreFile)
if err != nil {
cmd.Printf("Issue loading ignored fingerprints from bearer.ignore file: %s", err)
Expand All @@ -91,14 +92,23 @@ $ bearer ignore show <fingerprint>`,
cmd.Printf("bearer.ignore file not found. Perhaps you need to use --bearer-ignore-file to specify the path to bearer.ignore?\n")
return nil
}
fingerprintId := args[0]
selectedIgnoredFingerprint, ok := ignoredFingerprints[fingerprintId]
if !ok {
cmd.Printf("Ignored fingerprint '%s' was not found in bearer.ignore file\n", fingerprintId)
return nil
}

cmd.Print("\n")
cmd.Print(displayIgnoredEntryTextString(fingerprintId, selectedIgnoredFingerprint))
if options.IgnoreShowOptions.All {
// show all fingerprints
for fingerprintId, fingerprint := range ignoredFingerprints {
cmd.Print(displayIgnoredEntryTextString(fingerprintId, fingerprint))
}
} else {
// show a specific fingerprint
fingerprintId := args[0]
selectedIgnoredFingerprint, ok := ignoredFingerprints[fingerprintId]
if !ok {
cmd.Printf("Ignored fingerprint '%s' was not found in bearer.ignore file\n", fingerprintId)
return nil
}
cmd.Print(displayIgnoredEntryTextString(fingerprintId, selectedIgnoredFingerprint))
}
cmd.Print("\n")
return nil
},
Expand Down
40 changes: 40 additions & 0 deletions pkg/flag/ignore_show_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package flag

var (
AllFlag = Flag{
Name: "all",
ConfigName: "ignore_show.all",
Value: false,
Usage: "Show all ignored fingerprints.",
}
)

type IgnoreShowFlagGroup struct {
AllFlag *Flag
}

type IgnoreShowOptions struct {
All bool `mapstructure:"all" json:"all" yaml:"all"`
}

func NewIgnoreShowFlagGroup() *IgnoreShowFlagGroup {
return &IgnoreShowFlagGroup{
AllFlag: &AllFlag,
}
}

func (f *IgnoreShowFlagGroup) Name() string {
return "IgnoreShow"
}

func (f *IgnoreShowFlagGroup) Flags() []*Flag {
return []*Flag{
f.AllFlag,
}
}

func (f *IgnoreShowFlagGroup) ToOptions() IgnoreShowOptions {
return IgnoreShowOptions{
All: getBool(f.AllFlag),
}
}
9 changes: 9 additions & 0 deletions pkg/flag/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Flags struct {
GeneralFlagGroup *GeneralFlagGroup
IgnoreFlagGroup *IgnoreFlagGroup
IgnoreAddFlagGroup *IgnoreAddFlagGroup
IgnoreShowFlagGroup *IgnoreShowFlagGroup
IgnoreMigrateFlagGroup *IgnoreMigrateFlagGroup
}

Expand All @@ -67,6 +68,7 @@ type Options struct {
GeneralOptions
IgnoreOptions
IgnoreAddOptions
IgnoreShowOptions
IgnoreMigrateOptions
}

Expand Down Expand Up @@ -188,6 +190,9 @@ func (f *Flags) groups() []FlagGroup {
if f.IgnoreAddFlagGroup != nil {
groups = append(groups, f.IgnoreAddFlagGroup)
}
if f.IgnoreShowFlagGroup != nil {
groups = append(groups, f.IgnoreShowFlagGroup)
}
if f.IgnoreMigrateFlagGroup != nil {
groups = append(groups, f.IgnoreMigrateFlagGroup)
}
Expand Down Expand Up @@ -297,6 +302,10 @@ func (f *Flags) ToOptions(args []string) (Options, error) {
opts.IgnoreAddOptions = f.IgnoreAddFlagGroup.ToOptions()
}

if f.IgnoreShowFlagGroup != nil {
opts.IgnoreShowOptions = f.IgnoreShowFlagGroup.ToOptions()
}

if f.IgnoreMigrateFlagGroup != nil {
opts.IgnoreMigrateOptions = f.IgnoreMigrateFlagGroup.ToOptions()
}
Expand Down

0 comments on commit e1d001a

Please sign in to comment.