Rename projects to cache package
This commit is contained in:
68
internal/cache/projects_fs.go
vendored
Normal file
68
internal/cache/projects_fs.go
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
|
||||
)
|
||||
|
||||
func (c *Cache) GoTo(project *projects.Project) {
|
||||
pPath := c.GetProjectPath(project)
|
||||
|
||||
c.log.Debug("Going to project", c.log.Args(
|
||||
"project", project.String(),
|
||||
"path", pPath,
|
||||
))
|
||||
|
||||
if _, err := os.Stat(pPath); err != nil {
|
||||
c.log.Info("Preparing project path")
|
||||
c.PrepProjectPath(pPath)
|
||||
}
|
||||
|
||||
os.Chdir(filepath.Dir(pPath))
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectFromCwd() (*projects.Project, error) {
|
||||
var project *projects.Project
|
||||
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return project, err
|
||||
} else if !strings.HasPrefix(cwd, c.path) {
|
||||
return project, errors.New("Not in any project path")
|
||||
}
|
||||
|
||||
// Strip projects dir from path
|
||||
pathWithNs := cwd[len(c.path)+1:]
|
||||
c.log.Debug("Fetching project from current path", c.log.Args(
|
||||
"cwd", cwd, "pathWithNamespace", pathWithNs,
|
||||
))
|
||||
|
||||
project = c.GetProjectByPath(pathWithNs)
|
||||
|
||||
return project, nil
|
||||
}
|
||||
|
||||
func (c *Cache) IsProjectCloned(p *projects.Project) bool {
|
||||
_, err := os.Stat(c.GetProjectPath(p) + "/.git")
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Cache) PrepProjectPath(path string) {
|
||||
if err := os.MkdirAll(path, 0750); err != nil {
|
||||
c.log.Fatal("Failed to prepare project path", c.log.Args(
|
||||
"path", path,
|
||||
"error", err,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) GetProjectPath(p *projects.Project) string {
|
||||
return c.path + "/" + p.SanitizedPath()
|
||||
}
|
||||
Reference in New Issue
Block a user