git-project-manager/internal/remotes/remotes.go

58 lines
1.6 KiB
Go

package remotes
import (
"errors"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
)
// Will determine number of total projects,
// then based on projectsPerPage (request) and
// projectsPerGoroutine, will spin off goroutines
// with offsets
const (
projectsPerPage = 20
projectsPerGoroutine = 200
)
type Remotes []remote.Remote
func (r *Remotes) AddRemotes(remote ...remote.Remote) {
*r = append(*r, remote...)
}
func (r *Remotes) GetRemoteByHost(host string) remote.Remote {
for _, remote := range *r {
if remote.GetInfo().Host == host {
return remote
}
}
return nil
}
// Launches project streamsers for all remotes in goroutines
// returns slice of load.ProgressInfo, does not block, streamer is
// launched in goroutine
func StreamRemote(r remote.Remote, opts *remote.RemoteQueryOpts) (*load.ProgressInfo, error) {
progressInfo := &load.ProgressInfo{
ProgressChan: make(chan load.Progress),
ProjectsChan: make(chan []*projects.Project),
ErrorChan: make(chan error),
DoneChan: make(chan interface{}),
NumProjects: r.GetNumProjects(opts),
}
if progressInfo.NumProjects < 1 {
return nil, errors.New("Failed to fetch project count from remote, won't load")
}
go r.StreamProjects(progressInfo, opts)
return progressInfo, nil
}
func NewRemotes() *Remotes {
var remotes Remotes
remotes = make([]remote.Remote, 0)
return &remotes
}