diff options
author | Michael Muré <batolettre@gmail.com> | 2021-05-10 11:14:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-10 11:14:26 +0200 |
commit | 13d9632fb888dde9962e122ef06614ca9e9da83d (patch) | |
tree | f743c58e2748cfa5e01fd81ad3ea3c7a51003a33 /commands/env.go | |
parent | 52b7ec5959d6aa24b2fa3bff33f3e6424f11d885 (diff) | |
parent | 271dc1334cf8ce3c798dafeb9ac7113072195c93 (diff) | |
download | git-bug-13d9632fb888dde9962e122ef06614ca9e9da83d.tar.gz |
Merge pull request #671 from MichaelMure/close-on-error
commands: proper backend close on RunE error
Diffstat (limited to 'commands/env.go')
-rw-r--r-- | commands/env.go | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/commands/env.go b/commands/env.go index 8d12c5bf..ac7b789a 100644 --- a/commands/env.go +++ b/commands/env.go @@ -136,15 +136,23 @@ func loadBackendEnsureUser(env *Env) func(*cobra.Command, []string) error { } } -// closeBackend is a post-run function that will close the backend properly +// closeBackend is a wrapper for a RunE function that will close the backend properly // if it has been opened. -func closeBackend(env *Env) func(*cobra.Command, []string) error { +// This wrapper style is necessary because a Cobra PostE function does not run if RunE return an error. +func closeBackend(env *Env, runE func(cmd *cobra.Command, args []string) error) func(*cobra.Command, []string) error { return func(cmd *cobra.Command, args []string) error { + errRun := runE(cmd, args) + if env.backend == nil { return nil } err := env.backend.Close() env.backend = nil + + // prioritize the RunE error + if errRun != nil { + return errRun + } return err } } |