wip
This commit is contained in:
@ -3,6 +3,7 @@ package gitlab
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
@ -55,6 +56,10 @@ func (p *Project) String() string {
|
||||
return fmt.Sprintf("%s (%s)", p.Path, p.PathWithNamespace)
|
||||
}
|
||||
|
||||
func (p *Project) SanitizedPath() string {
|
||||
return strings.Trim(p.PathWithNamespace, " '\"%<>|`")
|
||||
}
|
||||
|
||||
// Given there may be thousands of projects, this will return
|
||||
// channels that stream progress info and then finally the full
|
||||
// list of projects on separate channels
|
||||
|
@ -22,13 +22,15 @@ type Cache struct {
|
||||
file string
|
||||
log *pterm.Logger
|
||||
gitlab *gitlab.Client
|
||||
path string
|
||||
}
|
||||
|
||||
type CacheOpts struct {
|
||||
Path string
|
||||
TTL time.Duration
|
||||
Logger *pterm.Logger
|
||||
Gitlab *gitlab.Client
|
||||
Path string
|
||||
ProjectsPath string
|
||||
TTL time.Duration
|
||||
Logger *pterm.Logger
|
||||
Gitlab *gitlab.Client
|
||||
}
|
||||
|
||||
// Load cache, if already loaded and up to date, nothing is done.
|
||||
@ -102,7 +104,7 @@ func (c *Cache) Clear(clearAliases bool) {
|
||||
}
|
||||
|
||||
func (c *Cache) refresh() {
|
||||
c.log.Info("Refreshing project cache, this may take a while")
|
||||
c.log.Info("Loading project cache, this may take a while\n")
|
||||
defer c.setUpdated()
|
||||
c.LoadProjects()
|
||||
}
|
||||
@ -172,6 +174,7 @@ func NewProjectCache(opts *CacheOpts) (*Cache, error) {
|
||||
lock: &sync.Mutex{},
|
||||
log: opts.Logger,
|
||||
gitlab: opts.Gitlab,
|
||||
path: opts.ProjectsPath,
|
||||
}
|
||||
|
||||
return cache, err
|
||||
|
@ -1,6 +1,25 @@
|
||||
package projects
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
func (c *Cache) deleteAlias(alias *ProjectAlias) {
|
||||
for i, a := range c.Aliases {
|
||||
if a.Alias == alias.Alias {
|
||||
c.Aliases = append(c.Aliases[:i], c.Aliases[i+1:]...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) DeleteAlias(alias *ProjectAlias) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.deleteAlias(alias)
|
||||
}
|
||||
|
||||
func (c *Cache) addAlias(alias string, projectID int) error {
|
||||
if c.GetAliasByName(alias) != nil {
|
||||
@ -21,3 +40,24 @@ func (c *Cache) AddAlias(alias string, projectID int) error {
|
||||
defer c.lock.Unlock()
|
||||
return c.addAlias(alias, projectID)
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectsWithAliases() []*gitlab.Project {
|
||||
projectList := make([]*gitlab.Project, 0)
|
||||
projectsFound := make([]int, 0)
|
||||
for _, a := range c.Aliases {
|
||||
if !slices.Contains(projectsFound, a.ProjectID) {
|
||||
projectList = append(projectList, c.GetProjectByAlias(a))
|
||||
projectsFound = append(projectsFound, a.ProjectID)
|
||||
}
|
||||
}
|
||||
return projectList
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectAliasStrings(project *gitlab.Project) []string {
|
||||
aliases := c.GetProjectAliases(project)
|
||||
strings := make([]string, len(aliases))
|
||||
for i, a := range c.GetProjectAliases(project) {
|
||||
strings[i] = a.Alias
|
||||
}
|
||||
return strings
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ func (c *Cache) FuzzyFindAlias(name string) []*ProjectAlias {
|
||||
found[i] = r.Target
|
||||
}
|
||||
c.log.Warn("Fuzzy found multiple aliases, try being more specific",
|
||||
c.log.Args("foundAliases", strings.Join(found, ",")))
|
||||
c.log.Args("foundAliases", strings.Join(found, ", ")))
|
||||
}
|
||||
var aliases []*ProjectAlias
|
||||
if ranks.Len() > 0 {
|
||||
|
@ -1,8 +1,10 @@
|
||||
package projects
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
|
||||
@ -21,10 +23,29 @@ func ProjectAliasesString(aliases []*ProjectAlias) string {
|
||||
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) ProjectString(p *gitlab.Project) string {
|
||||
info := strings.Builder{}
|
||||
|
||||
info.WriteString(pterm.LightBlue(p.Name))
|
||||
info.WriteString(pterm.LightGreen("\n--------------\n"))
|
||||
info.WriteString(pterm.Bold.Sprint(p.Name))
|
||||
info.WriteRune('\n')
|
||||
if p.Description != "" {
|
||||
info.WriteString(p.Description)
|
||||
@ -40,6 +61,7 @@ func (c *Cache) ProjectString(p *gitlab.Project) string {
|
||||
aliases := c.GetProjectAliases(p)
|
||||
info.WriteString(ProjectAliasesString(aliases))
|
||||
|
||||
info.WriteString(pterm.LightGreen("\n--------------\n"))
|
||||
return info.String()
|
||||
}
|
||||
|
||||
@ -107,7 +129,7 @@ func (c *Cache) LoadProjects() {
|
||||
WithShowPercentage(true).
|
||||
WithTotal(-1).
|
||||
WithTitle("Listing GitLab Projects").
|
||||
WithMaxWidth(0)
|
||||
WithMaxWidth(100)
|
||||
|
||||
defer pBar.Stop()
|
||||
|
||||
@ -133,6 +155,7 @@ func (c *Cache) LoadProjects() {
|
||||
return
|
||||
case <-progressInfo.DoneChan:
|
||||
pBar.Add(pBar.Total - curProjects)
|
||||
fmt.Println("")
|
||||
c.log.Info("Project load complete")
|
||||
return
|
||||
}
|
||||
|
30
internal/projects/projects_fs.go
Normal file
30
internal/projects/projects_fs.go
Normal file
@ -0,0 +1,30 @@
|
||||
package projects
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
|
||||
)
|
||||
|
||||
func (c *Cache) GoTo(project *gitlab.Project) {
|
||||
pPath := c.path + "/" + project.SanitizedPath()
|
||||
|
||||
c.log.Debug("Going to project", c.log.Args(
|
||||
"project", project.String(),
|
||||
"path", pPath,
|
||||
))
|
||||
|
||||
if _, err := os.Stat(pPath); err != nil {
|
||||
c.log.Info("Preparing project path")
|
||||
c.PrepProjectPath(pPath)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) PrepProjectPath(path string) {
|
||||
if err := os.MkdirAll(path, 0750); err != nil {
|
||||
c.log.Fatal("Failed to prepare project path", c.log.Args(
|
||||
"path", path,
|
||||
"error", err,
|
||||
))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user