git-project-manager/internal/projects/projects.go
2023-12-07 12:08:56 -05:00

54 lines
1.3 KiB
Go

package projects
import (
"fmt"
"github.com/pterm/pterm"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
)
type ProjectAlias struct {
Alias string
ProjectID string
Project *gitlab.Project
}
func (c *Cache) LoadProjects() {
progressInfo := c.gitlab.StreamProjects()
c.Projects = make([]*gitlab.Project, 0)
pBar := pterm.DefaultProgressbar.
WithShowPercentage(true).
WithTotal(-1).
WithTitle("Listing GitLab Projects").
WithMaxWidth(0)
defer pBar.Stop()
var curProjects int
for {
select {
case p := <-progressInfo.ProgressChan:
if pBar.Total == -1 {
pBar = pBar.WithTotal(p.TotalProjects)
pBar, _ = pBar.Start()
}
// This sucks, has to be a better way, and why is the logger incompatible
// with the progressbar?
pterm.Debug.Println(fmt.Sprintf("Update received: %#v", p))
pBar.Add(p.Projects - curProjects)
curProjects = p.Projects
case p := <-progressInfo.ProjectsChan:
c.Projects = append(c.Projects, p...)
case e := <-progressInfo.ErrorChan:
c.log.Error("Fetch GitLab projects error", c.log.Args("error", e))
case <-c.gitlab.Ctx.Done():
c.log.Warn("LoadProjects cancelled", c.log.Args("reason", c.gitlab.Ctx.Err()))
return
case <-progressInfo.DoneChan:
c.log.Info("Project load complete")
return
}
}
}