Rename projects to cache package
This commit is contained in:
106
internal/cache/projects_alias.go
vendored
Normal file
106
internal/cache/projects_alias.go
vendored
Normal file
@@ -0,0 +1,106 @@
|
||||
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, gitlabs ...string) *ProjectAlias {
|
||||
for _, a := range c.Aliases {
|
||||
if len(gitlabs) > 0 && !slices.Contains(gitlabs, 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
|
||||
}
|
||||
Reference in New Issue
Block a user