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

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

View File

@ -13,13 +13,16 @@ var dumpCmd = &cobra.Command{
Long: `Dumps cache to display`, Long: `Dumps cache to display`,
PreRun: runCacheCmd, PreRun: runCacheCmd,
PostRun: postCacheCmd, PostRun: postCacheCmd,
Run: func(cmd *cobra.Command, args []string) { Run: runCacheDunpCmd,
if conf.Dump.Full { }
fmt.Println(projectCache.DumpString(true, searchStringFromArgs(args)))
} else { func runCacheDunpCmd(cmd *cobra.Command, args []string) {
plog.Info(projectCache.String()) remotes := viper.GetStringSlice("remote")
} if conf.Dump.Full {
}, fmt.Println(projectCache.DumpString(true, searchStringFromArgs(args), remotes...))
} else {
plog.Info(projectCache.String())
}
} }
func init() { func init() {

View File

@ -17,7 +17,8 @@ wants to find a new project.`,
} }
func loadCache(cmd *cobra.Command, args []string) { func loadCache(cmd *cobra.Command, args []string) {
projectCache.Refresh() remotes := viper.GetStringSlice("remote")
projectCache.Refresh(remotes...)
} }
func init() { func init() {

View File

@ -20,11 +20,11 @@ var projectCmd = &cobra.Command{
} }
func getProject(args []string) *projects.Project { func getProject(args []string) *projects.Project {
gitlabs := viper.GetStringSlice("project.gitlabs") remotes := viper.GetStringSlice("remote")
fzfOpts := &fzfProjectOpts{ fzfOpts := &fzfProjectOpts{
Ctx: rootCmd.Context(), Ctx: rootCmd.Context(),
Search: searchStringFromArgs(args), Search: searchStringFromArgs(args),
Gitlabs: gitlabs, Remotes: remotes,
} }
project := fzfFindProject(fzfOpts) project := fzfFindProject(fzfOpts)
@ -57,9 +57,6 @@ func postProjectCmd(cmd *cobra.Command, args []string) {
func init() { func init() {
rootCmd.AddCommand(projectCmd) rootCmd.AddCommand(projectCmd)
projectCmd.PersistentFlags().StringArray("gitlab", []string{}, "Specify gitlab remote, provide flag multiple times")
projectCmd.RegisterFlagCompletionFunc("gitlab", validGitlabRemotesFunc)
viper.BindPFlag("project.gitlabs", projectCmd.Flag("gitlab"))
} }
func mustHaveProjects(cmd *cobra.Command, args []string) { func mustHaveProjects(cmd *cobra.Command, args []string) {

View File

@ -21,12 +21,12 @@ var projectGoCmd = &cobra.Command{
} }
func projectGoCmdRun(cmd *cobra.Command, args []string) { func projectGoCmdRun(cmd *cobra.Command, args []string) {
gitlabs := viper.GetStringSlice("project.gitlabs") remotes := viper.GetStringSlice("remote")
fzfOpts := &fzfProjectOpts{ fzfOpts := &fzfProjectOpts{
Ctx: cmd.Context(), Ctx: cmd.Context(),
Search: searchStringFromArgs(args), Search: searchStringFromArgs(args),
MustHaveAlias: true, MustHaveAlias: true,
Gitlabs: gitlabs, Remotes: remotes,
} }
project := fzfSearchProjectAliases(fzfOpts) project := fzfSearchProjectAliases(fzfOpts)

View File

@ -16,8 +16,8 @@ var projectListCmd = &cobra.Command{
} }
func projectListCmdRun(cmd *cobra.Command, args []string) { func projectListCmdRun(cmd *cobra.Command, args []string) {
gitlabs := viper.GetStringSlice("project.gitlabs") remotes := viper.GetStringSlice("remote")
fmt.Println(projectCache.DumpString(viper.GetBool("project.list.all"), searchStringFromArgs(args), gitlabs...)) fmt.Println(projectCache.DumpString(viper.GetBool("project.list.all"), searchStringFromArgs(args), remotes...))
} }
func init() { func init() {

View File

@ -51,11 +51,11 @@ func projectOpenCmdRun(cmd *cobra.Command, args []string) {
plog.Fatal("No usable editor found") plog.Fatal("No usable editor found")
} }
gitlabs := viper.GetStringSlice("project.gitlabs") remotes := viper.GetStringSlice("remote")
fzfOpts := &fzfProjectOpts{ fzfOpts := &fzfProjectOpts{
Ctx: cmd.Context(), Ctx: cmd.Context(),
Search: searchStringFromArgs(args), Search: searchStringFromArgs(args),
Gitlabs: gitlabs, Remotes: remotes,
} }
project := fzfCwdOrSearchProjectAliases(fzfOpts) project := fzfCwdOrSearchProjectAliases(fzfOpts)
if project == nil { if project == nil {

View File

@ -18,11 +18,11 @@ var projectRunCmd = &cobra.Command{
} }
func projectRunCmdRun(cmd *cobra.Command, args []string) { func projectRunCmdRun(cmd *cobra.Command, args []string) {
gitlabs := viper.GetStringSlice("project.gitlabs") remotes := viper.GetStringSlice("remote")
fzfOpts := &fzfProjectOpts{ fzfOpts := &fzfProjectOpts{
Ctx: cmd.Context(), Ctx: cmd.Context(),
Search: searchStringFromArgs(args), Search: searchStringFromArgs(args),
Gitlabs: gitlabs, Remotes: remotes,
} }
project := fzfCwdOrSearchProjectAliases(fzfOpts) project := fzfCwdOrSearchProjectAliases(fzfOpts)
if project == nil { if project == nil {

View File

@ -23,11 +23,11 @@ func projectShowCmdRun(cmd *cobra.Command, args []string) {
var project *projects.Project var project *projects.Project
var inCwd bool var inCwd bool
gitlabs := viper.GetStringSlice("project.gitlabs") remotes := viper.GetStringSlice("remote")
fzfOpts := &fzfProjectOpts{ fzfOpts := &fzfProjectOpts{
Ctx: cmd.Context(), Ctx: cmd.Context(),
Search: searchStringFromArgs(args), Search: searchStringFromArgs(args),
Gitlabs: gitlabs, Remotes: remotes,
} }
// Try to find project from current directory // Try to find project from current directory

View File

@ -10,6 +10,8 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
gitearemote "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/gitea"
gitlabremote "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/gitlab"
) )
var conf config.Config var conf config.Config
@ -47,6 +49,7 @@ func init() {
cobra.EnableTraverseRunHooks = true cobra.EnableTraverseRunHooks = true
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
// Global flags
rootCmd.PersistentFlags().String("config", "", rootCmd.PersistentFlags().String("config", "",
"config file (default is "+defConfigPath+")") "config file (default is "+defConfigPath+")")
rootCmd.PersistentFlags().String("projectPath", "", rootCmd.PersistentFlags().String("projectPath", "",
@ -56,7 +59,9 @@ func init() {
rootCmd.PersistentFlags().StringSlice("remote", []string{}, rootCmd.PersistentFlags().StringSlice("remote", []string{},
"Specify remotes by host for any sub-command. Provide multiple times or comma delimited.") "Specify remotes by host for any sub-command. Provide multiple times or comma delimited.")
// Flag autocompletion
rootCmd.RegisterFlagCompletionFunc("logLevel", validLogLevelsFunc) rootCmd.RegisterFlagCompletionFunc("logLevel", validLogLevelsFunc)
rootCmd.RegisterFlagCompletionFunc("remote", validRemotesFunc)
viper.BindPFlags(rootCmd.PersistentFlags()) viper.BindPFlags(rootCmd.PersistentFlags())
} }
@ -106,6 +111,7 @@ func initConfig() {
} }
checkConfigPerms(viper.ConfigFileUsed()) // Abort on world-readable config checkConfigPerms(viper.ConfigFileUsed()) // Abort on world-readable config
setConfigFields() // Final chance to update config struct
plog.Debug("Configuration loaded", plog.Args("conf", conf)) plog.Debug("Configuration loaded", plog.Args("conf", conf))
} }
@ -127,6 +133,17 @@ func getPtermLogLevel(level string) pterm.LogLevel {
return pLevel return pLevel
} }
// Do any post-processing of configuration here
func setConfigFields() {
// Load Gitlabs
glRemotes := gitlabremote.GitlabRemote{}
conf.Remotes = append(conf.Remotes, glRemotes.GetInfos(conf)...)
// Load Giteas
giteaRemotes := gitearemote.GiteaRemote{}
conf.Remotes = append(conf.Remotes, giteaRemotes.GetInfos(conf)...)
}
// Don't allow world-readable configuration // Don't allow world-readable configuration
func checkConfigPerms(file string) { func checkConfigPerms(file string) {
stat, err := os.Stat(file) stat, err := os.Stat(file)

View File

@ -29,22 +29,8 @@ func validProjectsOrAliasesFunc(cmd *cobra.Command, args []string, toComplete st
func validRemotesFunc(cmd *cobra.Command, args []string, toComplete string) ( func validRemotesFunc(cmd *cobra.Command, args []string, toComplete string) (
[]string, cobra.ShellCompDirective) { []string, cobra.ShellCompDirective) {
var ttlRemotes int remotes := make([]string, 0, len(conf.Remotes))
ttlRemotes += len(conf.Gitlabs) for _, remote := range conf.Remotes {
ttlRemotes += len(conf.Giteas)
remotes := make([]string, 0, ttlRemotes)
for _, remote := range conf.Gitlabs {
if strings.HasPrefix(remote.Host, toComplete) {
remotes = append(remotes, remote.Host)
}
}
return remotes, cobra.ShellCompDirectiveNoFileComp
}
func validGitlabRemotesFunc(cmd *cobra.Command, args []string, toComplete string) (
[]string, cobra.ShellCompDirective) {
remotes := make([]string, 0, len(conf.Gitlabs))
for _, remote := range conf.Gitlabs {
if strings.HasPrefix(remote.Host, toComplete) { if strings.HasPrefix(remote.Host, toComplete) {
remotes = append(remotes, remote.Host) remotes = append(remotes, remote.Host)
} }

View File

@ -13,7 +13,7 @@ type fzfProjectOpts struct {
Ctx context.Context Ctx context.Context
Search string Search string
MustHaveAlias bool MustHaveAlias bool
Gitlabs []string Remotes []string
} }
// This will try to find a project by alias if a search term // This will try to find a project by alias if a search term
@ -52,7 +52,7 @@ func fzfCwdOrSearchProjectAliases(opts *fzfProjectOpts) *projects.Project {
func fzfSearchProjectAliases(opts *fzfProjectOpts) *projects.Project { func fzfSearchProjectAliases(opts *fzfProjectOpts) *projects.Project {
var project *projects.Project var project *projects.Project
var alias *cache.ProjectAlias var alias *cache.ProjectAlias
if alias = projectCache.GetAliasByName(opts.Search, opts.Gitlabs...); alias != nil { if alias = projectCache.GetAliasByName(opts.Search, opts.Remotes...); alias != nil {
project = projectCache.GetProjectByAlias(alias) project = projectCache.GetProjectByAlias(alias)
plog.Info("Perfect alias match... flawless") plog.Info("Perfect alias match... flawless")
} else { } else {
@ -130,8 +130,8 @@ func fzfProject(opts *fzfProjectOpts) (*projects.Project, error) {
} else { } else {
searchableProjects = projectCache.Projects searchableProjects = projectCache.Projects
} }
// Filter out unwanted gitlabs if provided // Filter out unwanted remotes if provided
searchableProjects = filterProjectsWithGitlabs(searchableProjects, opts.Gitlabs...) searchableProjects = filterProjectsWithRemotes(searchableProjects, opts.Remotes...)
return fzfProjectFromProjects(opts, searchableProjects) return fzfProjectFromProjects(opts, searchableProjects)
} }
@ -162,11 +162,11 @@ func fzfPreviewWindow(i, w, h int) string {
return projectCache.ProjectString(p) return projectCache.ProjectString(p)
} }
func filterProjectsWithGitlabs(gitProjects []*projects.Project, gitlabs ...string) []*projects.Project { func filterProjectsWithRemotes(gitProjects []*projects.Project, remotes ...string) []*projects.Project {
filteredProjects := make([]*projects.Project, 0, len(gitProjects)) filteredProjects := make([]*projects.Project, 0, len(gitProjects))
if len(gitlabs) > 0 { if len(remotes) > 0 {
for _, p := range gitProjects { for _, p := range gitProjects {
if slices.Contains(gitlabs, p.Remote) { if slices.Contains(remotes, p.Remote) {
filteredProjects = append(filteredProjects, p) filteredProjects = append(filteredProjects, p)
} }
} }

View File

@ -12,7 +12,6 @@ import (
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
gitearemote "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/gitea" gitearemote "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/gitea"
gitlabremote "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/gitlab" gitlabremote "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/gitlab"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -28,12 +27,7 @@ func initProjectCache(cmd *cobra.Command, args []string) {
conf.Cache.File = conf.ProjectPath + "/.cache.yaml" conf.Cache.File = conf.ProjectPath + "/.cache.yaml"
gitRemotes := remotes.NewRemotes() gitRemotes := remotes.NewRemotes()
gitRemotes.AddRemotes(getRemotes(cmd)...)
// Load GitLabs
gitRemotes.AddRemotes(getGitLabRemotes(cmd)...)
// Load Giteas
gitRemotes.AddRemotes(getGiteaRemotes(cmd)...)
cacheOpts := &cache.CacheOpts{ cacheOpts := &cache.CacheOpts{
ProjectsPath: conf.ProjectPath, ProjectsPath: conf.ProjectPath,
@ -56,68 +50,30 @@ func initProjectCache(cmd *cobra.Command, args []string) {
plog.Debug("Remotes Loaded", plog.Args("remotes", cacheOpts.Remotes)) plog.Debug("Remotes Loaded", plog.Args("remotes", cacheOpts.Remotes))
} }
// Loads all configured giteas // Generically loads remotes from info.RemoteInfo in config.Remotes
func getGiteaRemotes(cmd *cobra.Command) []remote.Remote { func getRemotes(cmd *cobra.Command) []remote.Remote {
gitRemotes := make([]remote.Remote, 0) gitRemotes := make([]remote.Remote, 0)
for _, gitea := range conf.Giteas { for _, r := range conf.Remotes {
if gitea.CloneProto == "" { r.Ctx = cmd.Context()
gitea.CloneProto = info.DefaultCloneProto var gitRemote remote.Remote
var err error
switch r.Type {
case "gitlab":
gitRemote, err = gitlabremote.NewGitlabRemote(&r)
case "gitea":
gitRemote, err = gitearemote.NewGiteaRemote(&r)
} }
giteaRemote, err := gitearemote.NewGiteaRemote(&info.RemoteInfo{
Ctx: cmd.Context(),
Host: gitea.Host,
Name: gitea.Name,
Token: gitea.Token,
Type: "gitea",
CloneProto: gitea.CloneProto,
})
if err != nil { if err != nil {
plog.Error("Failed to prepare Gitea remote", plog.Args("error", err)) plog.Error("Failed to prepare remote", plog.Args(
"error", err,
"type", r.Type))
} else { } else {
gitRemotes = append(gitRemotes, giteaRemote) gitRemotes = append(gitRemotes, gitRemote)
} }
} }
return gitRemotes return gitRemotes
} }
// Loads all configured gitlabs, including as defined by legacy
// top-level keys
func getGitLabRemotes(cmd *cobra.Command) []remote.Remote {
gitRemotes := make([]remote.Remote, 0)
// Support legacy keys
if conf.GitlabHost != "" && conf.GitlabToken != "" {
conf.Gitlabs = append(conf.Gitlabs, config.GitlabConfig{
Host: conf.GitlabHost,
Name: conf.GitlabHost,
Token: conf.GitlabToken,
CloneProto: info.CloneProtoSSH,
})
}
// Load Gitlabs
for _, gl := range conf.Gitlabs {
if gl.CloneProto == "" {
gl.CloneProto = info.DefaultCloneProto
}
gitlabRemote, err := gitlabremote.NewGitlabRemote(&info.RemoteInfo{
Ctx: cmd.Context(),
Host: gl.Host,
Name: gl.Name,
Token: gl.Token,
Type: "gitlab",
CloneProto: gl.CloneProto,
})
if err != nil {
plog.Error("Failed to prepare GitLab remote", plog.Args("error", err))
} else {
gitRemotes = append(gitRemotes, gitlabRemote)
}
}
return gitRemotes
}
func postProjectCache(cmd *cobra.Command, args []string) { func postProjectCache(cmd *cobra.Command, args []string) {
projectCache.Write() projectCache.Write()
} }

View File

@ -17,7 +17,7 @@ type Cache struct {
Projects []*projects.Project Projects []*projects.Project
Aliases []*ProjectAlias Aliases []*ProjectAlias
Updated time.Time Updated time.Time
Remotes *remotes.Remotes remotes *remotes.Remotes
config *config.Config config *config.Config
readFromFile bool readFromFile bool
lock *sync.Mutex // Lock the entire cache lock *sync.Mutex // Lock the entire cache
@ -138,22 +138,22 @@ func (c *Cache) Clear(clearAliases bool) {
c.clear(clearAliases) 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") c.log.Info("Loading project cache, this may take a while\n")
defer c.setUpdated() defer c.setUpdated()
// Fix any dangling aliases // Fix any dangling aliases
// For backwards-compatibility only // For backwards-compatibility only
c.setAliasRemotes() c.setAliasRemotes()
// Retrieve and add/update projects // Retrieve and add/update projects
c.LoadRemotes() c.LoadRemotes(remotes...)
} }
// Iterates through all GitLab projects the user has access to, updating // Iterates through all GitLab projects the user has access to, updating
// the project cache where necessary // the project cache where necessary
func (c *Cache) Refresh() { func (c *Cache) Refresh(remotes ...string) {
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() defer c.lock.Unlock()
c.refresh() c.refresh(remotes...)
} }
func (c *Cache) String() string { func (c *Cache) String() string {
@ -161,9 +161,9 @@ func (c *Cache) String() string {
c.Updated.String(), c.Updated.String(),
len(c.Projects), len(c.Projects),
len(c.Aliases), len(c.Aliases),
len(*c.Remotes), len(*c.remotes),
) )
for _, r := range *c.Remotes { for _, r := range *c.remotes {
cacheString += " " + r.GetInfo().Host cacheString += " " + r.GetInfo().Host
} }
return cacheString return cacheString
@ -202,7 +202,7 @@ func NewProjectCache(opts *CacheOpts) (*Cache, error) {
lock: &sync.Mutex{}, lock: &sync.Mutex{},
contentLock: &sync.Mutex{}, contentLock: &sync.Mutex{},
log: opts.Logger, log: opts.Logger,
Remotes: opts.Remotes, remotes: opts.Remotes,
path: opts.ProjectsPath, 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"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote" "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{} wg := &sync.WaitGroup{}
writer := pterm.DefaultMultiPrinter 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) { if !remote.IsAlive(r) {
c.log.Error("Skipping load of remote, not alive", c.log.Args( 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(), "remote", r.String(),
)) "remotes", gitRemotes))
continue continue
} }

View File

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

View File

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

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"code.gitea.io/sdk/gitea" "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" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
) )
@ -12,6 +13,30 @@ type GiteaRemote struct {
api *gitea.Client 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 { func (r *GiteaRemote) GetInfo() *info.RemoteInfo {
return r.info return r.info
} }

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/xanzy/go-gitlab" "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" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
) )
@ -12,6 +13,40 @@ type GitlabRemote struct {
api *gitlab.Client 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 { func (r *GitlabRemote) GetInfo() *info.RemoteInfo {
return r.info return r.info
} }

View File

@ -6,6 +6,7 @@ import (
"net/url" "net/url"
"time" "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/info"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load" "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 // stream all projects along with updates to channels
// provided by *load.ProgressInfo // provided by *load.ProgressInfo
type Remote interface { 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 String() string // String info for remote
GetInfo() *info.RemoteInfo // Returns basic RemoteInfo struct GetInfo() *info.RemoteInfo // Returns basic RemoteInfo struct
GetType() string // Returns the remote type (e.g. GitLab, Gitea) GetType() string // Returns the remote type (e.g. GitLab, Gitea)