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

53 lines
1.4 KiB
Go
Raw Normal View History

2024-01-15 20:39:35 +00:00
package remote
import (
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-12-19 19:55:49 +00:00
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/info"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/load"
)
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 {
2024-01-16 19:26:56 +00:00
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
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 info.CloneProtoHTTP:
2024-01-16 17:48:42 +00:00
port = 443
case info.CloneProtoSSH:
2024-01-16 17:48:42 +00:00
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
}
}