Migrate to remotes interface
This commit is contained in:
@ -18,6 +18,7 @@ func NewGitlabApi(info *remote.RemoteInfo) (*gitlab.Client, error) {
|
||||
func (r *GitlabRemote) GetNumProjects(opts *remote.RemoteQueryOpts) int {
|
||||
listOpts := *DefaultListOpts
|
||||
listOpts.PerPage = 1
|
||||
listOpts.Page = 1
|
||||
listOpts.Simple = gitlab.Ptr[bool](true)
|
||||
_, resp, err := r.api.Projects.ListProjects(&listOpts)
|
||||
if err != nil {
|
||||
|
@ -1,8 +1,6 @@
|
||||
package load
|
||||
|
||||
import (
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
|
||||
)
|
||||
import "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
|
||||
|
||||
// This package provides structs that serve
|
||||
// as the interface between remotes, and any code
|
||||
|
@ -13,22 +13,22 @@ import (
|
||||
|
||||
const defNetDialTimeoutSecs = 5
|
||||
|
||||
type GitlabProto int
|
||||
type GitProto int
|
||||
|
||||
const (
|
||||
GitlabProtoSSH GitlabProto = iota
|
||||
GitlabProtoHTTP
|
||||
GitProtoSSH GitProto = iota
|
||||
GitProtoHTTP
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnknownHost error = errors.New("No addresses found for host")
|
||||
)
|
||||
|
||||
func (p *Project) CheckHost(proto GitlabProto) error {
|
||||
func (p *Project) CheckHost(proto GitProto) error {
|
||||
switch proto {
|
||||
case GitlabProtoHTTP:
|
||||
case GitProtoHTTP:
|
||||
return p.checkHTTPRemote()
|
||||
case GitlabProtoSSH:
|
||||
case GitProtoSSH:
|
||||
return p.checkSSHRemote()
|
||||
}
|
||||
return errors.New("Unknown git protocol")
|
||||
|
@ -2,24 +2,23 @@ package remote
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load"
|
||||
)
|
||||
|
||||
type RemoteInfo struct {
|
||||
Ctx context.Context
|
||||
Host string
|
||||
Name string
|
||||
Token string
|
||||
Ctx context.Context
|
||||
Host string
|
||||
Name string
|
||||
Token string
|
||||
CloneProto config.CloneProto
|
||||
}
|
||||
|
||||
type RemoteProto string
|
||||
|
||||
const (
|
||||
RemoteProtoSSH RemoteProto = "ssh"
|
||||
RemoteProtoHTTP RemoteProto = "http"
|
||||
RemoteProtoHTTPS RemoteProto = "https"
|
||||
)
|
||||
const defNetDialTimeoutSecs = 3
|
||||
|
||||
// Any remote needs to be able to return
|
||||
// the number of projects the user has access to and also
|
||||
@ -27,7 +26,31 @@ const (
|
||||
// provided by *load.ProgressInfo
|
||||
type Remote interface {
|
||||
GetInfo() *RemoteInfo // Returns basic RemoteInfo struct
|
||||
IsAlive(RemoteProto) bool // Indicates if the remote is reachable by either SSH or HTTP
|
||||
GetNumProjects(*RemoteQueryOpts) int // Returns total number of accessible projects
|
||||
StreamProjects(*load.ProgressInfo, *RemoteQueryOpts) // Streams projects to chans provided in load.ProgressInfo
|
||||
}
|
||||
|
||||
func IsAlive(remote Remote) bool {
|
||||
var port int
|
||||
switch remote.GetInfo().CloneProto {
|
||||
case config.CloneProtoHTTP:
|
||||
port = 443
|
||||
case config.CloneProtoSSH:
|
||||
port = 22
|
||||
}
|
||||
|
||||
d, err := net.DialTimeout("tcp",
|
||||
fmt.Sprintf("%s:%d", remote.GetInfo().Host, port),
|
||||
defNetDialTimeoutSecs*time.Second)
|
||||
|
||||
if err == nil {
|
||||
_, err = d.Write([]byte("ok"))
|
||||
d.Close()
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ package remote
|
||||
|
||||
import "context"
|
||||
|
||||
// Generic options to be passed to any
|
||||
// impelenter of the Remote interface
|
||||
type RemoteQueryOpts struct {
|
||||
Ctx context.Context
|
||||
OwnerOnly bool
|
||||
|
@ -17,8 +17,8 @@ const (
|
||||
|
||||
type Remotes []remote.Remote
|
||||
|
||||
func (r *Remotes) AddRemote(remote remote.Remote) {
|
||||
*r = append(*r, remote)
|
||||
func (r *Remotes) AddRemotes(remote ...remote.Remote) {
|
||||
*r = append(*r, remote...)
|
||||
}
|
||||
|
||||
func (r *Remotes) GetRemoteByHost(host string) remote.Remote {
|
||||
@ -31,20 +31,18 @@ func (r *Remotes) GetRemoteByHost(host string) remote.Remote {
|
||||
}
|
||||
|
||||
// Launches project streamsers for all remotes in goroutines
|
||||
// returns slice of load.ProgressInfo
|
||||
func (r *Remotes) StreamRemotes(opts *remote.RemoteQueryOpts) []*load.ProgressInfo {
|
||||
progressInfos := make([]*load.ProgressInfo, len(*r))
|
||||
for i, remoteInstance := range *r {
|
||||
progressInfos[i] = &load.ProgressInfo{
|
||||
ProgressChan: make(chan load.Progress),
|
||||
ProjectsChan: make(chan []*projects.Project),
|
||||
ErrorChan: make(chan error),
|
||||
DoneChan: make(chan interface{}),
|
||||
NumProjects: remoteInstance.GetNumProjects(opts),
|
||||
}
|
||||
go remoteInstance.StreamProjects(progressInfos[i], opts)
|
||||
// returns slice of load.ProgressInfo, does not block, streamer is
|
||||
// launched in goroutine
|
||||
func StreamRemote(r remote.Remote, opts *remote.RemoteQueryOpts) *load.ProgressInfo {
|
||||
progressInfo := &load.ProgressInfo{
|
||||
ProgressChan: make(chan load.Progress),
|
||||
ProjectsChan: make(chan []*projects.Project),
|
||||
ErrorChan: make(chan error),
|
||||
DoneChan: make(chan interface{}),
|
||||
NumProjects: r.GetNumProjects(opts),
|
||||
}
|
||||
return progressInfos
|
||||
go r.StreamProjects(progressInfo, opts)
|
||||
return progressInfo
|
||||
}
|
||||
|
||||
func NewRemotes() *Remotes {
|
||||
|
@ -1,143 +0,0 @@
|
||||
package remotes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
"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/projects"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
Ctx context.Context
|
||||
Config *config.GitlabConfig
|
||||
apiClient *gitlab.Client
|
||||
}
|
||||
|
||||
type Clients []*Client
|
||||
|
||||
type ClientOpts struct {
|
||||
Ctx context.Context
|
||||
Name string
|
||||
Host string
|
||||
Token string
|
||||
}
|
||||
|
||||
func (c *Clients) AddClients(gitlabClient ...*Client) error {
|
||||
var err error
|
||||
for _, client := range gitlabClient {
|
||||
if c.GetClientByHost(client.Config.Host) != nil {
|
||||
err = errors.Join(err, fmt.Errorf("Client with host %s already exists", client.Config.Host))
|
||||
} else {
|
||||
*c = append(*c, client)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Clients) GetClientByHost(host string) *Client {
|
||||
for _, client := range *c {
|
||||
if client.Config.Host == host {
|
||||
return client
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewCLients() *Clients {
|
||||
var clients Clients
|
||||
clients = make([]*Client, 0)
|
||||
return &clients
|
||||
}
|
||||
|
||||
func NewGitlabClients(clientOpts []*ClientOpts) (*Clients, error) {
|
||||
var err error
|
||||
clients := NewCLients()
|
||||
for _, opts := range clientOpts {
|
||||
gitlabClient, e := NewGitlabClient(opts)
|
||||
if e != nil {
|
||||
err = errors.Join(err, e)
|
||||
continue
|
||||
}
|
||||
err = errors.Join(err, clients.AddClients(gitlabClient))
|
||||
}
|
||||
return clients, err
|
||||
}
|
||||
|
||||
func NewGitlabClient(opts *ClientOpts) (*Client, error) {
|
||||
client, err := gitlab.NewClient(opts.Token, gitlab.WithBaseURL(opts.Host))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gitlabClient := &Client{
|
||||
Ctx: opts.Ctx,
|
||||
Config: &config.GitlabConfig{
|
||||
Name: opts.Name,
|
||||
Host: opts.Host,
|
||||
Token: opts.Token,
|
||||
},
|
||||
apiClient: client,
|
||||
}
|
||||
return gitlabClient, nil
|
||||
}
|
||||
|
||||
func (c *Client) Api() *gitlab.Client {
|
||||
return c.apiClient
|
||||
}
|
||||
|
||||
func (c *Client) GetTotalProjects(opts *gitlab.ListProjectsOptions) int {
|
||||
reqOpts := *opts
|
||||
reqOpts.ListOptions = gitlab.ListOptions{
|
||||
Page: 1,
|
||||
PerPage: 1,
|
||||
}
|
||||
|
||||
var projects int
|
||||
if _, r, e := c.apiClient.Projects.ListProjects(opts, gitlab.WithContext(c.Ctx)); e == nil {
|
||||
projects = r.TotalItems
|
||||
}
|
||||
|
||||
return projects
|
||||
}
|
||||
|
||||
// Returns a list of projects along with the next page and an error
|
||||
// if there was an error
|
||||
func (c *Client) ListProjects(opts *gitlab.ListProjectsOptions) (
|
||||
[]*projects.Project, *gitlab.Response, error) {
|
||||
pList := make([]*projects.Project, 0)
|
||||
projects, resp, err := c.apiClient.Projects.ListProjects(
|
||||
opts,
|
||||
gitlab.WithContext(c.Ctx),
|
||||
)
|
||||
if err == nil {
|
||||
pList = append(pList, c.handleProjects(projects)...)
|
||||
}
|
||||
return pList, resp, err
|
||||
}
|
||||
|
||||
// A nil return indicates an API error or GitLab doesn't know what
|
||||
// language the project uses.
|
||||
func (c *Client) GetProjectLanguages(project *gitlab.Project) *projects.ProjectLanguages {
|
||||
l, _, e := c.apiClient.Projects.GetProjectLanguages(project.ID, gitlab.WithContext(c.Ctx))
|
||||
if e != nil {
|
||||
pterm.Error.Printfln("Failed requesting project languages: %s", e.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
var pLangs projects.ProjectLanguages
|
||||
pLangs = make([]*projects.ProjectLanguage, len(*l))
|
||||
|
||||
var i int
|
||||
for name, pcnt := range *l {
|
||||
pLangs[i] = &projects.ProjectLanguage{
|
||||
Name: name,
|
||||
Percentage: pcnt,
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
return &pLangs
|
||||
}
|
Reference in New Issue
Block a user