mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	Allow callers to bypass cmdutil.CheckError() logging
This commit is contained in:
		@@ -79,8 +79,6 @@ var (
 | 
				
			|||||||
		  kubectl edit svc/docker-registry --output-version=v1 -o json`)
 | 
							  kubectl edit svc/docker-registry --output-version=v1 -o json`)
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var errExit = fmt.Errorf("exit directly")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func NewCmdEdit(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command {
 | 
					func NewCmdEdit(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command {
 | 
				
			||||||
	options := &resource.FilenameOptions{}
 | 
						options := &resource.FilenameOptions{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -102,9 +100,6 @@ func NewCmdEdit(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command {
 | 
				
			|||||||
		Example: fmt.Sprintf(editExample),
 | 
							Example: fmt.Sprintf(editExample),
 | 
				
			||||||
		Run: func(cmd *cobra.Command, args []string) {
 | 
							Run: func(cmd *cobra.Command, args []string) {
 | 
				
			||||||
			err := RunEdit(f, out, errOut, cmd, args, options)
 | 
								err := RunEdit(f, out, errOut, cmd, args, options)
 | 
				
			||||||
			if err == errExit {
 | 
					 | 
				
			||||||
				os.Exit(1)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			cmdutil.CheckErr(err)
 | 
								cmdutil.CheckErr(err)
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		ValidArgs:  validArgs,
 | 
							ValidArgs:  validArgs,
 | 
				
			||||||
@@ -285,11 +280,11 @@ func RunEdit(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args
 | 
				
			|||||||
			// 3. invalid: retry those on the spot by looping ie. reloading the editor
 | 
								// 3. invalid: retry those on the spot by looping ie. reloading the editor
 | 
				
			||||||
			if results.retryable > 0 {
 | 
								if results.retryable > 0 {
 | 
				
			||||||
				fmt.Fprintf(errOut, "You can run `%s replace -f %s` to try this update again.\n", filepath.Base(os.Args[0]), file)
 | 
									fmt.Fprintf(errOut, "You can run `%s replace -f %s` to try this update again.\n", filepath.Base(os.Args[0]), file)
 | 
				
			||||||
				return errExit
 | 
									return cmdutil.ErrExit
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if results.notfound > 0 {
 | 
								if results.notfound > 0 {
 | 
				
			||||||
				fmt.Fprintf(errOut, "The edits you made on deleted resources have been saved to %q\n", file)
 | 
									fmt.Fprintf(errOut, "The edits you made on deleted resources have been saved to %q\n", file)
 | 
				
			||||||
				return errExit
 | 
									return cmdutil.ErrExit
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if len(results.edit) == 0 {
 | 
								if len(results.edit) == 0 {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -88,23 +88,26 @@ func DefaultBehaviorOnFatal() {
 | 
				
			|||||||
	fatalErrHandler = fatal
 | 
						fatalErrHandler = fatal
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// fatal prints the message if set and then exits. If V(2) or greater, glog.Fatal
 | 
					// fatal prints the message (if provided) and then exits. If V(2) or greater,
 | 
				
			||||||
// is invoked for extended information.
 | 
					// glog.Fatal is invoked for extended information.
 | 
				
			||||||
func fatal(msg string, code int) {
 | 
					func fatal(msg string, code int) {
 | 
				
			||||||
 | 
						if glog.V(2) {
 | 
				
			||||||
 | 
							glog.FatalDepth(2, msg)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	if len(msg) > 0 {
 | 
						if len(msg) > 0 {
 | 
				
			||||||
		// add newline if needed
 | 
							// add newline if needed
 | 
				
			||||||
		if !strings.HasSuffix(msg, "\n") {
 | 
							if !strings.HasSuffix(msg, "\n") {
 | 
				
			||||||
			msg += "\n"
 | 
								msg += "\n"
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					 | 
				
			||||||
		if glog.V(2) {
 | 
					 | 
				
			||||||
			glog.FatalDepth(2, msg)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		fmt.Fprint(os.Stderr, msg)
 | 
							fmt.Fprint(os.Stderr, msg)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	os.Exit(code)
 | 
						os.Exit(code)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// ErrExit may be passed to CheckError to instruct it to output nothing but exit with
 | 
				
			||||||
 | 
					// status code 1.
 | 
				
			||||||
 | 
					var ErrExit = fmt.Errorf("exit")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// CheckErr prints a user friendly error to STDERR and exits with a non-zero
 | 
					// CheckErr prints a user friendly error to STDERR and exits with a non-zero
 | 
				
			||||||
// exit code. Unrecognized errors will be printed with an "error: " prefix.
 | 
					// exit code. Unrecognized errors will be printed with an "error: " prefix.
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
@@ -130,6 +133,9 @@ func checkErr(prefix string, err error, handleErr func(string, int)) {
 | 
				
			|||||||
	switch {
 | 
						switch {
 | 
				
			||||||
	case err == nil:
 | 
						case err == nil:
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
 | 
						case err == ErrExit:
 | 
				
			||||||
 | 
							handleErr("", DefaultErrorExitCode)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
	case kerrors.IsInvalid(err):
 | 
						case kerrors.IsInvalid(err):
 | 
				
			||||||
		details := err.(*kerrors.StatusError).Status().Details
 | 
							details := err.(*kerrors.StatusError).Status().Details
 | 
				
			||||||
		s := fmt.Sprintf("%sThe %s %q is invalid", prefix, details.Kind, details.Name)
 | 
							s := fmt.Sprintf("%sThe %s %q is invalid", prefix, details.Kind, details.Name)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user