53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package remote
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"net/url"
|
|
"time"
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/info"
|
|
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/load"
|
|
)
|
|
|
|
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 {
|
|
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
|
|
}
|
|
|
|
func IsAlive(remote Remote) bool {
|
|
var port int
|
|
switch remote.GetInfo().CloneProto {
|
|
case info.CloneProtoHTTP:
|
|
port = 443
|
|
case info.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
|
|
}
|
|
}
|