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`,
PreRun: runCacheCmd,
PostRun: postCacheCmd,
Run: func(cmd *cobra.Command, args []string) {
if conf.Dump.Full {
fmt.Println(projectCache.DumpString(true, searchStringFromArgs(args)))
} else {
plog.Info(projectCache.String())
}
},
Run: runCacheDunpCmd,
}
func runCacheDunpCmd(cmd *cobra.Command, args []string) {
remotes := viper.GetStringSlice("remote")
if conf.Dump.Full {
fmt.Println(projectCache.DumpString(true, searchStringFromArgs(args), remotes...))
} else {
plog.Info(projectCache.String())
}
}
func init() {

View File

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

View File

@ -20,11 +20,11 @@ var projectCmd = &cobra.Command{
}
func getProject(args []string) *projects.Project {
gitlabs := viper.GetStringSlice("project.gitlabs")
remotes := viper.GetStringSlice("remote")
fzfOpts := &fzfProjectOpts{
Ctx: rootCmd.Context(),
Search: searchStringFromArgs(args),
Gitlabs: gitlabs,
Remotes: remotes,
}
project := fzfFindProject(fzfOpts)
@ -57,9 +57,6 @@ func postProjectCmd(cmd *cobra.Command, args []string) {
func init() {
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) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -10,6 +10,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"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
@ -47,6 +49,7 @@ func init() {
cobra.EnableTraverseRunHooks = true
cobra.OnInitialize(initConfig)
// Global flags
rootCmd.PersistentFlags().String("config", "",
"config file (default is "+defConfigPath+")")
rootCmd.PersistentFlags().String("projectPath", "",
@ -56,7 +59,9 @@ func init() {
rootCmd.PersistentFlags().StringSlice("remote", []string{},
"Specify remotes by host for any sub-command. Provide multiple times or comma delimited.")
// Flag autocompletion
rootCmd.RegisterFlagCompletionFunc("logLevel", validLogLevelsFunc)
rootCmd.RegisterFlagCompletionFunc("remote", validRemotesFunc)
viper.BindPFlags(rootCmd.PersistentFlags())
}
@ -106,6 +111,7 @@ func initConfig() {
}
checkConfigPerms(viper.ConfigFileUsed()) // Abort on world-readable config
setConfigFields() // Final chance to update config struct
plog.Debug("Configuration loaded", plog.Args("conf", conf))
}
@ -127,6 +133,17 @@ func getPtermLogLevel(level string) pterm.LogLevel {
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
func checkConfigPerms(file string) {
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) (
[]string, cobra.ShellCompDirective) {
var ttlRemotes int
ttlRemotes += len(conf.Gitlabs)
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 {
remotes := make([]string, 0, len(conf.Remotes))
for _, remote := range conf.Remotes {
if strings.HasPrefix(remote.Host, toComplete) {
remotes = append(remotes, remote.Host)
}

View File

@ -13,7 +13,7 @@ type fzfProjectOpts struct {
Ctx context.Context
Search string
MustHaveAlias bool
Gitlabs []string
Remotes []string
}
// 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 {
var project *projects.Project
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)
plog.Info("Perfect alias match... flawless")
} else {
@ -130,8 +130,8 @@ func fzfProject(opts *fzfProjectOpts) (*projects.Project, error) {
} else {
searchableProjects = projectCache.Projects
}
// Filter out unwanted gitlabs if provided
searchableProjects = filterProjectsWithGitlabs(searchableProjects, opts.Gitlabs...)
// Filter out unwanted remotes if provided
searchableProjects = filterProjectsWithRemotes(searchableProjects, opts.Remotes...)
return fzfProjectFromProjects(opts, searchableProjects)
}
@ -162,11 +162,11 @@ func fzfPreviewWindow(i, w, h int) string {
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))
if len(gitlabs) > 0 {
if len(remotes) > 0 {
for _, p := range gitProjects {
if slices.Contains(gitlabs, p.Remote) {
if slices.Contains(remotes, p.Remote) {
filteredProjects = append(filteredProjects, p)
}
}

View File

@ -12,7 +12,6 @@ import (
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
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"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
"golang.org/x/sys/unix"
)
@ -28,12 +27,7 @@ func initProjectCache(cmd *cobra.Command, args []string) {
conf.Cache.File = conf.ProjectPath + "/.cache.yaml"
gitRemotes := remotes.NewRemotes()
// Load GitLabs
gitRemotes.AddRemotes(getGitLabRemotes(cmd)...)
// Load Giteas
gitRemotes.AddRemotes(getGiteaRemotes(cmd)...)
gitRemotes.AddRemotes(getRemotes(cmd)...)
cacheOpts := &cache.CacheOpts{
ProjectsPath: conf.ProjectPath,
@ -56,68 +50,30 @@ func initProjectCache(cmd *cobra.Command, args []string) {
plog.Debug("Remotes Loaded", plog.Args("remotes", cacheOpts.Remotes))
}
// Loads all configured giteas
func getGiteaRemotes(cmd *cobra.Command) []remote.Remote {
// Generically loads remotes from info.RemoteInfo in config.Remotes
func getRemotes(cmd *cobra.Command) []remote.Remote {
gitRemotes := make([]remote.Remote, 0)
for _, gitea := range conf.Giteas {
if gitea.CloneProto == "" {
gitea.CloneProto = info.DefaultCloneProto
for _, r := range conf.Remotes {
r.Ctx = cmd.Context()
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 {
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 {
gitRemotes = append(gitRemotes, giteaRemote)
gitRemotes = append(gitRemotes, gitRemote)
}
}
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) {
projectCache.Write()
}

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)