44 "bytes"
55 "flag"
66 "fmt"
7+ "io"
78 "os"
89 "slices"
910 "strconv"
@@ -86,7 +87,7 @@ func initFlags(flags *Flags) error {
8687func NewApp (cfg AppConfig ) (* App , error ) {
8788 repoSplit := strings .Split (cfg .Repo , "/" )
8889 if len (repoSplit ) != 2 {
89- return nil , fmt .Errorf ("invalid repo name: %s" , cfg .Repo )
90+ return nil , fmt .Errorf ("Invalid repo name: %s" , cfg .Repo )
9091 }
9192 owner := repoSplit [0 ]
9293 repo := repoSplit [1 ]
@@ -379,19 +380,19 @@ func ignoreError[V any, E error](res V, _ E) V {
379380 return res
380381}
381382
382- func errorAndExit ( shouldFail bool , format string , args ... interface {} ) {
383- _ , err := WarningBuffer .WriteTo (os . Stderr )
383+ func outputAndExit ( w io. Writer , shouldFail bool , message string ) {
384+ _ , err := WarningBuffer .WriteTo (w )
384385 if err != nil {
385386 fmt .Fprintf (os .Stderr , "Error writing warning buffer: %v\n " , err )
386387 }
387388 if * flags .Verbose {
388- _ , err := InfoBuffer .WriteTo (os . Stderr )
389+ _ , err := InfoBuffer .WriteTo (w )
389390 if err != nil {
390391 fmt .Fprintf (os .Stderr , "Error writing info buffer: %v\n " , err )
391392 }
392393 }
393394
394- fmt .Fprintf ( os . Stderr , format , args ... )
395+ fmt .Fprint ( w , message )
395396 if testing .Testing () {
396397 return
397398 }
@@ -415,7 +416,7 @@ func printWarning(format string, args ...interface{}) {
415416func main () {
416417 err := initFlags (flags )
417418 if err != nil {
418- errorAndExit ( true , "%v \n " , err )
419+ outputAndExit ( os . Stderr , true , fmt . Sprintln ( err ) )
419420 }
420421
421422 cfg := AppConfig {
@@ -428,29 +429,19 @@ func main() {
428429
429430 app , err := NewApp (cfg )
430431 if err != nil {
431- errorAndExit ( true , "Failed to initialize app: %v\n " , err )
432+ outputAndExit ( os . Stderr , true , fmt . Sprintf ( "Failed to initialize app: %v\n " , err ) )
432433 }
433434
434435 success , message , err := app .Run ()
435436 if err != nil {
436- errorAndExit (true , "%v\n " , err )
437- }
438- _ , err = WarningBuffer .WriteTo (os .Stderr )
439- if err != nil {
440- fmt .Fprintf (os .Stderr , "Error writing warning buffer: %v\n " , err )
441- }
442- if * flags .Verbose {
443- _ , err = InfoBuffer .WriteTo (os .Stdout )
444- if err != nil {
445- fmt .Fprintf (os .Stderr , "Error writing info buffer: %v\n " , err )
446- }
437+ outputAndExit (os .Stderr , true , fmt .Sprintln (err ))
447438 }
439+ var w io.Writer
448440 if success {
449- fmt . Fprintln ( os .Stdout , message )
441+ w = os .Stdout
450442 } else {
451- fmt .Fprintln (os .Stderr , message )
452- }
453- if ! success && app .conf .Enforcement .FailCheck {
454- os .Exit (1 )
443+ w = os .Stderr
455444 }
445+ shouldFail := ! success && app .conf .Enforcement .FailCheck
446+ outputAndExit (w , shouldFail , message )
456447}
0 commit comments