From 271dc1334cf8ce3c798dafeb9ac7113072195c93 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 9 May 2021 11:33:20 +0200 Subject: commands: proper backend close on RunE error --- commands/env.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'commands/env.go') 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 } } -- cgit