77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
package cache
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/pterm/pterm"
|
|
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
|
|
"golang.org/x/exp/slices"
|
|
)
|
|
|
|
func (c *Cache) AddProjects(gitProjects ...*projects.Project) {
|
|
c.contentLock.Lock()
|
|
defer c.contentLock.Unlock()
|
|
for _, p := range gitProjects {
|
|
var existing *projects.Project
|
|
sameID := c.GetProjectsByID(p.ID)
|
|
|
|
// If there is only one by ID, we don't
|
|
// have to worry about matching remotes.
|
|
// If there are more than one, either match
|
|
// remotes or update the remote if it was never
|
|
// set due to being build from old code
|
|
// New caches should never have empty remotes.
|
|
if len(sameID) == 1 {
|
|
existing = sameID[0]
|
|
} else if len(sameID) > 1 {
|
|
for _, pr := range sameID {
|
|
if pr.Remote == p.Remote || pr.Remote == "" {
|
|
existing = pr
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// Add a new one, or update if changed
|
|
if existing == nil {
|
|
c.Projects = append(c.Projects, p)
|
|
} else if *existing != *p {
|
|
*existing = *p
|
|
}
|
|
}
|
|
}
|
|
|
|
// This command will only dump projects that have
|
|
// been cloned locally. Setting all to true will list all projects
|
|
func (c *Cache) DumpString(all bool, search string, remotes ...string) string {
|
|
str := strings.Builder{}
|
|
var term string
|
|
if all {
|
|
term = pterm.Bold.Sprint("Projects")
|
|
} else {
|
|
term = pterm.Bold.Sprint("Local Projects")
|
|
}
|
|
str.WriteString(c.String() + "\n\n" + term + ":\n")
|
|
projects := c.FuzzyFindProjects(search)
|
|
for _, project := range projects {
|
|
if !all && !c.IsProjectCloned(project) {
|
|
continue
|
|
} else if len(remotes) > 0 && !slices.Contains(remotes, project.Remote) {
|
|
continue
|
|
}
|
|
str.WriteString(" - " + pterm.FgLightBlue.Sprint(project.Name) + " (")
|
|
str.WriteString(project.PathWithNamespace + ")\n")
|
|
aliases := c.GetProjectAliases(project)
|
|
if len(aliases) > 0 {
|
|
str.WriteString(pterm.FgLightGreen.Sprint(" aliases:"))
|
|
for _, a := range aliases {
|
|
str.WriteString(" [" + pterm.FgCyan.Sprint(a.Alias) + "]")
|
|
}
|
|
str.WriteRune('\n')
|
|
}
|
|
str.WriteString(pterm.FgLightMagenta.Sprint(" remote: "))
|
|
str.WriteString(project.Remote + "\n")
|
|
}
|
|
return str.String()
|
|
}
|