57 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package remote
 | 
						|
 | 
						|
import (
 | 
						|
	"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/info"
 | 
						|
	"gitlab.sweetwater.com/it/devops/tools/gitlab-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 {
 | 
						|
	// Helper to process configs.
 | 
						|
	// Realistically, conf.Gitlabs and conf.Giteas should be conf.Remotes
 | 
						|
	GetInfos(config.Config) []info.RemoteInfo
 | 
						|
	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
 | 
						|
	}
 | 
						|
}
 |