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

87 lines
2.3 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"
"golang.org/x/exp/slices"
)
func (c *Cache) LoadRemotes(gitRemotes ...string) {
wg := &sync.WaitGroup{}
writer := pterm.DefaultMultiPrinter
for _, r := range *c.remotes {
// Don't bother if it's dead or the user has provided
// one or more remotes via --remote flag(s)
2024-01-16 17:48:42 +00:00
if !remote.IsAlive(r) {
c.log.Error("Skipping load of remote, not alive", c.log.Args(
"remote", r.String()))
continue
2024-01-18 17:04:06 +00:00
} else if len(gitRemotes) > 0 && !slices.Contains(gitRemotes, r.GetInfo().Host) {
c.log.Debug("Skipping remote not in --remote list", c.log.Args(
2024-01-16 19:26:56 +00:00
"remote", r.String(),
"remotes", gitRemotes))
2024-01-16 17:48:42 +00:00
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{
2024-01-17 21:56:41 +00:00
Ctx: r.GetInfo().Context(),
2024-01-16 17:48:42 +00:00
OwnerOnly: c.config.Cache.Load.OwnerOnly,
}
2024-01-16 19:26:56 +00:00
pi, err := remotes.StreamRemote(r, opts)
if err != nil {
c.log.Error("Skipping remote", c.log.Args("error", err))
continue
}
// 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))
2024-01-17 21:56:41 +00:00
case <-remote.GetInfo().Context().Done():
c.log.Warn("LoadProjects cancelled", c.log.Args("reason", remote.GetInfo().Context().Err()))
return
case <-progressInfo.DoneChan:
return
}
}
}