finally dump this old code
This commit is contained in:
parent
2fccf60335
commit
cb6394f34a
86
goSrcFind.go
86
goSrcFind.go
|
@ -1,86 +0,0 @@
|
||||||
package forgepb
|
|
||||||
|
|
||||||
// returns whatever your golang source dir is
|
|
||||||
// If there is a go.work file in your parent, that directory will be returned
|
|
||||||
// otherwise, return ~/go/src
|
|
||||||
|
|
||||||
func (f *Forge) GetHome() string {
|
|
||||||
return f.Config.ReposDir
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
// look for a go.work file
|
|
||||||
// otherwise use ~/go/src
|
|
||||||
func (f *Forge) findGoSrc() (string, error) {
|
|
||||||
pwd, err := os.Getwd()
|
|
||||||
// startpwd, _ := os.Getwd()
|
|
||||||
if err == nil {
|
|
||||||
// Check for go.work in the current directory and then move up until root
|
|
||||||
if pwd, err := digup(pwd); err == nil {
|
|
||||||
f.goWork = true
|
|
||||||
// found an existing go.work file
|
|
||||||
// os.Chdir(pwd)
|
|
||||||
return pwd, nil
|
|
||||||
} else {
|
|
||||||
// if there is an error looking for the go.work file
|
|
||||||
// default to using ~/go/src
|
|
||||||
// log.Info("forge.digup() err", pwd, err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// this shouldn't really happen. maybe your working directory got deleted
|
|
||||||
log.Info("forge.findGoSrc() os.Getwd() was probably deleted", pwd, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// there are no go.work files, resume the ~/go/src behavior from prior to golang 1.22
|
|
||||||
pwd, err = useGoSrc()
|
|
||||||
return pwd, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// this is the 'old way" and works fine for me. I use it because I like the ~/go/src directory
|
|
||||||
// because I know exactly what is in it: GO stuff & nothing else
|
|
||||||
func useGoSrc() (string, error) {
|
|
||||||
homeDir, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
pwd := filepath.Join(homeDir, "go/src")
|
|
||||||
err = os.MkdirAll(pwd, os.ModePerm)
|
|
||||||
return pwd, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Forge) goWorkExists() bool {
|
|
||||||
var err error
|
|
||||||
workFilePath := filepath.Join(f.Config.ReposDir, "go.work")
|
|
||||||
if _, err = os.Stat(workFilePath); err == nil {
|
|
||||||
// log.Info("f.goWorkExists() found", workFilePath)
|
|
||||||
return true
|
|
||||||
} else if !os.IsNotExist(err) {
|
|
||||||
// log.Info("f.goWorkExists() missing", workFilePath)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
// probably false, but some other error
|
|
||||||
// log.Info("f.goWorkExists() os.Stat() error", err, workFilePath)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
func digup(path string) (string, error) {
|
|
||||||
for {
|
|
||||||
workFilePath := filepath.Join(path, "go.work")
|
|
||||||
if _, err := os.Stat(workFilePath); err == nil {
|
|
||||||
return path, nil // Found the go.work file
|
|
||||||
} else if !os.IsNotExist(err) {
|
|
||||||
return "", err // An error other than not existing
|
|
||||||
}
|
|
||||||
|
|
||||||
parentPath := filepath.Dir(path)
|
|
||||||
if parentPath == path {
|
|
||||||
break // Reached the filesystem root
|
|
||||||
}
|
|
||||||
path = parentPath
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", fmt.Errorf("no go.work file found")
|
|
||||||
}
|
|
||||||
*/
|
|
206
goSrcScan.go
206
goSrcScan.go
|
@ -1,206 +0,0 @@
|
||||||
package forgepb
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"go.wit.com/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
/*
|
|
||||||
func (f *Forge) ScanGoSrc() (bool, error) {
|
|
||||||
dirs, err := gitDirectoriesNew(f.Config.ReposDir)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
stats := f.RillRepos(reloadCheck)
|
|
||||||
for _, stat := range stats {
|
|
||||||
if stat.Err == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
config.SetChanged("repos", true)
|
|
||||||
}
|
|
||||||
|
|
||||||
var gopaths []string
|
|
||||||
for _, dir := range dirs {
|
|
||||||
if strings.HasPrefix(dir, f.Config.ReposDir) {
|
|
||||||
gopath := strings.TrimPrefix(dir, f.Config.ReposDir)
|
|
||||||
gopath = strings.Trim(gopath, "/")
|
|
||||||
if r := f.FindByGoPath(gopath); r != nil {
|
|
||||||
// log.Info("already have", gopath)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
gopaths = append(gopaths, gopath)
|
|
||||||
} else {
|
|
||||||
log.Log(WARN, "ScanGoSrc() bad:", dir)
|
|
||||||
return false, errors.New("forgepb.ScanGoSrc() bad dir: " + dir)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
newcount, err := f.rillScanDirs(gopaths)
|
|
||||||
if err != nil {
|
|
||||||
log.Info("go src dir problem. exit for now?", err)
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
if newcount != 0 {
|
|
||||||
log.Info("forge go src scan found", newcount, "repos")
|
|
||||||
config.SetChanged("repos", true)
|
|
||||||
}
|
|
||||||
return true, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns a repo protobuf for a directory if the directory is a git repo
|
|
||||||
func (f *Forge) ScanDir(dir string) *gitpb.Repo {
|
|
||||||
repo, err := f.Repos.NewRepo(dir, "")
|
|
||||||
if err != nil {
|
|
||||||
log.Info("ScanDir() error", dir, err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return repo
|
|
||||||
}
|
|
||||||
|
|
||||||
func gitDirectoriesOld(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(WARN, "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(WARN, "Error walking the path:", srcDir, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return all, 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()
|
|
||||||
}
|
|
||||||
|
|
||||||
// rill is awesome. long live rill
|
|
||||||
// attempt scan with rill
|
|
||||||
func (f *Forge) rillScanDirs(gopaths []string) (int, 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.checkpath(id, "")
|
|
||||||
})
|
|
||||||
|
|
||||||
var counter int
|
|
||||||
// Activate users.
|
|
||||||
// Concurrency = 10
|
|
||||||
err := rill.ForEach(dirs, 10, func(repo *gitpb.Repo) error {
|
|
||||||
counter += 1
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return counter, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Forge) checkpath(gopath string, url string) (*gitpb.Repo, error) {
|
|
||||||
fullpath := filepath.Join(f.Config.ReposDir, gopath)
|
|
||||||
log.Info("forge creating protobuf for", fullpath)
|
|
||||||
repo, err := f.NewGoRepo(gopath, "")
|
|
||||||
if err != nil {
|
|
||||||
log.Info("\tprotobuf error", gopath, err)
|
|
||||||
}
|
|
||||||
return repo, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// deletes the repo from the protobuf (pray your mutex locks are working)
|
|
||||||
// re-scans the repo
|
|
||||||
// returns the new repo
|
|
||||||
func (f *Forge) ReAdd(repo *gitpb.Repo) (*gitpb.Repo, error) {
|
|
||||||
if repo == nil {
|
|
||||||
return nil, log.Errorf("can't delete repo == nil")
|
|
||||||
}
|
|
||||||
fullpath := repo.GetFullPath()
|
|
||||||
ns := repo.GetNamespace()
|
|
||||||
if !f.Repos.Delete(repo) {
|
|
||||||
return nil, log.Errorf("delete of repo failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
repo, err := f.AddNamespaceDir(ns, fullpath)
|
|
||||||
if err != nil {
|
|
||||||
log.Info("ReAdd() error", fullpath, err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return repo, err
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// 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
|
|
||||||
var trip bool
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
if d.IsDir() {
|
|
||||||
// log.Info("path is dir", path)
|
|
||||||
} else {
|
|
||||||
_, fname := filepath.Split(path)
|
|
||||||
switch fname {
|
|
||||||
case "repos.pb":
|
|
||||||
case "go.work":
|
|
||||||
case "go.work.last":
|
|
||||||
case "go.work.sum":
|
|
||||||
default:
|
|
||||||
// todo: figure out a way to do padding for init()
|
|
||||||
if trip == false {
|
|
||||||
log.Info("WARNING:")
|
|
||||||
}
|
|
||||||
log.Info("WARNING: you have an untracked file outside of any .git repository:", path)
|
|
||||||
trip = true
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
gitdir := filepath.Join(path, ".git")
|
|
||||||
_, err2 := os.Stat(gitdir)
|
|
||||||
if !os.IsNotExist(err2) {
|
|
||||||
all = append(all, path)
|
|
||||||
return filepath.SkipDir
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
//
|
|
||||||
// probably always leave this here forever
|
|
||||||
// this check, along with CheckDirty() makes sure you can safely delete ~/go/src or the go.work directory
|
|
||||||
// because everything is either checked in or deleted. An important thing to know!
|
|
||||||
if trip {
|
|
||||||
log.Info("WARNING:")
|
|
||||||
log.Info("WARNING: there isn't a way to disable this warning yet")
|
|
||||||
log.Info("WARNING: probably this is a good thing however. you don't want to leave files outside of git repos here")
|
|
||||||
log.Info("WARNING: so this warning should probably stay")
|
|
||||||
log.Info("WARNING:")
|
|
||||||
log.Info("WARNING: this also might mean you put these files here because you are actively working on them")
|
|
||||||
log.Info("WARNING: and you don't want to forget about them")
|
|
||||||
log.Info("WARNING:")
|
|
||||||
}
|
|
||||||
return all, err
|
|
||||||
}
|
|
|
@ -1,6 +1,10 @@
|
||||||
package forgepb
|
package forgepb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/destel/rill"
|
"github.com/destel/rill"
|
||||||
"go.wit.com/lib/config"
|
"go.wit.com/lib/config"
|
||||||
"go.wit.com/lib/protobuf/gitpb"
|
"go.wit.com/lib/protobuf/gitpb"
|
||||||
|
@ -108,3 +112,60 @@ func (f *Forge) rillScanDirsNew(fullpaths []string) (int, error) {
|
||||||
|
|
||||||
return counter, err
|
return counter, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
var trip bool
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.IsDir() {
|
||||||
|
// log.Info("path is dir", path)
|
||||||
|
} else {
|
||||||
|
_, fname := filepath.Split(path)
|
||||||
|
switch fname {
|
||||||
|
case "repos.pb":
|
||||||
|
case "go.work":
|
||||||
|
case "go.work.last":
|
||||||
|
case "go.work.sum":
|
||||||
|
default:
|
||||||
|
// todo: figure out a way to do padding for init()
|
||||||
|
if trip == false {
|
||||||
|
log.Info("WARNING:")
|
||||||
|
}
|
||||||
|
log.Info("WARNING: you have an untracked file outside of any .git repository:", path)
|
||||||
|
trip = true
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
gitdir := filepath.Join(path, ".git")
|
||||||
|
_, err2 := os.Stat(gitdir)
|
||||||
|
if !os.IsNotExist(err2) {
|
||||||
|
all = append(all, path)
|
||||||
|
return filepath.SkipDir
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
//
|
||||||
|
// probably always leave this here forever
|
||||||
|
// this check, along with CheckDirty() makes sure you can safely delete ~/go/src or the go.work directory
|
||||||
|
// because everything is either checked in or deleted. An important thing to know!
|
||||||
|
if trip {
|
||||||
|
log.Info("WARNING:")
|
||||||
|
log.Info("WARNING: there isn't a way to disable this warning yet")
|
||||||
|
log.Info("WARNING: probably this is a good thing however. you don't want to leave files outside of git repos here")
|
||||||
|
log.Info("WARNING: so this warning should probably stay")
|
||||||
|
log.Info("WARNING:")
|
||||||
|
log.Info("WARNING: this also might mean you put these files here because you are actively working on them")
|
||||||
|
log.Info("WARNING: and you don't want to forget about them")
|
||||||
|
log.Info("WARNING:")
|
||||||
|
}
|
||||||
|
return all, err
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue