Refactor gitlab to generic remotes

This commit is contained in:
2024-01-15 15:39:35 -05:00
parent da209c53e3
commit d6833a9ea0
21 changed files with 208 additions and 172 deletions

View File

@ -0,0 +1,72 @@
package projects
import (
"fmt"
"strings"
"time"
"github.com/go-git/go-git/v5"
)
type Project struct {
ID int
Description string
SSHURLToRepo string
HTTPURLToRepo string
WebURL string
Name string
NameWithNamespace string
Path string
PathWithNamespace string
AvatarURL string
LastActivityAt time.Time
Readme string
Remote string
Owner string
Languages *ProjectLanguages
gitRepo *git.Repository
}
type ProjectLanguages []*ProjectLanguage
type ProjectLanguage struct {
Name string
Percentage float32
}
func (p *Project) String() string {
var projectString string
if p != nil {
projectString = fmt.Sprintf("%s (%s)", p.Path, p.PathWithNamespace)
}
return projectString
}
func (p *Project) GetLanguage() *ProjectLanguage {
if p.Languages == nil {
return nil
}
var lang *ProjectLanguage
var maxPcnt float32
for _, p := range *p.Languages {
if p.Percentage > maxPcnt {
lang = p
}
maxPcnt = p.Percentage
}
return lang
}
func (p *Project) SanitizedPath() string {
return strings.Trim(p.PathWithNamespace, " '\"%<>|`")
}
func (p *Project) SetRepo(r *git.Repository) {
p.gitRepo = r
}
func (p *Project) GetRepo() *git.Repository {
return p.gitRepo
}

View File

@ -0,0 +1,37 @@
package projects
import "github.com/pterm/pterm"
func (p *Project) GetGitInfo() string {
repo := p.GetRepo()
if repo == nil {
return "No Repo"
}
var str string
str += "\n" + pterm.LightRed("Project: ") + pterm.Bold.Sprint(p.Name) + "\n"
head, _ := repo.Head()
branch := head.Name().String()[11:]
b, _ := repo.Branch(branch)
if b != nil {
str += pterm.LightCyan("Branch: ") + pterm.Bold.Sprint(b.Name) + "\n"
} else {
str += pterm.LightCyan("NEW Branch: ") + pterm.Bold.Sprint(branch) + "\n"
}
commit, _ := repo.CommitObject(head.Hash())
str += "\n" + commit.String()
str += pterm.LightMagenta("GitLab: ") + pterm.Bold.Sprint(p.HTTPURLToRepo) + "\n"
if remotes, _ := repo.Remotes(); len(remotes) > 0 {
str += pterm.LightBlue("Remote: ") + pterm.Bold.Sprint(remotes[0].Config().URLs[0])
}
return pterm.DefaultBox.
WithLeftPadding(5).WithRightPadding(5).
WithBoxStyle(&pterm.Style{pterm.FgLightBlue}).
WithTitle(pterm.Bold.Sprint(pterm.LightGreen("Project Git Status"))).
Sprint(str)
}

View File

@ -0,0 +1,78 @@
package projects
import (
"errors"
"fmt"
"net"
"net/url"
"strconv"
"time"
giturl "github.com/whilp/git-urls"
)
const defNetDialTimeoutSecs = 5
type GitlabProto int
const (
GitlabProtoSSH GitlabProto = iota
GitlabProtoHTTP
)
var (
ErrUnknownHost error = errors.New("No addresses found for host")
)
func (p *Project) CheckHost(proto GitlabProto) error {
switch proto {
case GitlabProtoHTTP:
return p.checkHTTPRemote()
case GitlabProtoSSH:
return p.checkSSHRemote()
}
return errors.New("Unknown git protocol")
}
func (p *Project) checkHTTPRemote() error {
u, err := giturl.Parse(p.HTTPURLToRepo)
if err == nil {
err = p.checkHost(u)
}
return err
}
func (p *Project) checkSSHRemote() error {
u, err := giturl.Parse(p.SSHURLToRepo)
if err == nil {
err = p.checkHost(u)
}
return err
}
func (p *Project) checkHost(u *url.URL) error {
var port int
if u.Port() == "" {
switch u.Scheme {
case "http":
port = 80
case "ssh":
port = 22
case "https":
port = 443
}
} else {
port, _ = strconv.Atoi(u.Port())
}
d, err := net.DialTimeout("tcp",
fmt.Sprintf("%s:%d", u.Hostname(), port),
defNetDialTimeoutSecs*time.Second)
if err == nil {
_, err = d.Write([]byte("ok"))
d.Close()
}
return err
}