diff --git a/cli/installations.go b/cli/installations.go index 7dca3a4..d0fc083 100644 --- a/cli/installations.go +++ b/cli/installations.go @@ -548,11 +548,14 @@ func downloadAndExtractMod(modReference string, version string, link string, has var extractUpdates chan utils.GenericProgress + var wg sync.WaitGroup if updates != nil { // Forward the inner updates as InstallUpdates extractUpdates = make(chan utils.GenericProgress) + wg.Add(1) go func() { + defer wg.Done() for up := range extractUpdates { select { case updates <- InstallUpdate{ @@ -585,8 +588,12 @@ func downloadAndExtractMod(modReference string, version string, link string, has }: default: } + + close(extractUpdates) } + wg.Wait() + return nil } diff --git a/go.mod b/go.mod index 3004199..91434fc 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/charmbracelet/glamour v0.5.0 github.com/charmbracelet/lipgloss v0.6.0 github.com/jlaffaye/ftp v0.1.0 - github.com/mircearoata/pubgrub-go v0.3.2 + github.com/mircearoata/pubgrub-go v0.3.3 github.com/muesli/reflow v0.3.0 github.com/pkg/errors v0.9.1 github.com/pterm/pterm v0.12.67 diff --git a/go.sum b/go.sum index f1238e7..29fddef 100644 --- a/go.sum +++ b/go.sum @@ -269,8 +269,8 @@ github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh github.com/microcosm-cc/bluemonday v1.0.17/go.mod h1:Z0r70sCuXHig8YpBzCc5eGHAap2K7e/u082ZUpDRRqM= github.com/microcosm-cc/bluemonday v1.0.21 h1:dNH3e4PSyE4vNX+KlRGHT5KrSvjeUkoNPwEORjffHJg= github.com/microcosm-cc/bluemonday v1.0.21/go.mod h1:ytNkv4RrDrLJ2pqlsSI46O6IVXmZOBBD4SaJyDwwTkM= -github.com/mircearoata/pubgrub-go v0.3.2 h1:AeC2bvHebii6YrfyN+AST06WiQUENKQkREA7Xoi4/HY= -github.com/mircearoata/pubgrub-go v0.3.2/go.mod h1:9oWL9ZXdjFYvnGl95qiM1dTciFNx1MN8fUnG3SUwDi8= +github.com/mircearoata/pubgrub-go v0.3.3 h1:XGwL8Xh5GX+mbnvWItbM/lVJxAq3NZtfUtbJ/hUf2ig= +github.com/mircearoata/pubgrub-go v0.3.3/go.mod h1:9oWL9ZXdjFYvnGl95qiM1dTciFNx1MN8fUnG3SUwDi8= github.com/mitchellh/mapstructure v1.2.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= diff --git a/utils/io.go b/utils/io.go index 6ad5630..39c8eff 100644 --- a/utils/io.go +++ b/utils/io.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "sync" + "sync/atomic" "github.com/pkg/errors" @@ -101,12 +102,13 @@ func ExtractMod(f io.ReaderAt, size int64, location string, hash string, updates } totalExtracted := int64(0) + totalExtractedPtr := &totalExtracted + channelUsers := sync.WaitGroup{} if updates != nil { defer func() { channelUsers.Wait() - close(updates) }() } @@ -126,7 +128,7 @@ func ExtractMod(f io.ReaderAt, size int64, location string, hash string, updates defer channelUsers.Done() for fileUpdate := range fileUpdates { updates <- GenericProgress{ - Completed: totalExtracted + fileUpdate.Completed, + Completed: atomic.LoadInt64(totalExtractedPtr) + fileUpdate.Completed, Total: totalSize, } } @@ -137,7 +139,7 @@ func ExtractMod(f io.ReaderAt, size int64, location string, hash string, updates return err } - totalExtracted += int64(file.UncompressedSize64) + atomic.AddInt64(totalExtractedPtr, int64(file.UncompressedSize64)) } }