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

84 lines
2.0 KiB
Go

package projects
import (
"strings"
"github.com/pterm/pterm"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
"golang.org/x/exp/slices"
)
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 int) *projects.Project {
for _, p := range c.Projects {
if p.ID == 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
}