Skip to content

Commit

Permalink
feat: introduce --silence-events flag on diff/sync commands
Browse files Browse the repository at this point in the history
This patch also lays out code in a way to avoid duplication across
commands. decK's commands share significant amount of flags and a code
pattern like this can minimize the amount of code and number of code
paths that exist with flag parsing.
  • Loading branch information
hbagdi committed May 24, 2021
1 parent 0fd5ce0 commit 58789ef
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ that will be created or updated or deleted.
return errors.New("A state file with Kong's configuration " +
"must be specified using -s/--state flag.")
}
return nil
return preRunSilenceEventsFlag()
},
}

Expand Down Expand Up @@ -61,4 +61,5 @@ func init() {
false, "return exit code 2 if there is a diff present,\n"+
"exit code 0 if no diff is found,\n"+
"and exit code 1 if an error occurs.")
addSilenceEventsFlag(diffCmd.Flags())
}
4 changes: 4 additions & 0 deletions cmd/konnect_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ that will be created or updated or deleted.` + konnectAlphaState,
return syncKonnect(cmd.Context(), konnectDiffCmdKongStateFile, true,
konnectDiffCmdParallelism)
},
PreRunE: func(cmd *cobra.Command, args []string) error {
return preRunSilenceEventsFlag()
},
}

func init() {
Expand All @@ -45,4 +48,5 @@ func init() {
false, "return exit code 2 if there is a diff present,\n"+
"exit code 0 if no diff is found,\n"+
"and exit code 1 if an error occurs.")
addSilenceEventsFlag(konnectDiffCmd.Flags())
}
4 changes: 4 additions & 0 deletions cmd/konnect_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ to get Konnect's state in sync with the input state.` + konnectAlphaState,
return syncKonnect(cmd.Context(), konnectDiffCmdKongStateFile, false,
konnectDiffCmdParallelism)
},
PreRunE: func(cmd *cobra.Command, args []string) error {
return preRunSilenceEventsFlag()
},
}

func init() {
Expand All @@ -33,4 +36,5 @@ func init() {
"with consumers")
konnectSyncCmd.Flags().IntVar(&konnectDiffCmdParallelism, "parallelism",
100, "Maximum number of concurrent operations")
addSilenceEventsFlag(konnectSyncCmd.Flags())
}
3 changes: 2 additions & 1 deletion cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ to get Kong's state in sync with the input state.`,
return errors.New("A state file with Kong's configuration " +
"must be specified using -s/--state flag.")
}
return nil
return preRunSilenceEventsFlag()
},
}

Expand Down Expand Up @@ -58,4 +58,5 @@ func init() {
0, "aritificial delay in seconds that is injected between insert operations \n"+
"for related entities (usually for cassandra deployments).\n"+
"See 'db_update_propagation' in kong.conf.")
addSilenceEventsFlag(syncCmd.Flags())
}
16 changes: 16 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cmd

import (
"github.com/fatih/color"
printPkg "github.com/kong/deck/print"
"github.com/kong/deck/solver"
"github.com/spf13/pflag"
)

func printStats(stats solver.Stats) {
Expand All @@ -14,3 +16,17 @@ func printStats(stats solver.Stats) {
printFn(" Updated: %v\n", stats.UpdateOps)
printFn(" Deleted: %v\n", stats.DeleteOps)
}

var (
silenceEvents bool
)

func preRunSilenceEventsFlag() error {
printPkg.DisableOutput = true
return nil
}

func addSilenceEventsFlag(set *pflag.FlagSet) {
set.BoolVar(&silenceEvents, "silence-events", false,
"disable printing events to stdout")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v0.0.7
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
Expand Down

0 comments on commit 58789ef

Please sign in to comment.