backup/restore: fix termstatus initialization

The termstatus must only be canceled once the command has returned.
Otherwise output may be lost when the context gets canceled.
This commit is contained in:
Michael Eischer 2024-01-20 18:12:36 +01:00
parent d26d2d41f8
commit 6b65a495b1
3 changed files with 6 additions and 5 deletions

View File

@ -54,7 +54,7 @@ Exit status is 3 if some source data could not be read (incomplete snapshot crea
}, },
DisableAutoGenTag: true, DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
term, cancel := setupTermstatus(cmd.Context()) term, cancel := setupTermstatus()
defer cancel() defer cancel()
return runBackup(cmd.Context(), backupOptions, globalOptions, term, args) return runBackup(cmd.Context(), backupOptions, globalOptions, term, args)
}, },

View File

@ -37,7 +37,7 @@ Exit status is 0 if the command was successful, and non-zero if there was any er
`, `,
DisableAutoGenTag: true, DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
term, cancel := setupTermstatus(cmd.Context()) term, cancel := setupTermstatus()
defer cancel() defer cancel()
return runRestore(cmd.Context(), restoreOptions, globalOptions, term, args) return runRestore(cmd.Context(), restoreOptions, globalOptions, term, args)
}, },

View File

@ -13,13 +13,14 @@ import (
// //
// Expected usage: // Expected usage:
// ``` // ```
// term, cancel := setupTermstatus(ctx) // term, cancel := setupTermstatus()
// defer cancel() // defer cancel()
// // do stuff // // do stuff
// ``` // ```
func setupTermstatus(ctx context.Context) (*termstatus.Terminal, func()) { func setupTermstatus() (*termstatus.Terminal, func()) {
var wg sync.WaitGroup var wg sync.WaitGroup
cancelCtx, cancel := context.WithCancel(ctx) // only shutdown once cancel is called to ensure that no output is lost
cancelCtx, cancel := context.WithCancel(context.Background())
term := termstatus.New(globalOptions.stdout, globalOptions.stderr, globalOptions.Quiet) term := termstatus.New(globalOptions.stdout, globalOptions.stderr, globalOptions.Quiet)
wg.Add(1) wg.Add(1)