Cobra command runners skip defer
ed funcs and postruns on failures
#2890
Labels
bug
Something isn't working
good first issue
Good for newcomers
I4
No visible changes
neofs-cli
NeoFS CLI application issues
neofs-lens
NeoFS Lens app issues
S3
Minimally significant
U4
Nothing urgent
Milestone
some
Cobra
-based programs exit instantly on particular errors:neofs-node/cmd/neofs-cli/internal/common/exit.go
Line 22 in 450c4e5
neofs-node/cmd/neofs-lens/internal/errors.go
Line 20 in 450c4e5
they call
os.Exit
due to which neither cmd postruns, nor finalizers, nor even execdefer
funcs are called. The latter are usually responsible for allocating resources such as network connections, files, etc.Steps to Reproduce
cmd/neofs-test/main.go
make bin/neofs-test
Expected Behavior
Current Behavior
Possible Solution
1. Finalizing closure in
Run
the most clean solution to me in terms of code and command lifetime. At the same time, requires a lot of changes, code will be pretty "spaghetti"
2. Specific error +
RunE
+ unsupported*PostRun
the most correct solution to me in terms of
os.Exit
, but narrows the breadth of cmd lifetime use. Also requires a lot of changes but code will look less "spaghetti"3. Fake
defer
's + unsupported*PostRun
the worst to me but fast solution requiring less changes
Context
Cobra itself has no in-box mechanism to work with OS exit codes, only similar funcs
ive been hiding this topic in myself for a long time, but now im ready to present it
Regression
no
Your Environment
[email protected]
The text was updated successfully, but these errors were encountered: