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

@ -10,7 +10,6 @@ import (
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
"gopkg.in/yaml.v3"
)
@ -26,8 +25,7 @@ type Cache struct {
file string
log *pterm.Logger
path string
gitlabs *remotes.Clients
remotes *remote.Remotes
remotes *remotes.Remotes
}
type CacheOpts struct {
@ -35,8 +33,7 @@ type CacheOpts struct {
ProjectsPath string
TTL time.Duration
Logger *pterm.Logger
Gitlabs *remotes.Clients
Remotes *remote.Remotes
Remotes *remotes.Remotes
Config *config.Config
}
@ -148,7 +145,7 @@ func (c *Cache) refresh() {
// For backwards-compatibility only
c.setAliasRemotes()
// Retrieve and add/update projects
c.LoadGitlabs()
c.LoadRemotes()
}
// Iterates through all GitLab projects the user has access to, updating
@ -164,10 +161,10 @@ func (c *Cache) String() string {
c.Updated.String(),
len(c.Projects),
len(c.Aliases),
len(*c.gitlabs),
len(*c.remotes),
)
for _, r := range *c.gitlabs {
cacheString += " " + r.Config.Host
for _, r := range *c.remotes {
cacheString += " " + r.GetInfo().Host
}
return cacheString
}
@ -196,14 +193,6 @@ func NewProjectCache(opts *CacheOpts) (*Cache, error) {
err = createProjectCache(opts.Path)
}
// Combine old-and-new gitlabs
var gitlabs *remotes.Clients
if opts.Gitlabs != nil {
gitlabs = opts.Gitlabs
} else {
gitlabs = remotes.NewCLients()
}
cache := &Cache{
Projects: make([]*projects.Project, 0),
Aliases: make([]*ProjectAlias, 0),
@ -213,8 +202,7 @@ func NewProjectCache(opts *CacheOpts) (*Cache, error) {
lock: &sync.Mutex{},
contentLock: &sync.Mutex{},
log: opts.Logger,
gitlabs: gitlabs,
remotes: remote.NewRemotes(),
remotes: opts.Remotes,
path: opts.ProjectsPath,
}

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

View File

@ -31,7 +31,7 @@ func (c *Cache) OpenProject(ctx context.Context, project *projects.Project) *git
// Check to make sure we can connect before we time out
// shouldn't be necessary, but go-git does not properly
// honor its context
if err := project.CheckHost(projects.GitlabProtoSSH); err != nil {
if err := project.CheckHost(projects.GitProtoSSH); err != nil {
c.log.Fatal("Git remote unreachable, giving up", c.log.Args("error", err))
}