Begin move to project/alias by unique ID

This commit is contained in:
2024-12-30 16:42:45 -05:00
parent 11a2ca434c
commit 70027a9880
7 changed files with 51 additions and 25 deletions

View File

@ -22,7 +22,7 @@ func (c *Cache) DeleteAlias(alias *ProjectAlias) {
c.deleteAlias(alias)
}
func (c *Cache) addAlias(alias string, projectID int, remote string) error {
func (c *Cache) addAlias(alias string, project *projects.Project) error {
if c.GetAliasByName(alias) != nil {
return errors.New("failed to add alias, already exists")
}
@ -30,26 +30,27 @@ func (c *Cache) addAlias(alias string, projectID int, remote string) error {
c.Aliases = append(c.Aliases,
&ProjectAlias{
Alias: alias,
ProjectID: projectID,
Remote: remote,
ProjectID: project.ID,
ID: project.GetID(),
Remote: project.Remote,
})
return nil
}
func (c *Cache) AddAlias(alias string, projectID int, remote string) error {
func (c *Cache) AddAlias(alias string, project *projects.Project) error {
c.lock.Lock()
defer c.lock.Unlock()
return c.addAlias(alias, projectID, remote)
return c.addAlias(alias, project)
}
func (c *Cache) GetProjectsWithAliases() []*projects.Project {
projectList := make([]*projects.Project, 0)
projectsFound := make([]int, 0)
projectsFound := make([]string, 0)
for _, a := range c.Aliases {
if !slices.Contains(projectsFound, a.ProjectID) {
if !slices.Contains(projectsFound, a.ID) {
projectList = append(projectList, c.GetProjectByAlias(a))
projectsFound = append(projectsFound, a.ProjectID)
projectsFound = append(projectsFound, a.ID)
}
}
return projectList
@ -68,12 +69,13 @@ func (c *Cache) setAliasRemotes() {
}
func (c *Cache) setAliasRemote(alias *ProjectAlias) {
project := c.GetProjectByID(alias.ProjectID)
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,
))
}

View File

@ -4,8 +4,9 @@ import (
"strings"
"github.com/pterm/pterm"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
"golang.org/x/exp/slices"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
)
func (c *Cache) ProjectString(p *projects.Project) string {
@ -60,9 +61,9 @@ func (c *Cache) GetProjectByRemoteAndId(remote string, id int) *projects.Project
return nil
}
func (c *Cache) GetProjectByID(id int) *projects.Project {
func (c *Cache) GetProjectByID(id string) *projects.Project {
for _, p := range c.Projects {
if p.ID == id {
if p.GetID() == id {
return p
}
}

View File

@ -7,13 +7,15 @@ import (
"text/tabwriter"
"github.com/pterm/pterm"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
"golang.org/x/exp/slices"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
)
type ProjectAlias struct {
Alias string
ProjectID int
ID string
Remote string
}
@ -96,7 +98,7 @@ func (c *Cache) GetProjectByAlias(alias *ProjectAlias) *projects.Project {
return nil
}
for _, p := range c.Projects {
if p.ID == alias.ProjectID && p.Remote == alias.Remote {
if p.GetID() == alias.ID {
return p
}
}