Rename projects to cache package
This commit is contained in:
79
internal/cache/cache_aliases.go
vendored
Normal file
79
internal/cache/cache_aliases.go
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
|
||||
"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, remote string) error {
|
||||
if c.GetAliasByName(alias) != nil {
|
||||
return errors.New("Failed to add alias, already exists")
|
||||
}
|
||||
|
||||
c.Aliases = append(c.Aliases,
|
||||
&ProjectAlias{
|
||||
Alias: alias,
|
||||
ProjectID: projectID,
|
||||
Remote: remote,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) AddAlias(alias string, projectID int, remote string) error {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
return c.addAlias(alias, projectID, remote)
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectsWithAliases() []*projects.Project {
|
||||
projectList := make([]*projects.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
|
||||
}
|
||||
|
||||
// This method only exists if a cache was built
|
||||
// before multi-remote support. Upon the first load
|
||||
// with multi-remotes, this will be run first to update
|
||||
// any missing alias remotes
|
||||
func (c *Cache) setAliasRemotes() {
|
||||
for _, alias := range c.Aliases {
|
||||
if alias.Remote == "" {
|
||||
c.setAliasRemote(alias)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) setAliasRemote(alias *ProjectAlias) {
|
||||
project := c.GetProjectByID(alias.ProjectID)
|
||||
if project != nil {
|
||||
alias.Remote = project.Remote
|
||||
c.log.Debug("Fixed missing alias remote", c.log.Args(
|
||||
"alias", alias.Alias,
|
||||
"projectID", alias.ProjectID,
|
||||
"remote", alias.Remote,
|
||||
))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user