mirror of
https://git.sr.ht/~phw/scotty
synced 2025-06-01 19:38:34 +02:00
Use the transfer context also for the progress bars
This commit is contained in:
parent
3b545a0fd6
commit
adfe3f5771
2 changed files with 14 additions and 6 deletions
|
@ -18,6 +18,7 @@ Scotty. If not, see <https://www.gnu.org/licenses/>.
|
||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -39,9 +40,10 @@ type progressBarUpdater struct {
|
||||||
importedItems int
|
importedItems int
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupProgressBars(updateChan chan models.TransferProgress) progressBarUpdater {
|
func setupProgressBars(ctx context.Context, updateChan chan models.TransferProgress) progressBarUpdater {
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
p := mpb.New(
|
p := mpb.NewWithContext(
|
||||||
|
ctx,
|
||||||
mpb.WithWaitGroup(wg),
|
mpb.WithWaitGroup(wg),
|
||||||
mpb.WithOutput(color.Output),
|
mpb.WithOutput(color.Output),
|
||||||
// mpb.WithWidth(64),
|
// mpb.WithWidth(64),
|
||||||
|
|
|
@ -110,11 +110,13 @@ func (c *TransferCmd[E, I, R]) Transfer(exp backends.ExportProcessor[R], imp bac
|
||||||
}
|
}
|
||||||
printTimestamp("From timestamp: %v (%v)", timestamp)
|
printTimestamp("From timestamp: %v (%v)", timestamp)
|
||||||
|
|
||||||
|
// Use a context with cancel to abort the transfer
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
// Prepare progress bars
|
// Prepare progress bars
|
||||||
progressChan := make(chan models.TransferProgress)
|
progressChan := make(chan models.TransferProgress)
|
||||||
progress := setupProgressBars(progressChan)
|
progress := setupProgressBars(ctx, progressChan)
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
|
|
||||||
// Export from source
|
// Export from source
|
||||||
|
@ -126,8 +128,12 @@ func (c *TransferCmd[E, I, R]) Transfer(exp backends.ExportProcessor[R], imp bac
|
||||||
go imp.Process(ctx, wg, exportChan, resultChan, progressChan)
|
go imp.Process(ctx, wg, exportChan, resultChan, progressChan)
|
||||||
result := <-resultChan
|
result := <-resultChan
|
||||||
|
|
||||||
// Once import is done, the context can be cancelled
|
// If the import has errored, the context can be cancelled immediately
|
||||||
cancel()
|
if result.Error != nil {
|
||||||
|
cancel()
|
||||||
|
} else {
|
||||||
|
defer cancel()
|
||||||
|
}
|
||||||
|
|
||||||
// Wait for all goroutines to finish
|
// Wait for all goroutines to finish
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue