git-project-manager/internal/cache/projects_fs.go

66 lines
1.4 KiB
Go
Raw Normal View History

2024-01-15 21:02:15 +00:00
package cache
2023-12-09 04:13:17 +00:00
import (
"errors"
2023-12-09 04:13:17 +00:00
"os"
2023-12-10 04:19:19 +00:00
"path/filepath"
"strings"
2023-12-09 04:13:17 +00:00
2024-01-15 20:39:35 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
2023-12-09 04:13:17 +00:00
)
2024-01-15 20:39:35 +00:00
func (c *Cache) GoTo(project *projects.Project) {
2023-12-10 04:19:19 +00:00
pPath := c.GetProjectPath(project)
2023-12-09 04:13:17 +00:00
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)
}
2023-12-10 04:19:19 +00:00
os.Chdir(filepath.Dir(pPath))
}
2024-01-15 20:39:35 +00:00
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
}
2024-01-15 20:39:35 +00:00
func (c *Cache) IsProjectCloned(p *projects.Project) bool {
2023-12-10 04:19:19 +00:00
_, err := os.Stat(c.GetProjectPath(p) + "/.git")
2024-10-01 18:29:14 +00:00
return err == nil
2023-12-09 04:13:17 +00:00
}
func (c *Cache) PrepProjectPath(path string) {
2024-10-01 18:29:14 +00:00
if err := os.MkdirAll(path, 0o750); err != nil {
2023-12-09 04:13:17 +00:00
c.log.Fatal("Failed to prepare project path", c.log.Args(
"path", path,
"error", err,
))
}
}
2023-12-10 04:19:19 +00:00
2024-01-15 20:39:35 +00:00
func (c *Cache) GetProjectPath(p *projects.Project) string {
2023-12-10 04:19:19 +00:00
return c.path + "/" + p.SanitizedPath()
}