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,19 +342,22 @@ func (i *Installation) Install(ctx *GlobalContext, updates chan InstallUpdate) e
}
}
completed := 0
for modReference, version := range lockfile {
// Only install if a link is provided, otherwise assume mod is already installed
if version.Link != "" {
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{
ModName: modReference,
OverallProgress: float64(completed) / float64(len(lockfile)),
DownloadProgress: 0,
ExtractProgress: 0,
@ -372,6 +376,12 @@ func (i *Installation) Install(ctx *GlobalContext, updates chan InstallUpdate) e
update.ExtractProgress = up.Progress
}
if up.ModReference != nil {
update.ModName = *up.ModReference
}
update.OverallProgress = float64(completed) / float64(len(lockfile))
select {
case updates <- update:
default:
@ -380,6 +390,18 @@ func (i *Installation) Install(ctx *GlobalContext, updates chan InstallUpdate) e
}()
}
for modReference, version := range lockfile {
// Only install if a link is provided, otherwise assume mod is already installed
if version.Link != "" {
downloading = true
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")
reader, size, err := utils.DownloadOrCache(modReference+"_"+version.Version+".zip", version.Hash, version.Link, genericUpdates)
if err != nil {
@ -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

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