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

@ -101,6 +101,17 @@ func promptConfigSettings(c *config.Config) *config.Config {
gitlabConfig.Token = token
}
if proto, err := pterm.DefaultInteractiveSelect.
WithOptions([]string{string(config.CloneProtoHTTP), string(config.CloneProtoSSH)}).
WithDefaultText("Git Clone Protocol").
Show(); err == nil {
if proto == "ssh" {
gitlabConfig.CloneProto = config.CloneProtoSSH
} else {
gitlabConfig.CloneProto = config.CloneProtoHTTP
}
}
if pPath, err := pterm.DefaultInteractiveTextInput.
WithDefaultValue(c.ProjectPath).
WithDefaultText("Enter path for projects and cache").

View File

@ -10,6 +10,8 @@ import (
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/cache"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
gitlabremote "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/gitlab"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
"golang.org/x/sys/unix"
)
@ -19,52 +21,19 @@ import (
// func from their PersistentPreRun commands
func initProjectCache(cmd *cobra.Command, args []string) {
var err error
plog.Debug("Running pre-run for cacheCmd")
conf.Cache.File = conf.ProjectPath + "/.cache.yaml"
// Backwards-compatible support for singular instance
opts := make([]*remotes.ClientOpts, 0)
if conf.GitlabHost != "" {
opts = append(opts, &remotes.ClientOpts{
Ctx: cmd.Context(),
Host: conf.GitlabHost, // deprecated, switch to gitlabs
Token: conf.GitlabToken, // deprecated, switch to gitlabs
Name: conf.GitlabHost, // not originally supported, use the new gitlabs field
})
}
// If defined, load additional instances
for _, g := range conf.Gitlabs {
opts = append(opts, &remotes.ClientOpts{
Ctx: cmd.Context(),
Name: g.Name,
Host: g.Host,
Token: g.Token,
})
}
// We need at least one GitLab
if len(opts) < 1 {
plog.Error("At least one GitLab must be configured. Add to .gitlabs in your config file")
os.Exit(1)
}
// Load all gitlab configs into clients
var gitlabs *remotes.Clients
var err error
gitlabs, err = remotes.NewGitlabClients(opts)
if err != nil {
plog.Error("Failed to create GitLab clients", plog.Args("error", err))
os.Exit(1)
}
gitRemotes := remotes.NewRemotes()
gitRemotes.AddRemotes(getGitLabRemotes(cmd)...)
cacheOpts := &cache.CacheOpts{
ProjectsPath: conf.ProjectPath,
Path: conf.Cache.File,
TTL: conf.Cache.Ttl,
Logger: plog,
Gitlabs: gitlabs,
Remotes: gitRemotes,
Config: &conf,
}
if projectCache, err = cache.NewProjectCache(cacheOpts); err != nil {
@ -77,7 +46,39 @@ func initProjectCache(cmd *cobra.Command, args []string) {
os.Exit(1)
}
plog.Debug("Gitlab Clients", plog.Args("gitlabs", cacheOpts.Gitlabs))
plog.Debug("Remotes Loaded", plog.Args("remotes", cacheOpts.Remotes))
}
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: config.CloneProtoSSH,
})
}
// Load Gitlabs
for _, gl := range conf.Gitlabs {
gitlabRemote, err := gitlabremote.NewGitlabRemote(&remote.RemoteInfo{
Ctx: cmd.Context(),
Host: gl.Host,
Name: gl.Name,
Token: gl.Token,
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) {