Remove all legacy config

This commit is contained in:
2024-01-17 16:56:41 -05:00
parent 1c373ff00f
commit 4b389fca1c
13 changed files with 101 additions and 142 deletions

View File

@ -35,7 +35,7 @@ func (c *Cache) LoadRemotes(gitRemotes ...string) {
))
opts := &remote.RemoteQueryOpts{
Ctx: r.GetInfo().Ctx,
Ctx: r.GetInfo().Context(),
OwnerOnly: c.config.Cache.Load.OwnerOnly,
}
@ -76,8 +76,8 @@ func (c *Cache) ReceiveRemoteStream(remote remote.Remote, wg *sync.WaitGroup, pB
c.AddProjects(p...)
case e := <-progressInfo.ErrorChan:
c.log.Error("Fetch projects error", c.log.Args("error", e, "remote", remote.GetInfo().Name))
case <-remote.GetInfo().Ctx.Done():
c.log.Warn("LoadProjects cancelled", c.log.Args("reason", remote.GetInfo().Ctx.Err()))
case <-remote.GetInfo().Context().Done():
c.log.Warn("LoadProjects cancelled", c.log.Args("reason", remote.GetInfo().Context().Err()))
return
case <-progressInfo.DoneChan:
return

View File

@ -9,10 +9,6 @@ import (
type Config struct {
// Named keys above maintained for backwards compatibility
// Ideally only Gitlabs is used
GitlabHost string `yaml:"gitlabHost,omitempty" json:"gitlabHost,omitempty"`
GitlabToken string `yaml:"gitlabToken,omitempty" json:"gitlabToken,omitempty"`
Gitlabs []GitlabConfig `yaml:"gitlabs" json:"gitlabs"`
Giteas []GiteaConfig `yaml:"giteas" json:"giteas"`
Remotes []info.RemoteInfo
LogLevel string `yaml:"logLevel" json:"logLevel" enum:"info,warn,debug,error"`
ProjectPath string `yaml:"projectPath" json:"projectPath"`
@ -23,20 +19,6 @@ type Config struct {
Editor editorConfig `yaml:"editor" json:"editor"`
}
type GiteaConfig struct {
Host string `yaml:"host" json:"host"`
Name string `yaml:"name" json:"name"`
Token string `yaml:"token" json:"token"`
CloneProto info.CloneProto `yaml:"cloneProto" json:"cloneProto"`
}
type GitlabConfig struct {
Host string `yaml:"host" json:"host"`
Name string `yaml:"name" json:"name"`
Token string `yaml:"token" json:"token"`
CloneProto info.CloneProto `yaml:"cloneProto" json:"cloneProto"`
}
type editorConfig struct {
DisplayName string `yaml:"displanName,omitempty" json:"displanName,omitempty"`
Binary string `yaml:"binary,omitempty" json:"binary,omitempty"`
@ -63,11 +45,12 @@ type cacheConfig struct {
var DefaultConfig = Config{
LogLevel: "warn",
ProjectPath: "~/work/projects",
Gitlabs: []GitlabConfig{{
Remotes: []info.RemoteInfo{{
Host: "https://gitlab.com",
Token: "yourtokenhere",
CloneProto: info.CloneProtoSSH,
Name: "GitLab",
Type: "gitlab",
}},
Cache: cacheConfig{
Ttl: 168 * time.Hour,

View File

@ -4,7 +4,6 @@ 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"
)
@ -13,36 +12,12 @@ 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
}
func (r *GiteaRemote) GetType() string {
return r.info.Type
return r.info.Type.String()
}
func (r *GiteaRemote) String() string {
@ -52,7 +27,7 @@ func (r *GiteaRemote) String() string {
func NewGiteaRemote(remoteInfo *info.RemoteInfo) (*GiteaRemote, error) {
client, err := gitea.NewClient(remoteInfo.Host,
gitea.SetContext(remoteInfo.Ctx),
gitea.SetContext(remoteInfo.Context()),
gitea.SetToken(remoteInfo.Token),
)

View File

@ -4,7 +4,6 @@ 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"
)
@ -13,46 +12,12 @@ 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
}
func (r *GitlabRemote) GetType() string {
return r.info.Type
return r.info.Type.String()
}
func (r *GitlabRemote) String() string {

View File

@ -120,6 +120,6 @@ func (r *GitlabRemote) GetProjectLanguages(project *gitlab.Project) *projects.Pr
func (r *GitlabRemote) GetDefaultRequestOptions() []gitlab.RequestOptionFunc {
requestOpts := make([]gitlab.RequestOptionFunc, 1)
requestOpts[0] = gitlab.WithContext(r.GetInfo().Ctx)
requestOpts[0] = gitlab.WithContext(r.GetInfo().Context())
return requestOpts
}

View File

@ -54,7 +54,7 @@ func (r *GitlabRemote) StreamProjects(pi *load.ProgressInfo, opts *remote.Remote
// We're done when we have it all or our context is done
// or we've hit our total pages
if r.info.Ctx.Err() != nil || resp.NextPage == 0 {
if r.info.Context().Err() != nil || resp.NextPage == 0 {
break
} else if opts.Page == endPage {
break

View File

@ -12,13 +12,58 @@ const (
DefaultCloneProto CloneProto = CloneProtoSSH
)
type RemoteType string
type RemoteTypes []RemoteType
// Register remote types here and also add a case
// to func GetRemoteTypeFromString
var (
RemoteTypeGitlab RemoteType = "gitlab"
RemoteTypeGitea RemoteType = "gitea"
RemoteTypesAll RemoteTypes = []RemoteType{
RemoteTypeGitea,
RemoteTypeGitlab,
}
)
func GetRemoteTypeFromString(remoteType string) RemoteType {
var rt RemoteType
switch remoteType {
case RemoteTypeGitea.String():
rt = RemoteTypeGitea
case RemoteTypeGitlab.String():
rt = RemoteTypeGitlab
}
return rt
}
func (rt *RemoteTypes) Strings() []string {
rtStrings := make([]string, len(*rt))
for i, t := range *rt {
rtStrings[i] = string(t)
}
return rtStrings
}
func (rt *RemoteType) String() string {
return string(*rt)
}
// Globally shared info for all remote types
// Stub package to prevent import cycle
type RemoteInfo struct {
Ctx context.Context // Base context for all API calls
Host string // Host as URL with protocol (e.g. https://gitlab.com)
Name string // Human-friendly name for remote
Token string // API token for remote
Type string // Remote type (e.g. gitlab, gitea)
CloneProto CloneProto // CloneProto (ssh or http) determines what url to use for git clone
Host string // Host as URL with protocol (e.g. https://gitlab.com)
Name string // Human-friendly name for remote
Token string // API token for remote
Type RemoteType // Remote type (e.g. gitlab, gitea)
CloneProto CloneProto // CloneProto (ssh or http) determines what url to use for git clone
ctx context.Context
}
func (ri *RemoteInfo) SetContext(ctx context.Context) {
ri.ctx = ctx
}
func (ri *RemoteInfo) Context() context.Context {
return ri.ctx
}

View File

@ -6,7 +6,6 @@ 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"
)
@ -18,9 +17,6 @@ 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)