Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
fb14c8cd
Commit
fb14c8cd
authored
Sep 09, 2015
by
Michal Fojtik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow to replace os.Exit() with panic when CLI command fatal error
parent
f867ba3b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
16 deletions
+25
-16
helpers.go
pkg/kubectl/cmd/util/helpers.go
+25
-16
No files found.
pkg/kubectl/cmd/util/helpers.go
View file @
fb14c8cd
...
@@ -62,13 +62,37 @@ func AddSourceToErr(verb string, source string, err error) error {
...
@@ -62,13 +62,37 @@ func AddSourceToErr(verb string, source string, err error) error {
return
err
return
err
}
}
var
fatalErrHandler
=
fatal
// BehaviorOnFatal allows you to override the default behavior when a fatal
// error occurs, which is call os.Exit(1). You can pass 'panic' as a function
// here if you prefer the panic() over os.Exit(1).
func
BehaviorOnFatal
(
f
func
(
string
))
{
fatalErrHandler
=
f
}
// fatal prints the message and then exits. If V(2) or greater, glog.Fatal
// is invoked for extended information.
func
fatal
(
msg
string
)
{
// add newline if needed
if
!
strings
.
HasSuffix
(
msg
,
"
\n
"
)
{
msg
+=
"
\n
"
}
if
glog
.
V
(
2
)
{
glog
.
FatalDepth
(
2
,
msg
)
}
fmt
.
Fprint
(
os
.
Stderr
,
msg
)
os
.
Exit
(
1
)
}
// 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.
//
//
// This method is generic to the command in use and may be used by non-Kubectl
// This method is generic to the command in use and may be used by non-Kubectl
// commands.
// commands.
func
CheckErr
(
err
error
)
{
func
CheckErr
(
err
error
)
{
checkErr
(
err
,
fatal
)
checkErr
(
err
,
fatal
ErrHandler
)
}
}
func
checkErr
(
err
error
,
handleErr
func
(
string
))
{
func
checkErr
(
err
error
,
handleErr
func
(
string
))
{
...
@@ -180,21 +204,6 @@ func messageForError(err error) string {
...
@@ -180,21 +204,6 @@ func messageForError(err error) string {
return
msg
return
msg
}
}
// fatal prints the message and then exits. If V(2) or greater, glog.Fatal
// is invoked for extended information.
func
fatal
(
msg
string
)
{
// add newline if needed
if
!
strings
.
HasSuffix
(
msg
,
"
\n
"
)
{
msg
+=
"
\n
"
}
if
glog
.
V
(
2
)
{
glog
.
FatalDepth
(
2
,
msg
)
}
fmt
.
Fprint
(
os
.
Stderr
,
msg
)
os
.
Exit
(
1
)
}
func
UsageError
(
cmd
*
cobra
.
Command
,
format
string
,
args
...
interface
{})
error
{
func
UsageError
(
cmd
*
cobra
.
Command
,
format
string
,
args
...
interface
{})
error
{
msg
:=
fmt
.
Sprintf
(
format
,
args
...
)
msg
:=
fmt
.
Sprintf
(
format
,
args
...
)
return
fmt
.
Errorf
(
"%s
\n
see '%s -h' for help."
,
msg
,
cmd
.
CommandPath
())
return
fmt
.
Errorf
(
"%s
\n
see '%s -h' for help."
,
msg
,
cmd
.
CommandPath
())
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment