git-project-manager/internal/cache/cache_load.go

75 lines
1.8 KiB
Go
Raw Normal View History

2024-01-15 21:02:15 +00:00
package cache
import (
"fmt"
"sync"
"github.com/pterm/pterm"
2024-01-15 19:57:15 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
2024-01-16 17:48:42 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
)
2024-01-16 17:48:42 +00:00
func (c *Cache) LoadRemotes() {
wg := &sync.WaitGroup{}
writer := pterm.DefaultMultiPrinter
2024-01-16 17:48:42 +00:00
for _, r := range *c.remotes {
if !remote.IsAlive(r) {
c.log.Error("Skipping load of remote, not alive", c.log.Args(
"remote", r.GetInfo(),
))
continue
}
c.log.Info("Loading projects for remote", c.log.Args(
2024-01-16 17:48:42 +00:00
"host", r.GetInfo().Host,
"name", r.GetInfo().Name,
))
2024-01-16 17:48:42 +00:00
opts := &remote.RemoteQueryOpts{
Ctx: r.GetInfo().Ctx,
OwnerOnly: c.config.Cache.Load.OwnerOnly,
}
pi := remotes.StreamRemote(r, opts)
// Prepare progressbar
pBar, _ := pterm.DefaultProgressbar.
WithShowPercentage(true).
2024-01-16 17:48:42 +00:00
WithTotal(pi.NumProjects).
WithWriter(writer.NewWriter()).
WithMaxWidth(100).
2024-01-16 17:48:42 +00:00
Start(r.GetInfo().Name)
wg.Add(1)
2024-01-16 17:48:42 +00:00
go c.ReceiveRemoteStream(r, wg, pBar, pi)
}
fmt.Println("")
writer.Start()
wg.Wait()
writer.Stop()
fmt.Println("")
}
2024-01-16 17:48:42 +00:00
func (c *Cache) ReceiveRemoteStream(remote remote.Remote, wg *sync.WaitGroup, pBar *pterm.ProgressbarPrinter, progressInfo *load.ProgressInfo) {
defer wg.Done()
for {
select {
case p := <-progressInfo.ProgressChan:
pBar.Add(p.Projects)
case p := <-progressInfo.ProjectsChan:
c.AddProjects(p...)
case e := <-progressInfo.ErrorChan:
2024-01-16 17:48:42 +00:00
c.log.Error("Fetch projects error", c.log.Args("error", e, "remote", remote.GetInfo().Name))
case <-remote.GetInfo().Ctx.Done():
c.log.Warn("LoadProjects cancelled", c.log.Args("reason", remote.GetInfo().Ctx.Err()))
return
case <-progressInfo.DoneChan:
return
}
}
}