fix: always close update channel and wait for goroutine exit

fix: copy label style to not affect it everywhere
This commit is contained in:
Vilsol 2022-06-18 19:09:09 +03:00
parent b0111404aa
commit 6d5b929ef9
3 changed files with 58 additions and 38 deletions

View file

@ -7,6 +7,7 @@ import (
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/satisfactorymodding/ficsit-cli/utils"
@ -341,43 +342,64 @@ func (i *Installation) Install(ctx *GlobalContext, updates chan InstallUpdate) e
}
}
downloading := true
completed := 0
var genericUpdates chan utils.GenericUpdate
if updates != nil {
var wg sync.WaitGroup
wg.Add(1)
defer wg.Wait()
genericUpdates = make(chan utils.GenericUpdate)
defer close(genericUpdates)
go func() {
defer wg.Done()
update := InstallUpdate{
OverallProgress: float64(completed) / float64(len(lockfile)),
DownloadProgress: 0,
ExtractProgress: 0,
}
select {
case updates <- update:
default:
}
for up := range genericUpdates {
if downloading {
update.DownloadProgress = up.Progress
} else {
update.DownloadProgress = 1
update.ExtractProgress = up.Progress
}
if up.ModReference != nil {
update.ModName = *up.ModReference
}
update.OverallProgress = float64(completed) / float64(len(lockfile))
select {
case updates <- update:
default:
}
}
}()
}
for modReference, version := range lockfile {
// Only install if a link is provided, otherwise assume mod is already installed
if version.Link != "" {
downloading := true
downloading = true
var genericUpdates chan utils.GenericUpdate
if updates != nil {
genericUpdates = make(chan utils.GenericUpdate)
go func() {
update := InstallUpdate{
ModName: modReference,
OverallProgress: float64(completed) / float64(len(lockfile)),
DownloadProgress: 0,
ExtractProgress: 0,
}
select {
case updates <- update:
default:
}
for up := range genericUpdates {
if downloading {
update.DownloadProgress = up.Progress
} else {
update.DownloadProgress = 1
update.ExtractProgress = up.Progress
}
select {
case updates <- update:
default:
}
}
}()
if genericUpdates != nil {
select {
case genericUpdates <- utils.GenericUpdate{ModReference: &modReference}:
default:
}
}
log.Info().Str("mod_reference", modReference).Str("version", version.Version).Str("link", version.Link).Msg("downloading mod")
@ -392,10 +414,6 @@ func (i *Installation) Install(ctx *GlobalContext, updates chan InstallUpdate) e
if err := utils.ExtractMod(reader, size, filepath.Join(modsDirectory, modReference), version.Hash, genericUpdates); err != nil {
return errors.Wrap(err, "could not extract "+modReference)
}
if genericUpdates != nil {
close(genericUpdates)
}
}
completed++

View file

@ -4,6 +4,7 @@ import (
"github.com/charmbracelet/bubbles/progress"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/satisfactorymodding/ficsit-cli/cli"
"github.com/satisfactorymodding/ficsit-cli/tea/components"
"github.com/satisfactorymodding/ficsit-cli/tea/utils"
@ -194,7 +195,7 @@ func (m apply) View() string {
}
if m.status.done {
strs = append(strs, utils.LabelStyle.Padding(0).Margin(1).Render("Done! Press Enter to return"))
strs = append(strs, utils.LabelStyle.Copy().Padding(0).Margin(1).Render("Done! Press Enter to return"))
}
result := lipgloss.NewStyle().MarginLeft(1).Render(lipgloss.JoinVertical(lipgloss.Left, strs...))

View file

@ -42,7 +42,8 @@ func (pt *Progresser) Read(p []byte) (int, error) {
}
type GenericUpdate struct {
Progress float64
Progress float64
ModReference *string
}
func DownloadOrCache(cacheKey string, hash string, url string, updates chan GenericUpdate) (r io.ReaderAt, size int64, err error) {