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: Add --fail-on-diff-error CLI flag to return non-zero exit code on diff error #3

Closed
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Flags:
--base string base commitish (default to origin/main)
--debug debug mode
--exclude string exclude regexp (default to none)
--fail-on-diff-error return non-zero status code if a diff error occurs
--git-path string path of a git binary (default to git)
-h, --help help for run
--include string include regexp (default to all)
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type rootFlags struct {
var RootCmd = &cobra.Command{
Use: "git-kustomize-diff",
Short: "git-kustomize-diff",
SilenceUsage: true,
SilenceErrors: false,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if cmd.Use == "version" {
Expand Down
10 changes: 10 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type runFlags struct {
gitPath string
debug bool
allowDirty bool
failOnDiffError bool
}

var runCmd = &cobra.Command{
Expand Down Expand Up @@ -80,6 +81,14 @@ var runCmd = &cobra.Command{

printRunResult(dir, opts, res)

if runOpts.failOnDiffError {
for _, v := range res.DiffMap.Results {
if _, ok := v.(*gitkustomizediff.DiffError); ok {
return fmt.Errorf("fail on diff error")
}
}
}

return nil
},
}
Expand All @@ -96,6 +105,7 @@ func init() {
runCmd.PersistentFlags().StringVar(&runOpts.gitPath, "git-path", "", "path of a git binary (default to git)")
runCmd.PersistentFlags().BoolVar(&runOpts.debug, "debug", false, "debug mode")
runCmd.PersistentFlags().BoolVar(&runOpts.allowDirty, "allow-dirty", false, "allow dirty tree")
runCmd.PersistentFlags().BoolVar(&runOpts.failOnDiffError, "fail-on-diff-error", false, "return non-zero status code if a diff error occurs")
}

func printRunResult(dirPath string, opts gitkustomizediff.RunOpts, res *gitkustomizediff.RunResult) {
Expand Down