forgepb/goSrcScan.go

142 lines
3.3 KiB
Go
Raw Normal View History

2024-11-28 08:35:39 -06:00
package forgepb
import (
2024-11-30 12:45:07 -06:00
"errors"
"fmt"
2024-11-28 08:35:39 -06:00
"os"
"path/filepath"
"strings"
2024-11-30 12:45:07 -06:00
"github.com/destel/rill"
"go.wit.com/lib/protobuf/gitpb"
2024-11-28 08:35:39 -06:00
"go.wit.com/log"
)
func (f *Forge) ScanGoSrc() (bool, error) {
log.Info("pre dir walk")
dirs, err := gitDirectoriesNew(f.goSrc)
log.Info("post dir walk", len(dirs))
2024-11-28 08:35:39 -06:00
if err != nil {
return false, err
}
2024-11-30 12:45:07 -06:00
var gopaths []string
2024-11-28 08:35:39 -06:00
for _, dir := range dirs {
if strings.HasPrefix(dir, f.goSrc) {
gopath := strings.TrimPrefix(dir, f.goSrc)
gopath = strings.Trim(gopath, "/")
2024-11-30 12:45:07 -06:00
gopaths = append(gopaths, gopath)
2024-11-28 08:35:39 -06:00
} else {
2024-11-28 08:54:30 -06:00
log.Log(FORGEPBWARN, "ScanGoSrc() bad:", dir)
2024-11-30 12:45:07 -06:00
return false, errors.New("forgepb.ScanGoSrc() bad dir: " + dir)
2024-11-28 08:35:39 -06:00
}
}
log.Info("pre rill")
2024-11-30 12:45:07 -06:00
f.rillScanDirs(gopaths)
return true, err
}
2024-11-30 12:45:07 -06:00
// doesn't enter the directory any further when it finds a .git/
// not stupid like my old version
func gitDirectoriesNew(srcDir string) ([]string, error) {
var all []string
err := filepath.WalkDir(srcDir, func(path string, d os.DirEntry, err error) error {
if err != nil {
// Handle possible errors, like permission issues
fmt.Fprintf(os.Stderr, "error accessing path %q: %v\n", path, err)
return err
2024-11-30 12:45:07 -06:00
}
gitdir := filepath.Join(path, ".git")
_, err2 := os.Stat(gitdir)
if !os.IsNotExist(err2) {
all = append(all, path)
return filepath.SkipDir
}
return nil
})
return all, err
2024-11-28 08:35:39 -06:00
}
func gitDirectoriesOld(srcDir string) ([]string, error) {
2024-11-28 08:35:39 -06:00
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
}
2024-11-29 21:50:55 -06:00
// 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()
}
2024-11-28 08:35:39 -06:00
/*
// 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)
}
*/
2024-11-30 12:45:07 -06:00
// rill is awesome. long live rill
// attempt scan with rill
func (f *Forge) rillScanDirs(gopaths []string) error {
// 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) (*gitpb.Repo, error) {
return f.Repos.NewGoPath(f.goSrc, id)
})
// Activate users.
// Concurrency = 10
err := rill.ForEach(dirs, 10, func(repo *gitpb.Repo) error {
// could do something here
// fmt.Printf("Repo found : %s\n", repo.GoPath)
// repo.Run([]string{"git", "pull"})
return nil
})
return err
}