package cache import ( "fmt" "sync" "github.com/pterm/pterm" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes" ) func (c *Cache) LoadGitlabs() { wg := &sync.WaitGroup{} writer := pterm.DefaultMultiPrinter for _, gl := range *c.gitlabs { c.log.Info("Loading projects for remote", c.log.Args( "host", gl.Config.Host, "name", gl.Config.Name, )) opts := *remotes.DefaultListOpts opts.Owned = &c.config.Cache.Load.OwnerOnly projects := gl.GetTotalProjects(&opts) // Prepare progressbar pBar, _ := pterm.DefaultProgressbar. WithShowPercentage(true). WithTotal(projects). WithWriter(writer.NewWriter()). WithMaxWidth(100). Start(gl.Config.Host) wg.Add(1) go c.LoadGitlab(gl, wg, pBar, projects) } fmt.Println("") writer.Start() wg.Wait() writer.Stop() fmt.Println("") } 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 GitLab projects error", c.log.Args("error", e, "gitlab", client.Config.Name)) case <-client.Ctx.Done(): c.log.Warn("LoadProjects cancelled", c.log.Args("reason", client.Ctx.Err())) return case <-progressInfo.DoneChan: return } } }