git-project-manager/internal/remotes/info/info.go
2024-01-18 11:09:54 -05:00

74 lines
1.7 KiB
Go

package info
import (
"context"
)
type CloneProto string
const (
CloneProtoSSH CloneProto = "ssh"
CloneProtoHTTP CloneProto = "http"
DefaultCloneProto CloneProto = CloneProtoSSH
)
type RemoteType string
type RemoteTypes []RemoteType
// Register remote types here and also add a case
// to func GetRemoteTypeFromString
var (
RemoteTypeGitlab RemoteType = "gitlab"
RemoteTypeGitea RemoteType = "gitea"
RemoteTypeGithub RemoteType = "github"
RemoteTypesAll RemoteTypes = []RemoteType{
RemoteTypeGitea,
RemoteTypeGitlab,
RemoteTypeGithub,
}
)
func GetRemoteTypeFromString(remoteType string) RemoteType {
var rt RemoteType
switch remoteType {
case RemoteTypeGitea.String():
rt = RemoteTypeGitea
case RemoteTypeGitlab.String():
rt = RemoteTypeGitlab
case RemoteTypeGithub.String():
rt = RemoteTypeGithub
}
return rt
}
func (rt *RemoteTypes) Strings() []string {
rtStrings := make([]string, len(*rt))
for i, t := range *rt {
rtStrings[i] = string(t)
}
return rtStrings
}
func (rt *RemoteType) String() string {
return string(*rt)
}
// Globally shared info for all remote types
// Stub package to prevent import cycle
type RemoteInfo struct {
Host string // Host as URL with protocol (e.g. https://gitlab.com)
Name string // Human-friendly name for remote
Token string // API token for remote
Type RemoteType // Remote type (e.g. gitlab, gitea)
CloneProto CloneProto // CloneProto (ssh or http) determines what url to use for git clone
ctx context.Context
}
func (ri *RemoteInfo) SetContext(ctx context.Context) {
ri.ctx = ctx
}
func (ri *RemoteInfo) Context() context.Context {
return ri.ctx
}