Refactor gitlab to generic remotes
This commit is contained in:
		@@ -9,11 +9,12 @@ import (
 | 
			
		||||
	"github.com/pterm/pterm"
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
 | 
			
		||||
	"gopkg.in/yaml.v3"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Cache struct {
 | 
			
		||||
	Projects     []*remotes.Project
 | 
			
		||||
	Projects     []*projects.Project
 | 
			
		||||
	Aliases      []*ProjectAlias
 | 
			
		||||
	Updated      time.Time
 | 
			
		||||
	config       *config.Config
 | 
			
		||||
@@ -124,7 +125,7 @@ func (c *Cache) Read() error {
 | 
			
		||||
// project aliase cache. Writes to disk.
 | 
			
		||||
func (c *Cache) clear(clearAliases bool) {
 | 
			
		||||
	c.log.Info("Clearing project cache")
 | 
			
		||||
	c.Projects = make([]*remotes.Project, 0)
 | 
			
		||||
	c.Projects = make([]*projects.Project, 0)
 | 
			
		||||
	if clearAliases {
 | 
			
		||||
		c.log.Info("Clearing project alias cache")
 | 
			
		||||
		c.Aliases = make([]*ProjectAlias, 0)
 | 
			
		||||
@@ -201,7 +202,7 @@ func NewProjectCache(opts *CacheOpts) (*Cache, error) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	cache := &Cache{
 | 
			
		||||
		Projects:    make([]*remotes.Project, 0),
 | 
			
		||||
		Projects:    make([]*projects.Project, 0),
 | 
			
		||||
		Aliases:     make([]*ProjectAlias, 0),
 | 
			
		||||
		config:      opts.Config,
 | 
			
		||||
		file:        opts.Path,
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,7 @@ package projects
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
 | 
			
		||||
	"golang.org/x/exp/slices"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -42,8 +42,8 @@ func (c *Cache) AddAlias(alias string, projectID int, remote string) error {
 | 
			
		||||
	return c.addAlias(alias, projectID, remote)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Cache) GetProjectsWithAliases() []*remotes.Project {
 | 
			
		||||
	projectList := make([]*remotes.Project, 0)
 | 
			
		||||
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) {
 | 
			
		||||
 
 | 
			
		||||
@@ -4,15 +4,15 @@ import (
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/pterm/pterm"
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
 | 
			
		||||
	"golang.org/x/exp/slices"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (c *Cache) AddProjects(projects ...*remotes.Project) {
 | 
			
		||||
func (c *Cache) AddProjects(gitProjects ...*projects.Project) {
 | 
			
		||||
	c.contentLock.Lock()
 | 
			
		||||
	defer c.contentLock.Unlock()
 | 
			
		||||
	for _, p := range projects {
 | 
			
		||||
		var existing *remotes.Project
 | 
			
		||||
	for _, p := range gitProjects {
 | 
			
		||||
		var existing *projects.Project
 | 
			
		||||
		sameID := c.GetProjectsByID(p.ID)
 | 
			
		||||
 | 
			
		||||
		// If there is only one by ID, we don't
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,7 @@ import (
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/lithammer/fuzzysearch/fuzzy"
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Performs a fuzzy find on the input string, returning the closest
 | 
			
		||||
@@ -39,8 +39,8 @@ func (c *Cache) FuzzyFindAlias(name string) []*ProjectAlias {
 | 
			
		||||
 | 
			
		||||
// Returns all matching projects by fuzzy find term
 | 
			
		||||
// Matches NameWithNamespace and Aliases
 | 
			
		||||
func (c *Cache) FuzzyFindProjects(search string) []*remotes.Project {
 | 
			
		||||
	projects := make([]*remotes.Project, 0, len(c.Projects))
 | 
			
		||||
func (c *Cache) FuzzyFindProjects(search string) []*projects.Project {
 | 
			
		||||
	projects := make([]*projects.Project, 0, len(c.Projects))
 | 
			
		||||
	for _, p := range c.Projects {
 | 
			
		||||
		if fuzzy.MatchFold(search, p.NameWithNamespace) {
 | 
			
		||||
			projects = append(projects, p)
 | 
			
		||||
 
 | 
			
		||||
@@ -4,11 +4,11 @@ import (
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/pterm/pterm"
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
 | 
			
		||||
	"golang.org/x/exp/slices"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (c *Cache) ProjectString(p *remotes.Project) string {
 | 
			
		||||
func (c *Cache) ProjectString(p *projects.Project) string {
 | 
			
		||||
	info := strings.Builder{}
 | 
			
		||||
 | 
			
		||||
	info.WriteString(pterm.LightGreen("\n--------------\n"))
 | 
			
		||||
@@ -42,7 +42,7 @@ func (c *Cache) ProjectStrings(prefix string) []string {
 | 
			
		||||
	return slices.Clip(projects)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Cache) GetProjectByPath(path string) *remotes.Project {
 | 
			
		||||
func (c *Cache) GetProjectByPath(path string) *projects.Project {
 | 
			
		||||
	for _, p := range c.Projects {
 | 
			
		||||
		if p.PathWithNamespace == path {
 | 
			
		||||
			return p
 | 
			
		||||
@@ -51,7 +51,7 @@ func (c *Cache) GetProjectByPath(path string) *remotes.Project {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Cache) GetProjectByRemoteAndId(remote string, id int) *remotes.Project {
 | 
			
		||||
func (c *Cache) GetProjectByRemoteAndId(remote string, id int) *projects.Project {
 | 
			
		||||
	for _, p := range c.Projects {
 | 
			
		||||
		if p.ID == id && p.Remote == remote {
 | 
			
		||||
			return p
 | 
			
		||||
@@ -60,7 +60,7 @@ func (c *Cache) GetProjectByRemoteAndId(remote string, id int) *remotes.Project
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Cache) GetProjectByID(id int) *remotes.Project {
 | 
			
		||||
func (c *Cache) GetProjectByID(id int) *projects.Project {
 | 
			
		||||
	for _, p := range c.Projects {
 | 
			
		||||
		if p.ID == id {
 | 
			
		||||
			return p
 | 
			
		||||
@@ -72,8 +72,8 @@ func (c *Cache) GetProjectByID(id int) *remotes.Project {
 | 
			
		||||
// Plural form of GetProjectByID
 | 
			
		||||
// Since multiple remotes may have the same project ID,
 | 
			
		||||
// this will return all matching
 | 
			
		||||
func (c *Cache) GetProjectsByID(id int) []*remotes.Project {
 | 
			
		||||
	projects := make([]*remotes.Project, 0)
 | 
			
		||||
func (c *Cache) GetProjectsByID(id int) []*projects.Project {
 | 
			
		||||
	projects := make([]*projects.Project, 0)
 | 
			
		||||
	for _, p := range c.Projects {
 | 
			
		||||
		if p.ID == id {
 | 
			
		||||
			projects = append(projects, p)
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,7 @@ import (
 | 
			
		||||
	"text/tabwriter"
 | 
			
		||||
 | 
			
		||||
	"github.com/pterm/pterm"
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
 | 
			
		||||
	"golang.org/x/exp/slices"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -17,7 +17,7 @@ type ProjectAlias struct {
 | 
			
		||||
	Remote    string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Cache) GetProjectAliasStrings(project *remotes.Project) []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) {
 | 
			
		||||
@@ -26,7 +26,7 @@ func (c *Cache) GetProjectAliasStrings(project *remotes.Project) []string {
 | 
			
		||||
	return strings
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Cache) GetProjectStringWithAliases(project *remotes.Project) string {
 | 
			
		||||
func (c *Cache) GetProjectStringWithAliases(project *projects.Project) string {
 | 
			
		||||
	aliases := c.GetProjectAliasStrings(project)
 | 
			
		||||
	return fmt.Sprintf("%s (%s) -> %s",
 | 
			
		||||
		project.Name,
 | 
			
		||||
@@ -83,7 +83,7 @@ func (c *Cache) GetAliasByName(name string, gitlabs ...string) *ProjectAlias {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Cache) GetProjectByAlias(alias *ProjectAlias) *remotes.Project {
 | 
			
		||||
func (c *Cache) GetProjectByAlias(alias *ProjectAlias) *projects.Project {
 | 
			
		||||
	if alias == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
@@ -95,7 +95,7 @@ func (c *Cache) GetProjectByAlias(alias *ProjectAlias) *remotes.Project {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Cache) GetProjectAliases(project *remotes.Project) []*ProjectAlias {
 | 
			
		||||
func (c *Cache) GetProjectAliases(project *projects.Project) []*ProjectAlias {
 | 
			
		||||
	aliases := make([]*ProjectAlias, 0)
 | 
			
		||||
	for _, alias := range c.Aliases {
 | 
			
		||||
		if alias.ProjectID == project.ID {
 | 
			
		||||
 
 | 
			
		||||
@@ -6,10 +6,10 @@ import (
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (c *Cache) GoTo(project *remotes.Project) {
 | 
			
		||||
func (c *Cache) GoTo(project *projects.Project) {
 | 
			
		||||
	pPath := c.GetProjectPath(project)
 | 
			
		||||
 | 
			
		||||
	c.log.Debug("Going to project", c.log.Args(
 | 
			
		||||
@@ -25,8 +25,8 @@ func (c *Cache) GoTo(project *remotes.Project) {
 | 
			
		||||
	os.Chdir(filepath.Dir(pPath))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Cache) GetProjectFromCwd() (*remotes.Project, error) {
 | 
			
		||||
	var project *remotes.Project
 | 
			
		||||
func (c *Cache) GetProjectFromCwd() (*projects.Project, error) {
 | 
			
		||||
	var project *projects.Project
 | 
			
		||||
 | 
			
		||||
	cwd, err := os.Getwd()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -46,7 +46,7 @@ func (c *Cache) GetProjectFromCwd() (*remotes.Project, error) {
 | 
			
		||||
	return project, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Cache) IsProjectCloned(p *remotes.Project) bool {
 | 
			
		||||
func (c *Cache) IsProjectCloned(p *projects.Project) bool {
 | 
			
		||||
	_, err := os.Stat(c.GetProjectPath(p) + "/.git")
 | 
			
		||||
	if err == nil {
 | 
			
		||||
		return true
 | 
			
		||||
@@ -63,6 +63,6 @@ func (c *Cache) PrepProjectPath(path string) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Cache) GetProjectPath(p *remotes.Project) string {
 | 
			
		||||
func (c *Cache) GetProjectPath(p *projects.Project) string {
 | 
			
		||||
	return c.path + "/" + p.SanitizedPath()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ import (
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	git "github.com/go-git/go-git/v5"
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
 | 
			
		||||
	"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const gitCloneTimeoutSecs = 30
 | 
			
		||||
@@ -13,7 +13,7 @@ const gitCloneTimeoutSecs = 30
 | 
			
		||||
// Will either read in the current repo, preparing a report
 | 
			
		||||
// on its current state, or will clone the project if it has not
 | 
			
		||||
// already been cloned in its path
 | 
			
		||||
func (c *Cache) OpenProject(ctx context.Context, project *remotes.Project) *git.Repository {
 | 
			
		||||
func (c *Cache) OpenProject(ctx context.Context, project *projects.Project) *git.Repository {
 | 
			
		||||
	path := c.GetProjectPath(project)
 | 
			
		||||
	cloneCtx, cncl := context.WithDeadline(ctx, time.Now().Add(gitCloneTimeoutSecs*time.Second))
 | 
			
		||||
	defer cncl()
 | 
			
		||||
@@ -31,7 +31,7 @@ func (c *Cache) OpenProject(ctx context.Context, project *remotes.Project) *git.
 | 
			
		||||
		// Check to make sure we can connect before we time out
 | 
			
		||||
		// shouldn't be necessary, but go-git does not properly
 | 
			
		||||
		// honor its context
 | 
			
		||||
		if err := project.CheckHost(remotes.GitlabProtoSSH); err != nil {
 | 
			
		||||
		if err := project.CheckHost(projects.GitlabProtoSSH); err != nil {
 | 
			
		||||
			c.log.Fatal("Git remote unreachable, giving up", c.log.Args("error", err))
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user