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

61 lines
1.4 KiB
Go
Raw Normal View History

2024-01-15 20:39:35 +00:00
package remote
import (
"context"
2024-01-16 17:48:42 +00:00
"fmt"
"net"
2024-01-16 19:26:56 +00:00
"net/url"
2024-01-16 17:48:42 +00:00
"time"
2024-01-16 17:48:42 +00:00
"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 {
2024-01-16 17:48:42 +00:00
Ctx context.Context
Host string
Name string
Token string
CloneProto config.CloneProto
}
2024-01-15 20:39:35 +00:00
2024-01-16 17:48:42 +00:00
const defNetDialTimeoutSecs = 3
2024-01-15 20:39:35 +00:00
// 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
2024-01-16 19:26:56 +00:00
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
2024-01-15 20:39:35 +00:00
}
2024-01-16 17:48:42 +00:00
func IsAlive(remote Remote) bool {
var port int
switch remote.GetInfo().CloneProto {
case config.CloneProtoHTTP:
port = 443
case config.CloneProtoSSH:
port = 22
}
2024-01-16 19:26:56 +00:00
remoteUrl, _ := url.Parse(remote.GetInfo().Host)
2024-01-16 17:48:42 +00:00
d, err := net.DialTimeout("tcp",
2024-01-16 19:26:56 +00:00
fmt.Sprintf("%s:%d", remoteUrl.Host, port),
2024-01-16 17:48:42 +00:00
defNetDialTimeoutSecs*time.Second)
if err == nil {
_, err = d.Write([]byte("ok"))
d.Close()
}
if err == nil {
return true
} else {
return false
}
}