Move from --gitlab flags to global --remote flags

This commit is contained in:
2024-01-17 10:06:20 -05:00
parent f33199bd7b
commit 702f599f5f
19 changed files with 152 additions and 121 deletions

View File

@ -17,7 +17,7 @@ type Cache struct {
Projects []*projects.Project
Aliases []*ProjectAlias
Updated time.Time
Remotes *remotes.Remotes
remotes *remotes.Remotes
config *config.Config
readFromFile bool
lock *sync.Mutex // Lock the entire cache
@ -138,22 +138,22 @@ func (c *Cache) Clear(clearAliases bool) {
c.clear(clearAliases)
}
func (c *Cache) refresh() {
func (c *Cache) refresh(remotes ...string) {
c.log.Info("Loading project cache, this may take a while\n")
defer c.setUpdated()
// Fix any dangling aliases
// For backwards-compatibility only
c.setAliasRemotes()
// Retrieve and add/update projects
c.LoadRemotes()
c.LoadRemotes(remotes...)
}
// Iterates through all GitLab projects the user has access to, updating
// the project cache where necessary
func (c *Cache) Refresh() {
func (c *Cache) Refresh(remotes ...string) {
c.lock.Lock()
defer c.lock.Unlock()
c.refresh()
c.refresh(remotes...)
}
func (c *Cache) String() string {
@ -161,9 +161,9 @@ func (c *Cache) String() string {
c.Updated.String(),
len(c.Projects),
len(c.Aliases),
len(*c.Remotes),
len(*c.remotes),
)
for _, r := range *c.Remotes {
for _, r := range *c.remotes {
cacheString += " " + r.GetInfo().Host
}
return cacheString
@ -202,7 +202,7 @@ func NewProjectCache(opts *CacheOpts) (*Cache, error) {
lock: &sync.Mutex{},
contentLock: &sync.Mutex{},
log: opts.Logger,
Remotes: opts.Remotes,
remotes: opts.Remotes,
path: opts.ProjectsPath,
}

View File

@ -8,17 +8,24 @@ import (
"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"
"golang.org/x/exp/slices"
)
func (c *Cache) LoadRemotes() {
func (c *Cache) LoadRemotes(gitRemotes ...string) {
wg := &sync.WaitGroup{}
writer := pterm.DefaultMultiPrinter
for _, r := range *c.Remotes {
for _, r := range *c.remotes {
// Don't bother if it's dead or the user has provided
// one or more remotes via --remote flag(s)
if !remote.IsAlive(r) {
c.log.Error("Skipping load of remote, not alive", c.log.Args(
"remote", r.String()))
continue
} else if !slices.Contains(gitRemotes, r.GetInfo().Host) {
c.log.Debug("Skipping remote not in --remote list", c.log.Args(
"remote", r.String(),
))
"remotes", gitRemotes))
continue
}

View File

@ -43,7 +43,7 @@ func (c *Cache) AddProjects(gitProjects ...*projects.Project) {
// This command will only dump projects that have
// been cloned locally. Setting all to true will list all projects
func (c *Cache) DumpString(all bool, search string, gitlabs ...string) string {
func (c *Cache) DumpString(all bool, search string, remotes ...string) string {
str := strings.Builder{}
var term string
if all {
@ -56,7 +56,7 @@ func (c *Cache) DumpString(all bool, search string, gitlabs ...string) string {
for _, project := range projects {
if !all && !c.IsProjectCloned(project) {
continue
} else if len(gitlabs) > 0 && !slices.Contains(gitlabs, project.Remote) {
} else if len(remotes) > 0 && !slices.Contains(remotes, project.Remote) {
continue
}
str.WriteString(" - " + pterm.FgLightBlue.Sprint(project.Name) + " (")

View File

@ -71,9 +71,9 @@ func (c *Cache) AliasStrings(prefix string) []string {
return aliases
}
func (c *Cache) GetAliasByName(name string, gitlabs ...string) *ProjectAlias {
func (c *Cache) GetAliasByName(name string, remotes ...string) *ProjectAlias {
for _, a := range c.Aliases {
if len(gitlabs) > 0 && !slices.Contains(gitlabs, a.Remote) {
if len(remotes) > 0 && !slices.Contains(remotes, a.Remote) {
continue
}
if name == a.Alias {

View File

@ -4,6 +4,7 @@ import (
"fmt"
"code.gitea.io/sdk/gitea"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
)
@ -12,6 +13,30 @@ type GiteaRemote struct {
api *gitea.Client
}
func (r *GiteaRemote) GetInfos(conf config.Config) []info.RemoteInfo {
// Prepare infos
infos := make([]info.RemoteInfo, len(conf.Giteas))
for i, g := range conf.Giteas {
// Set Defaults
proto := info.CloneProtoSSH
if g.CloneProto == info.CloneProtoHTTP {
proto = info.CloneProtoHTTP
}
if g.Name == "" {
g.Name = g.Host
}
infos[i] = info.RemoteInfo{
Host: g.Host,
Name: g.Name,
Type: "gitea",
Token: g.Token,
CloneProto: proto,
}
}
return infos
}
func (r *GiteaRemote) GetInfo() *info.RemoteInfo {
return r.info
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/xanzy/go-gitlab"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
)
@ -12,6 +13,40 @@ type GitlabRemote struct {
api *gitlab.Client
}
func (r *GitlabRemote) GetInfos(conf config.Config) []info.RemoteInfo {
// Support legacy fields
if conf.GitlabHost != "" && conf.GitlabToken != "" {
conf.Gitlabs = append(conf.Gitlabs, config.GitlabConfig{
Host: conf.GitlabHost,
Name: conf.GitlabHost,
Token: conf.GitlabToken,
CloneProto: info.CloneProtoSSH,
})
}
// Prepare infos
infos := make([]info.RemoteInfo, len(conf.Gitlabs))
for i, g := range conf.Gitlabs {
// Set Defaults
proto := info.CloneProtoSSH
if g.CloneProto == info.CloneProtoHTTP {
proto = info.CloneProtoHTTP
}
if g.Name == "" {
g.Name = g.Host
}
infos[i] = info.RemoteInfo{
Host: g.Host,
Name: g.Name,
Type: "gitlab",
Token: g.Token,
CloneProto: proto,
}
}
return infos
}
func (r *GitlabRemote) GetInfo() *info.RemoteInfo {
return r.info
}

View File

@ -6,6 +6,7 @@ import (
"net/url"
"time"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load"
)
@ -17,6 +18,9 @@ const defNetDialTimeoutSecs = 3
// stream all projects along with updates to channels
// provided by *load.ProgressInfo
type Remote interface {
// Helper to process configs.
// Realistically, conf.Gitlabs and conf.Giteas should be conf.Remotes
GetInfos(config.Config) []info.RemoteInfo
String() string // String info for remote
GetInfo() *info.RemoteInfo // Returns basic RemoteInfo struct
GetType() string // Returns the remote type (e.g. GitLab, Gitea)