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

@ -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
}
}

View File

@ -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