code.cestus.io/tools/fabricator@v0.4.3/internal/pkg/util/helpers.go (about)

     1  package util
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  const (
    13  	DefaultErrorExitCode = 1
    14  )
    15  
    16  var fatalErrHandler = fatal
    17  
    18  // BehaviorOnFatal allows you to override the default behavior when a fatal
    19  // error occurs, which is to call os.Exit(code). You can pass 'panic' as a function
    20  // here if you prefer the panic() over os.Exit(1).
    21  func BehaviorOnFatal(f func(string, int)) {
    22  	fatalErrHandler = f
    23  }
    24  
    25  // DefaultBehaviorOnFatal allows you to undo any previous override.  Useful in
    26  // tests.
    27  func DefaultBehaviorOnFatal() {
    28  	fatalErrHandler = fatal
    29  }
    30  
    31  // fatal prints the message (if provided) and then exits. If V(6) or greater,
    32  // klog.Fatal is invoked for extended information.
    33  func fatal(msg string, code int) {
    34  	if len(msg) > 0 {
    35  		// add newline if needed
    36  		if !strings.HasSuffix(msg, "\n") {
    37  			msg += "\n"
    38  		}
    39  		fmt.Fprint(os.Stderr, msg)
    40  	}
    41  	os.Exit(code)
    42  }
    43  
    44  // ErrExit may be passed to CheckError to instruct it to output nothing but exit with
    45  // status code 1.
    46  var ErrExit = fmt.Errorf("exit")
    47  
    48  // CheckErr prints a user friendly error to STDERR and exits with a non-zero
    49  // exit code. Unrecognized errors will be printed with an "error: " prefix.
    50  //
    51  // This method is generic to the command in use and may be used by non-fabricator
    52  // commands.
    53  func CheckErr(err error) {
    54  	checkErr(err, fatalErrHandler)
    55  }
    56  
    57  // checkErr formats a given error as a string and calls the passed handleErr
    58  // func with that string and an kubectl exit code.
    59  func checkErr(err error, handleErr func(string, int)) {
    60  
    61  	if err == nil {
    62  		return
    63  	}
    64  	fatalErrHandler(err.Error(), DefaultErrorExitCode)
    65  }
    66  
    67  // DefaultSubCommandRun prints a command's help string to the specified output if no
    68  // arguments (sub-commands) are provided, or a usage error otherwise.
    69  func DefaultSubCommandRun(out io.Writer) func(c *cobra.Command, args []string) {
    70  	return func(c *cobra.Command, args []string) {
    71  		c.SetOutput(out)
    72  		RequireNoArguments(c, args)
    73  		c.Help()
    74  		CheckErr(ErrExit)
    75  	}
    76  }
    77  
    78  // RequireNoArguments exits with a usage error if extra arguments are provided.
    79  func RequireNoArguments(c *cobra.Command, args []string) {
    80  	if len(args) > 0 {
    81  		CheckErr(UsageErrorf(c, "unknown command %q", strings.Join(args, " ")))
    82  	}
    83  }
    84  
    85  // UsageErrorf prints the usage error
    86  func UsageErrorf(cmd *cobra.Command, format string, args ...interface{}) error {
    87  	msg := fmt.Sprintf(format, args...)
    88  	return fmt.Errorf("%s\nSee '%s -h' for help and examples", msg, cmd.CommandPath())
    89  }