Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/silence print #393

Merged
merged 2 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"

"github.com/blang/semver/v4"
"github.com/fatih/color"
"github.com/kong/deck/diff"
"github.com/kong/deck/dump"
"github.com/kong/deck/file"
Expand Down Expand Up @@ -164,11 +163,8 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
s, _ := diff.NewSyncer(currentState, targetState)
s.StageDelaySec = delay
stats, errs := solver.Solve(ctx, s, wsClient, nil, parallelism, dry)
printFn := color.New(color.FgGreen, color.Bold).PrintfFunc()
printFn("Summary:\n")
printFn(" Created: %v\n", stats.CreateOps.Count())
printFn(" Updated: %v\n", stats.UpdateOps.Count())
printFn(" Deleted: %v\n", stats.DeleteOps.Count())
// print stats before error to report completed operations
printStats(stats)
if errs != nil {
return utils.ErrArray{Errors: errs}
}
Expand Down
8 changes: 2 additions & 6 deletions cmd/common_konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/kong/deck/diff"
"github.com/kong/deck/dump"
"github.com/kong/deck/file"
Expand Down Expand Up @@ -88,11 +87,8 @@ func syncKonnect(ctx context.Context,

s, _ := diff.NewSyncer(currentState, targetState)
stats, errs := solver.Solve(ctx, s, kongClient, konnectClient, parallelism, dry)
printFn := color.New(color.FgGreen, color.Bold).PrintfFunc()
printFn("Summary:\n")
printFn(" Created: %v\n", stats.CreateOps.Count())
printFn(" Updated: %v\n", stats.UpdateOps.Count())
printFn(" Deleted: %v\n", stats.DeleteOps.Count())
// print stats before error to report completed operations
printStats(stats)
if errs != nil {
return utils.ErrArray{Errors: errs}
}
Expand Down
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())
}
32 changes: 32 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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) {
// do not use github.com/kong/deck/print because that package
// is used only for events logs
printFn := color.New(color.FgGreen, color.Bold).PrintfFunc()
printFn("Summary:\n")
printFn(" Created: %v\n", stats.CreateOps)
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