@@ -3,11 +3,13 @@ package cmd
33
44import (
55 "context"
6+ "errors"
67 "io"
78 "os"
89 "path/filepath"
910
1011 "github.com/redhat-openshift-ecosystem/openshift-preflight/artifacts"
12+ "github.com/redhat-openshift-ecosystem/openshift-preflight/internal/cli"
1113 "github.com/redhat-openshift-ecosystem/openshift-preflight/internal/runtime"
1214 "github.com/redhat-openshift-ecosystem/openshift-preflight/internal/viper"
1315 "github.com/redhat-openshift-ecosystem/openshift-preflight/version"
@@ -45,6 +47,9 @@ func rootCmd() *cobra.Command {
4547 rootCmd .PersistentFlags ().String ("loglevel" , "" , "The verbosity of the preflight tool itself. Ex. warn, debug, trace, info, error. (env: PFLT_LOGLEVEL)" )
4648 _ = viper .BindPFlag ("loglevel" , rootCmd .PersistentFlags ().Lookup ("loglevel" ))
4749
50+ rootCmd .PersistentFlags ().Bool ("exit-with-failure" , false , "Exit with exit code 2 if any checks encounter an error or exit code 1 if any checks fail. (env: PFLT_EXIT_WITH_FAILURE)" )
51+ _ = viper .BindPFlag ("exit_with_failure" , rootCmd .PersistentFlags ().Lookup ("exit-with-failure" ))
52+
4853 rootCmd .AddCommand (checkCmd ())
4954 rootCmd .AddCommand (listChecksCmd ())
5055 rootCmd .AddCommand (runtimeAssetsCmd ())
@@ -53,8 +58,37 @@ func rootCmd() *cobra.Command {
5358 return rootCmd
5459}
5560
61+ type ChecksFailedError struct {
62+ error
63+ }
64+
65+ func (e ChecksFailedError ) Error () string {
66+ return e .error .Error ()
67+ }
68+
69+ type ChecksErroredError struct {
70+ error
71+ }
72+
73+ func (e ChecksErroredError ) Error () string {
74+ return e .error .Error ()
75+ }
76+
5677func Execute () error {
57- return rootCmd ().ExecuteContext (context .Background ())
78+ err := rootCmd ().ExecuteContext (context .Background ())
79+ if errors .Is (err , & cli.ChecksErroredError {}) {
80+ if viper .Instance ().GetBool ("exit_with_failure" ) {
81+ return ChecksErroredError {error : err }
82+ }
83+ } else if errors .Is (err , & cli.ChecksFailedError {}) {
84+ if viper .Instance ().GetBool ("exit_with_failure" ) {
85+ return ChecksFailedError {error : err }
86+ }
87+ } else if err != nil {
88+ return err
89+ }
90+
91+ return nil
5892}
5993
6094func initConfig (viper * spfviper.Viper ) {
0 commit comments