wip
This commit is contained in:
@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"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/gitlab"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@ -16,6 +17,7 @@ type Cache struct {
|
||||
Projects []*gitlab.Project
|
||||
Aliases []*ProjectAlias
|
||||
Updated time.Time
|
||||
config *config.Config
|
||||
readFromFile bool
|
||||
lock *sync.Mutex
|
||||
ttl time.Duration
|
||||
@ -31,6 +33,7 @@ type CacheOpts struct {
|
||||
TTL time.Duration
|
||||
Logger *pterm.Logger
|
||||
Gitlab *gitlab.Client
|
||||
Config *config.Config
|
||||
}
|
||||
|
||||
// Load cache, if already loaded and up to date, nothing is done.
|
||||
@ -124,10 +127,21 @@ func (c *Cache) String() string {
|
||||
len(c.Aliases))
|
||||
}
|
||||
|
||||
func (c *Cache) DumpString() string {
|
||||
// This command will only dump projects that have
|
||||
// been cloned locally. Setting all to true will list all projects
|
||||
func (c *Cache) DumpString(all bool) string {
|
||||
str := strings.Builder{}
|
||||
str.WriteString(c.String() + "\n\nProjects:\n")
|
||||
var term string
|
||||
if all {
|
||||
term = pterm.Bold.Sprint("Projects")
|
||||
} else {
|
||||
term = pterm.Bold.Sprint("Local Projects")
|
||||
}
|
||||
str.WriteString(c.String() + "\n\n" + term + ":\n")
|
||||
for _, project := range c.Projects {
|
||||
if !all && !c.IsProjectCloned(project) {
|
||||
continue
|
||||
}
|
||||
str.WriteString(" - " + pterm.FgLightBlue.Sprint(project.Name) + " (")
|
||||
str.WriteString(project.PathWithNamespace + ")\n")
|
||||
aliases := c.GetProjectAliases(project)
|
||||
@ -169,6 +183,7 @@ func NewProjectCache(opts *CacheOpts) (*Cache, error) {
|
||||
cache := &Cache{
|
||||
Projects: make([]*gitlab.Project, 0),
|
||||
Aliases: make([]*ProjectAlias, 0),
|
||||
config: opts.Config,
|
||||
file: opts.Path,
|
||||
ttl: opts.TTL,
|
||||
lock: &sync.Mutex{},
|
||||
|
@ -122,7 +122,7 @@ func (c *Cache) GetProjectAliases(project *gitlab.Project) []*ProjectAlias {
|
||||
}
|
||||
|
||||
func (c *Cache) LoadProjects() {
|
||||
progressInfo := c.gitlab.StreamProjects()
|
||||
progressInfo := c.gitlab.StreamProjects(c.config.Cache.Load.OwnerOnly)
|
||||
c.Projects = make([]*gitlab.Project, 0)
|
||||
|
||||
pBar := pterm.DefaultProgressbar.
|
||||
|
@ -2,12 +2,13 @@ package projects
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
|
||||
)
|
||||
|
||||
func (c *Cache) GoTo(project *gitlab.Project) {
|
||||
pPath := c.path + "/" + project.SanitizedPath()
|
||||
pPath := c.GetProjectPath(project)
|
||||
|
||||
c.log.Debug("Going to project", c.log.Args(
|
||||
"project", project.String(),
|
||||
@ -18,6 +19,16 @@ func (c *Cache) GoTo(project *gitlab.Project) {
|
||||
c.log.Info("Preparing project path")
|
||||
c.PrepProjectPath(pPath)
|
||||
}
|
||||
|
||||
os.Chdir(filepath.Dir(pPath))
|
||||
}
|
||||
|
||||
func (c *Cache) IsProjectCloned(p *gitlab.Project) bool {
|
||||
_, err := os.Stat(c.GetProjectPath(p) + "/.git")
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Cache) PrepProjectPath(path string) {
|
||||
@ -28,3 +39,7 @@ func (c *Cache) PrepProjectPath(path string) {
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectPath(p *gitlab.Project) string {
|
||||
return c.path + "/" + p.SanitizedPath()
|
||||
}
|
||||
|
60
internal/projects/projects_git.go
Normal file
60
internal/projects/projects_git.go
Normal file
@ -0,0 +1,60 @@
|
||||
package projects
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
git "github.com/go-git/go-git/v5"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
|
||||
)
|
||||
|
||||
const gitCloneTimeoutSecs = 10
|
||||
|
||||
// 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 *gitlab.Project) *git.Repository {
|
||||
path := c.GetProjectPath(project)
|
||||
cloneCtx, cncl := context.WithDeadline(ctx, time.Now().Add(gitCloneTimeoutSecs*time.Second))
|
||||
defer cncl()
|
||||
|
||||
var repo *git.Repository
|
||||
var err error
|
||||
|
||||
if repo, err = git.PlainOpen(path); err != nil {
|
||||
if err == git.ErrRepositoryNotExists {
|
||||
c.log.Debug("Project not yet cloned")
|
||||
}
|
||||
}
|
||||
|
||||
if repo == nil {
|
||||
// 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(gitlab.GitlabProtoSSH); err != nil {
|
||||
c.log.Fatal("Git remote unreachable, giving up", c.log.Args("error", err))
|
||||
}
|
||||
|
||||
c.log.Info("Cloning project from remote", c.log.Args("remote", project.SSHURLToRepo))
|
||||
repo, err = git.PlainCloneContext(cloneCtx, path, false, &git.CloneOptions{
|
||||
URL: project.SSHURLToRepo,
|
||||
})
|
||||
|
||||
if err == git.ErrRepositoryAlreadyExists {
|
||||
c.log.Debug("Skipping clone, already exists")
|
||||
} else if err != nil {
|
||||
c.log.Fatal("Failed to open git project", c.log.Args(
|
||||
"error", err,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
head, err := repo.Head()
|
||||
if err != nil {
|
||||
c.log.Fatal("Something went wrong, unable to fetch HEAD from repo", c.log.Args(
|
||||
"error", err,
|
||||
))
|
||||
}
|
||||
c.log.Debug("Repository ready", c.log.Args("branch", head.Name().String()))
|
||||
return repo
|
||||
}
|
Reference in New Issue
Block a user