Skip to content

Commit f9ebd56

Browse files
committed
Add new flag to use non-zero exit code when checks fail
Signed-off-by: Caleb Xu <[email protected]>
1 parent c2622b1 commit f9ebd56

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

cmd/preflight/cmd/root.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package cmd
33

44
import (
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+
5677
func 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

6094
func initConfig(viper *spfviper.Viper) {

cmd/preflight/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
package main
22

33
import (
4+
"errors"
45
"log"
6+
"os"
57

68
"github.com/redhat-openshift-ecosystem/openshift-preflight/cmd/preflight/cmd"
79
)
810

911
func main() {
1012
if err := cmd.Execute(); err != nil {
11-
log.Fatal(err)
13+
if errors.Is(err, &cmd.ChecksErroredError{}) {
14+
log.Println(err)
15+
os.Exit(1)
16+
} else if errors.Is(err, &cmd.ChecksFailedError{}) {
17+
log.Println(err)
18+
os.Exit(2)
19+
} else {
20+
log.Fatal(err)
21+
}
1222
}
1323
}

internal/cli/cli.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ type CheckConfig struct {
2323
SubmitResults bool
2424
}
2525

26+
type ChecksFailedError struct{}
27+
28+
func (e *ChecksFailedError) Error() string {
29+
return "one or more checks failed"
30+
}
31+
32+
type ChecksErroredError struct{}
33+
34+
func (e *ChecksErroredError) Error() string {
35+
return "one or more checks encountered an error"
36+
}
37+
2638
// RunPreflight executes checks, writes logs, results, and submits results if requested.
2739
func RunPreflight(
2840
ctx context.Context,
@@ -84,6 +96,14 @@ func RunPreflight(
8496

8597
logger.Info(fmt.Sprintf("Preflight result: %s", convertPassedOverall(results.PassedOverall)))
8698

99+
if len(results.Errors) > 0 {
100+
return &ChecksErroredError{}
101+
}
102+
103+
if len(results.Failed) > 0 {
104+
return &ChecksFailedError{}
105+
}
106+
87107
return nil
88108
}
89109

0 commit comments

Comments
 (0)