107 lines
2.3 KiB
Go
107 lines
2.3 KiB
Go
package cache
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"strings"
|
|
"text/tabwriter"
|
|
|
|
"github.com/pterm/pterm"
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
|
|
"golang.org/x/exp/slices"
|
|
)
|
|
|
|
type ProjectAlias struct {
|
|
Alias string
|
|
ProjectID int
|
|
Remote string
|
|
}
|
|
|
|
func (c *Cache) GetProjectAliasStrings(project *projects.Project) []string {
|
|
aliases := c.GetProjectAliases(project)
|
|
strings := make([]string, len(aliases))
|
|
for i, a := range c.GetProjectAliases(project) {
|
|
strings[i] = a.Alias
|
|
}
|
|
return strings
|
|
}
|
|
|
|
func (c *Cache) GetProjectStringWithAliases(project *projects.Project) string {
|
|
aliases := c.GetProjectAliasStrings(project)
|
|
return fmt.Sprintf("%s (%s) -> %s",
|
|
project.Name,
|
|
strings.Join(aliases, ", "),
|
|
project.PathWithNamespace,
|
|
)
|
|
}
|
|
|
|
func ProjectAliasesString(aliases []*ProjectAlias) string {
|
|
var str string
|
|
for _, a := range aliases {
|
|
str += "[" + pterm.LightCyan(a.Alias) + "] "
|
|
}
|
|
return strings.Trim(str, " ")
|
|
}
|
|
|
|
func (c *Cache) AliasesByProjectString() string {
|
|
var str bytes.Buffer
|
|
|
|
w := new(tabwriter.Writer)
|
|
w.Init(&str, 10, 0, 0, ' ', tabwriter.AlignRight)
|
|
|
|
for _, p := range c.GetProjectsWithAliases() {
|
|
var pa string
|
|
pa += pterm.LightBlue("- ")
|
|
pa += fmt.Sprint(pterm.Bold.Sprint(p.String()) + " \t ")
|
|
pa += fmt.Sprint(ProjectAliasesString(c.GetProjectAliases(p)))
|
|
fmt.Fprintln(w, pa)
|
|
}
|
|
|
|
w.Flush()
|
|
return str.String()
|
|
}
|
|
|
|
func (c *Cache) AliasStrings(prefix string) []string {
|
|
aliases := make([]string, 0, len(c.Aliases))
|
|
for _, a := range c.Aliases {
|
|
if strings.HasPrefix(a.Alias, prefix) {
|
|
aliases = append(aliases, a.Alias)
|
|
}
|
|
}
|
|
return aliases
|
|
}
|
|
|
|
func (c *Cache) GetAliasByName(name string, remotes ...string) *ProjectAlias {
|
|
for _, a := range c.Aliases {
|
|
if len(remotes) > 0 && !slices.Contains(remotes, a.Remote) {
|
|
continue
|
|
}
|
|
if name == a.Alias {
|
|
return a
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Cache) GetProjectByAlias(alias *ProjectAlias) *projects.Project {
|
|
if alias == nil {
|
|
return nil
|
|
}
|
|
for _, p := range c.Projects {
|
|
if p.ID == alias.ProjectID && p.Remote == alias.Remote {
|
|
return p
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Cache) GetProjectAliases(project *projects.Project) []*ProjectAlias {
|
|
aliases := make([]*ProjectAlias, 0)
|
|
for _, alias := range c.Aliases {
|
|
if alias.ProjectID == project.ID {
|
|
aliases = append(aliases, alias)
|
|
}
|
|
}
|
|
return aliases
|
|
}
|