package cache import ( "errors" "golang.org/x/exp/slices" "gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects" ) 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, project *projects.Project) error { if c.GetAliasByName(alias) != nil { return errors.New("failed to add alias, already exists") } c.Aliases = append(c.Aliases, &ProjectAlias{ Alias: alias, ProjectID: project.ID, ID: project.GetID(), Remote: project.Remote, }) return nil } func (c *Cache) AddAlias(alias string, project *projects.Project) error { c.lock.Lock() defer c.lock.Unlock() return c.addAlias(alias, project) } func (c *Cache) GetProjectsWithAliases() []*projects.Project { projectList := make([]*projects.Project, 0) projectsFound := make([]string, 0) for _, a := range c.Aliases { if !slices.Contains(projectsFound, a.ID) { projectList = append(projectList, c.GetProjectByAlias(a)) projectsFound = append(projectsFound, a.ID) } } 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.ID) if project != nil { alias.Remote = project.Remote c.log.Debug("Fixed missing alias remote", c.log.Args( "alias", alias.Alias, "projectID", alias.ProjectID, "ID", alias.ID, "remote", alias.Remote, )) } }