Move RemoteInfo and CloneProto to info package

This commit is contained in:
2024-01-17 08:13:58 -05:00
parent 1e4e9147f1
commit f33199bd7b
12 changed files with 100 additions and 58 deletions

View File

@ -17,6 +17,7 @@ type Cache struct {
Projects []*projects.Project
Aliases []*ProjectAlias
Updated time.Time
Remotes *remotes.Remotes
config *config.Config
readFromFile bool
lock *sync.Mutex // Lock the entire cache
@ -25,7 +26,6 @@ type Cache struct {
file string
log *pterm.Logger
path string
remotes *remotes.Remotes
}
type CacheOpts struct {
@ -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

@ -14,7 +14,7 @@ func (c *Cache) LoadRemotes() {
wg := &sync.WaitGroup{}
writer := pterm.DefaultMultiPrinter
for _, r := range *c.remotes {
for _, r := range *c.Remotes {
if !remote.IsAlive(r) {
c.log.Error("Skipping load of remote, not alive", c.log.Args(
"remote", r.String(),

View File

@ -1,6 +1,10 @@
package config
import "time"
import (
"time"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
)
type Config struct {
// Named keys above maintained for backwards compatibility
@ -9,9 +13,10 @@ type Config struct {
GitlabToken string `yaml:"gitlabToken,omitempty" json:"gitlabToken,omitempty"`
Gitlabs []GitlabConfig `yaml:"gitlabs" json:"gitlabs"`
Giteas []GiteaConfig `yaml:"giteas" json:"giteas"`
LogLevel string `yaml:"logLevel" json:"logLevel" enum:"info,warn,debug,error"`
ProjectPath string `yaml:"projectPath" json:"projectPath"`
Cache cacheConfig `yaml:"cache" json:"cache"`
Remotes []info.RemoteInfo
LogLevel string `yaml:"logLevel" json:"logLevel" enum:"info,warn,debug,error"`
ProjectPath string `yaml:"projectPath" json:"projectPath"`
Cache cacheConfig `yaml:"cache" json:"cache"`
Dump struct {
Full bool `yaml:"full" json:"full"`
} `yaml:"dump" json:"dump"`
@ -19,27 +24,19 @@ type Config struct {
}
type GiteaConfig struct {
Host string `yaml:"host" json:"host"`
Name string `yaml:"name" json:"name"`
Token string `yaml:"token" json:"token"`
CloneProto CloneProto `yaml:"cloneProto" json:"cloneProto"`
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 CloneProto `yaml:"cloneProto" json:"cloneProto"`
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 CloneProto string
const (
CloneProtoSSH CloneProto = "ssh"
CloneProtoHTTP CloneProto = "http"
DefaultCloneProto CloneProto = CloneProtoSSH
)
type editorConfig struct {
DisplayName string `yaml:"displanName,omitempty" json:"displanName,omitempty"`
Binary string `yaml:"binary,omitempty" json:"binary,omitempty"`
@ -69,7 +66,7 @@ var DefaultConfig = Config{
Gitlabs: []GitlabConfig{{
Host: "https://gitlab.com",
Token: "yourtokenhere",
CloneProto: CloneProtoSSH,
CloneProto: info.CloneProtoSSH,
Name: "GitLab",
}},
Cache: cacheConfig{

View File

@ -4,24 +4,28 @@ import (
"fmt"
"code.gitea.io/sdk/gitea"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
)
type GiteaRemote struct {
info *remote.RemoteInfo
info *info.RemoteInfo
api *gitea.Client
}
func (r *GiteaRemote) GetInfo() *remote.RemoteInfo {
func (r *GiteaRemote) GetInfo() *info.RemoteInfo {
return r.info
}
func (r *GiteaRemote) GetType() string {
return r.info.Type
}
func (r *GiteaRemote) String() string {
return fmt.Sprintf("Gitea %s (%s), clone proto %s",
r.GetInfo().Name, r.GetInfo().Host, r.GetInfo().CloneProto)
}
func NewGiteaRemote(remoteInfo *remote.RemoteInfo) (*GiteaRemote, error) {
func NewGiteaRemote(remoteInfo *info.RemoteInfo) (*GiteaRemote, error) {
client, err := gitea.NewClient(remoteInfo.Host,
gitea.SetContext(remoteInfo.Ctx),
gitea.SetToken(remoteInfo.Token),

View File

@ -4,24 +4,28 @@ import (
"fmt"
"github.com/xanzy/go-gitlab"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
)
type GitlabRemote struct {
info *remote.RemoteInfo
info *info.RemoteInfo
api *gitlab.Client
}
func (r *GitlabRemote) GetInfo() *remote.RemoteInfo {
func (r *GitlabRemote) GetInfo() *info.RemoteInfo {
return r.info
}
func (r *GitlabRemote) GetType() string {
return r.info.Type
}
func (r *GitlabRemote) String() string {
return fmt.Sprintf("GitLab %s (%s), clone proto %s",
r.GetInfo().Name, r.GetInfo().Host, r.GetInfo().CloneProto)
}
func NewGitlabRemote(remoteInfo *remote.RemoteInfo) (*GitlabRemote, error) {
func NewGitlabRemote(remoteInfo *info.RemoteInfo) (*GitlabRemote, error) {
api, err := NewGitlabApi(remoteInfo)
if err != nil {
return nil, err

View File

@ -5,6 +5,7 @@ import (
"github.com/pterm/pterm"
"github.com/xanzy/go-gitlab"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
)
@ -22,10 +23,10 @@ var (
defApiWaitMax time.Duration = 5 * time.Second
)
func NewGitlabApi(info *remote.RemoteInfo) (*gitlab.Client, error) {
func NewGitlabApi(remoteInfo *info.RemoteInfo) (*gitlab.Client, error) {
client, err := gitlab.NewClient(
info.Token,
gitlab.WithBaseURL(info.Host),
remoteInfo.Token,
gitlab.WithBaseURL(remoteInfo.Host),
gitlab.WithCustomRetryWaitMinMax(defApiWaitMin, defApiWaitMax),
)
if err != nil {

View File

@ -0,0 +1,24 @@
package info
import (
"context"
)
type CloneProto string
const (
CloneProtoSSH CloneProto = "ssh"
CloneProtoHTTP CloneProto = "http"
DefaultCloneProto CloneProto = CloneProtoSSH
)
// Globally shared info for all remote types
// Stub package to prevent import cycle
type RemoteInfo struct {
Ctx context.Context
Host string
Name string
Token string
Type string
CloneProto CloneProto
}

View File

@ -1,24 +1,15 @@
package remote
import (
"context"
"fmt"
"net"
"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"
)
type RemoteInfo struct {
Ctx context.Context
Host string
Name string
Token string
CloneProto config.CloneProto
}
const defNetDialTimeoutSecs = 3
// Any remote needs to be able to return
@ -26,8 +17,9 @@ const defNetDialTimeoutSecs = 3
// stream all projects along with updates to channels
// provided by *load.ProgressInfo
type Remote interface {
GetInfo() *RemoteInfo // Returns basic RemoteInfo struct
String() string // String info for remote
GetInfo() *info.RemoteInfo // Returns basic RemoteInfo struct
GetType() string // Returns the remote type (e.g. GitLab, Gitea)
GetNumProjects(*RemoteQueryOpts) int // Returns total number of accessible projects
StreamProjects(*load.ProgressInfo, *RemoteQueryOpts) // Streams projects to chans provided in load.ProgressInfo
}
@ -35,9 +27,9 @@ type Remote interface {
func IsAlive(remote Remote) bool {
var port int
switch remote.GetInfo().CloneProto {
case config.CloneProtoHTTP:
case info.CloneProtoHTTP:
port = 443
case config.CloneProtoSSH:
case info.CloneProtoSSH:
port = 22
}