85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
package cache
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/pterm/pterm"
|
|
"golang.org/x/exp/slices"
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
|
|
)
|
|
|
|
func (c *Cache) ProjectString(p *projects.Project) string {
|
|
info := strings.Builder{}
|
|
|
|
info.WriteString(pterm.LightGreen("\n--------------\n"))
|
|
info.WriteString(pterm.Bold.Sprint(p.Name))
|
|
info.WriteRune('\n')
|
|
if p.Description != "" {
|
|
info.WriteString(p.Description)
|
|
info.WriteRune('\n')
|
|
}
|
|
|
|
info.WriteString("\nPath: " + pterm.LightGreen(p.PathWithNamespace))
|
|
info.WriteString("\nProjectID: " + pterm.LightGreen(p.ID))
|
|
info.WriteString("\nURL: " + pterm.LightGreen(p.WebURL))
|
|
info.WriteString("\nLastActivity: " + pterm.LightMagenta(p.LastActivityAt.String()))
|
|
info.WriteString("\nAliases: ")
|
|
|
|
aliases := c.GetProjectAliases(p)
|
|
info.WriteString(ProjectAliasesString(aliases))
|
|
|
|
info.WriteString(pterm.LightGreen("\n--------------\n"))
|
|
return info.String()
|
|
}
|
|
|
|
func (c *Cache) ProjectStrings(prefix string) []string {
|
|
projects := make([]string, 0, len(c.Projects))
|
|
for _, p := range c.Projects {
|
|
if strings.HasPrefix(p.NameWithNamespace, prefix) {
|
|
projects = append(projects, p.NameWithNamespace)
|
|
}
|
|
}
|
|
return slices.Clip(projects)
|
|
}
|
|
|
|
func (c *Cache) GetProjectByPath(path string) *projects.Project {
|
|
for _, p := range c.Projects {
|
|
if p.PathWithNamespace == path {
|
|
return p
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Cache) GetProjectByRemoteAndId(remote string, id int) *projects.Project {
|
|
for _, p := range c.Projects {
|
|
if p.ID == id && p.Remote == remote {
|
|
return p
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Cache) GetProjectByID(id string) *projects.Project {
|
|
for _, p := range c.Projects {
|
|
if p.GetID() == id {
|
|
return p
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Plural form of GetProjectByID
|
|
// Since multiple remotes may have the same project ID,
|
|
// this will return all matching
|
|
func (c *Cache) GetProjectsByID(id int) []*projects.Project {
|
|
projects := make([]*projects.Project, 0)
|
|
for _, p := range c.Projects {
|
|
if p.ID == id {
|
|
projects = append(projects, p)
|
|
}
|
|
}
|
|
return projects
|
|
}
|