git-project-manager/internal/cache/projects.go

85 lines
2.0 KiB
Go
Raw Normal View History

2024-01-15 21:02:15 +00:00
package cache
2023-12-07 17:08:56 +00:00
import (
2023-12-08 21:52:26 +00:00
"strings"
2023-12-07 17:08:56 +00:00
"github.com/pterm/pterm"
"golang.org/x/exp/slices"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
2023-12-07 17:08:56 +00:00
)
2024-01-15 20:39:35 +00:00
func (c *Cache) ProjectString(p *projects.Project) string {
2023-12-08 21:52:26 +00:00
info := strings.Builder{}
2023-12-09 04:13:17 +00:00
info.WriteString(pterm.LightGreen("\n--------------\n"))
info.WriteString(pterm.Bold.Sprint(p.Name))
2023-12-08 21:52:26 +00:00
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))
2023-12-09 04:13:17 +00:00
info.WriteString(pterm.LightGreen("\n--------------\n"))
2023-12-08 21:52:26 +00:00
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)
}
2023-12-08 21:52:26 +00:00
}
return slices.Clip(projects)
2023-12-08 21:52:26 +00:00
}
2024-01-15 20:39:35 +00:00
func (c *Cache) GetProjectByPath(path string) *projects.Project {
for _, p := range c.Projects {
if p.PathWithNamespace == path {
return p
}
}
return nil
}
2024-01-15 20:39:35 +00:00
func (c *Cache) GetProjectByRemoteAndId(remote string, id int) *projects.Project {
2023-12-08 21:52:26 +00:00
for _, p := range c.Projects {
if p.ID == id && p.Remote == remote {
2023-12-08 21:52:26 +00:00
return p
}
}
return nil
}
func (c *Cache) GetProjectByID(id string) *projects.Project {
2023-12-08 21:52:26 +00:00
for _, p := range c.Projects {
if p.GetID() == id {
2023-12-08 21:52:26 +00:00
return p
}
}
return nil
}
// Plural form of GetProjectByID
// Since multiple remotes may have the same project ID,
// this will return all matching
2024-01-15 20:39:35 +00:00
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)
2023-12-07 17:08:56 +00:00
}
}
return projects
2023-12-07 17:08:56 +00:00
}