Migrate to remotes interface

This commit is contained in:
2024-01-16 12:48:42 -05:00
parent 5337ea544b
commit e7f8b86f72
13 changed files with 160 additions and 283 deletions

View File

@@ -6,32 +6,43 @@ import (
"github.com/pterm/pterm"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
)
func (c *Cache) LoadGitlabs() {
func (c *Cache) LoadRemotes() {
wg := &sync.WaitGroup{}
writer := pterm.DefaultMultiPrinter
for _, gl := range *c.gitlabs {
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(
"host", gl.Config.Host,
"name", gl.Config.Name,
"host", r.GetInfo().Host,
"name", r.GetInfo().Name,
))
opts := *remotes.DefaultListOpts
opts.Owned = &c.config.Cache.Load.OwnerOnly
projects := gl.GetTotalProjects(&opts)
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).
WithTotal(projects).
WithTotal(pi.NumProjects).
WithWriter(writer.NewWriter()).
WithMaxWidth(100).
Start(gl.Config.Host)
Start(r.GetInfo().Name)
wg.Add(1)
go c.LoadGitlab(gl, wg, pBar, projects)
go c.ReceiveRemoteStream(r, wg, pBar, pi)
}
fmt.Println("")
@@ -43,10 +54,8 @@ func (c *Cache) LoadGitlabs() {
fmt.Println("")
}
func (c *Cache) LoadRemote(client *remotes.Client, wg *sync.WaitGroup, pBar *pterm.ProgressbarPrinter, projects int) {
func (c *Cache) ReceiveRemoteStream(remote remote.Remote, wg *sync.WaitGroup, pBar *pterm.ProgressbarPrinter, progressInfo *load.ProgressInfo) {
defer wg.Done()
progressInfo := client.StreamProjects(c.config.Cache.Load.OwnerOnly, projects)
for {
select {
case p := <-progressInfo.ProgressChan:
@@ -54,30 +63,9 @@ func (c *Cache) LoadRemote(client *remotes.Client, wg *sync.WaitGroup, pBar *pte
case p := <-progressInfo.ProjectsChan:
c.AddProjects(p...)
case e := <-progressInfo.ErrorChan:
c.log.Error("Fetch projects error", c.log.Args("error", e, "remote", client.Config.Name))
case <-client.Ctx.Done():
c.log.Warn("LoadProjects cancelled", c.log.Args("reason", client.Ctx.Err()))
return
case <-progressInfo.DoneChan:
return
}
}
}
func (c *Cache) LoadGitlab(client *remotes.Client, wg *sync.WaitGroup, pBar *pterm.ProgressbarPrinter, projects int) {
defer wg.Done()
progressInfo := client.StreamProjects(c.config.Cache.Load.OwnerOnly, projects)
for {
select {
case p := <-progressInfo.ProgressChan:
pBar.Add(p.Projects)
case p := <-progressInfo.ProjectsChan:
c.AddProjects(p...)
case e := <-progressInfo.ErrorChan:
c.log.Error("Fetch projects error", c.log.Args("error", e, "remote", client.Config.Name))
case <-client.Ctx.Done():
c.log.Warn("LoadProjects cancelled", c.log.Args("reason", client.Ctx.Err()))
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