98 lines
2.1 KiB
Go
98 lines
2.1 KiB
Go
package forgepb
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"go.wit.com/lib/protobuf/gitpb"
|
|
"go.wit.com/log"
|
|
)
|
|
|
|
func (f *Forge) ScanGoSrc() (bool, error) {
|
|
dirs, err := gitDirectories(f.goSrc)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
var repos *gitpb.Repos
|
|
repos = new(gitpb.Repos)
|
|
|
|
newr, err := repos.NewGoPath("/home/jcarr/go/src", "go.wit.com/apps/wit-package")
|
|
if err != nil {
|
|
log.Info("init failed", err)
|
|
panic("crapnuts")
|
|
} else {
|
|
log.Info("init worked for", newr.GoPath)
|
|
}
|
|
|
|
for _, dir := range dirs {
|
|
if strings.HasPrefix(dir, f.goSrc) {
|
|
gopath := strings.TrimPrefix(dir, f.goSrc)
|
|
gopath = strings.Trim(gopath, "/")
|
|
log.Info("ScanGoSrc() ok:", f.goSrc, gopath)
|
|
} else {
|
|
log.Info("ScanGoSrc() bad:", dir)
|
|
}
|
|
}
|
|
return true, err
|
|
}
|
|
|
|
func gitDirectories(srcDir string) ([]string, error) {
|
|
var all []string
|
|
err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
log.Log(FORGEPBWARN, "Error accessing path:", path, err)
|
|
return nil
|
|
}
|
|
|
|
// Check if the path is a directory and has a .git subdirectory
|
|
if info.IsDir() && IsGitDir(path) {
|
|
all = append(all, path)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
log.Log(FORGEPBWARN, "Error walking the path:", srcDir, err)
|
|
}
|
|
|
|
return all, err
|
|
}
|
|
|
|
/*
|
|
// rill is awesome. long live rill
|
|
func rillAddDirs(gopaths []string) {
|
|
// Convert a slice of user IDs into a channel
|
|
ids := rill.FromSlice(gopaths, nil)
|
|
|
|
// Read users from the API.
|
|
// Concurrency = 20
|
|
dirs := rill.Map(ids, 20, func(id string) (*repolist.RepoRow, error) {
|
|
return me.repos.View.FindByName(id), nil
|
|
})
|
|
|
|
// Activate users.
|
|
// Concurrency = 10
|
|
err := rill.ForEach(dirs, 10, func(repo *repolist.RepoRow) error {
|
|
fmt.Printf("Repo found : %s\n", repo.GoPath())
|
|
repo.Run([]string{"git", "pull"})
|
|
return nil
|
|
})
|
|
|
|
// Handle errors
|
|
fmt.Println("Error:", err)
|
|
}
|
|
*/
|
|
|
|
// IsGitDir checks if a .git directory exists inside the given directory
|
|
func IsGitDir(dir string) bool {
|
|
gitDir := filepath.Join(dir, ".git")
|
|
info, err := os.Stat(gitDir)
|
|
if os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return info.IsDir()
|
|
}
|