git-project-manager/internal/remotes/remote/remote.go

61 lines
1.4 KiB
Go

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/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
// the number of projects the user has access to and also
// 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
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
}
remoteUrl, _ := url.Parse(remote.GetInfo().Host)
d, err := net.DialTimeout("tcp",
fmt.Sprintf("%s:%d", remoteUrl.Host, port),
defNetDialTimeoutSecs*time.Second)
if err == nil {
_, err = d.Write([]byte("ok"))
d.Close()
}
if err == nil {
return true
} else {
return false
}
}