cleaner Init() output

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-12-12 23:50:38 -06:00
parent 4928804bf4
commit 4e94089128
3 changed files with 42 additions and 26 deletions

View File

@ -17,26 +17,26 @@ import (
// otherwise use ~/go/src
func (f *Forge) findGoSrc() (string, error) {
pwd, err := os.Getwd()
startpwd, _ := os.Getwd()
// startpwd, _ := os.Getwd()
if err == nil {
log.Info("forge.findGoSrc() trying digup", pwd, err)
// Check for go.work in the current directory and then move up until root
if pwd, err := digup(pwd); err == nil {
log.Info("forge.findGoSrc() using go.work file in directory", pwd)
f.goWork = true
// found an existing go.work file
// os.Chdir(pwd)
return pwd, nil
} else {
log.Info("forge.digup() err", pwd, err)
// if there is an error looking for the go.work file
// default to using ~/go/src
// log.Info("forge.digup() err", pwd, err)
}
} else {
log.Info("forge.findGoSrc() os.Getwd()", pwd, err)
// 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()
log.Info("forge.findGoSrc() 2 using ~/go/src directory", pwd, "start was", startpwd)
return pwd, err
}
@ -57,26 +57,23 @@ func (f *Forge) goWorkExists() bool {
var err error
workFilePath := filepath.Join(f.GetGoSrc(), "go.work")
if _, err = os.Stat(workFilePath); err == nil {
log.Info("f.goWorkExists() found", workFilePath)
// log.Info("f.goWorkExists() found", workFilePath)
return true
} else if !os.IsNotExist(err) {
log.Info("f.goWorkExists() missing", workFilePath)
// log.Info("f.goWorkExists() missing", workFilePath)
return false
}
// probably false, but some other error
log.Info("f.goWorkExists() os.Stat() error", err, workFilePath)
// log.Info("f.goWorkExists() os.Stat() error", err, workFilePath)
return false
}
func digup(path string) (string, error) {
for {
workFilePath := filepath.Join(path, "go.work")
log.Info("digup trying", workFilePath)
if _, err := os.Stat(workFilePath); err == nil {
log.Info("digup found", path)
return path, nil // Found the go.work file
} else if !os.IsNotExist(err) {
log.Info("forgepb.digup() failed", workFilePath, err)
return "", err // An error other than not existing
}

View File

@ -50,6 +50,8 @@ func (f *Forge) ScanGoSrc() (bool, error) {
// not stupid like my old version
func gitDirectoriesNew(srcDir string) ([]string, error) {
var all []string
var trip bool
reposfile := filepath.Join(srcDir, "repos.pb")
err := filepath.WalkDir(srcDir, func(path string, d os.DirEntry, err error) error {
if err != nil {
// Handle possible errors, like permission issues
@ -60,7 +62,13 @@ func gitDirectoriesNew(srcDir string) ([]string, error) {
if d.IsDir() {
// log.Info("path is dir", path)
} else {
log.Info("warning: you have an untracked file:", path)
if path == reposfile {
// ignore repos.pb // todo: also ignore go.work // add config option to not warn about this
} else {
// todo: figure out a way to do padding for init()
log.Info("WARNING: you have an untracked file outside of any .git repository:", path)
trip = true
}
return nil
}
@ -72,6 +80,20 @@ func gitDirectoriesNew(srcDir string) ([]string, error) {
}
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
}

23
init.go
View File

@ -12,21 +12,17 @@ import (
func Init() *Forge {
f := new(Forge)
getwd, _ := os.Getwd()
log.Info("forgepbb.Init() os.Getwd()", getwd)
log.Info("forgepbb.Init() started with FORGE_CONFIG", os.Getenv("FORGE_CONFIG"))
log.Info("forgepbb.Init() started with FORGE_GOSRC", os.Getenv("FORGE_GOSRC"))
// TODO: rethink this but it works for now
gosrc := os.Getenv("FORGE_GOSRC")
if gosrc == "" {
goSrcDir, err := f.findGoSrc()
if err != nil {
log.Warn("forge init() findGoSrc()", err)
os.Exit(-1)
}
os.Setenv("FORGE_GOSRC", goSrcDir)
}
f.goSrc = os.Getenv("FORGE_GOSRC")
log.Info("forge.Init() using ~/go/src directory", f.goSrc)
// also rethink this, but maybe this is the right thing to do
if os.Getenv("FORGE_CONFIG") == "" {
@ -34,12 +30,15 @@ func Init() *Forge {
fullpath := filepath.Join(homeDir, ".config/forge")
os.Setenv("FORGE_CONFIG", fullpath)
}
// check again for go.work // user could have a go.work file in ~/go/src
if f.goWorkExists() {
f.goWork = true
}
log.Info("forgepbb.Init() ~/go/src ", f.goSrc)
log.Info("forgepbb.Init() 2 FORGE_CONFIG", os.Getenv("FORGE_CONFIG"))
log.Info("forgepbb.Init() 2 FORGE_GOSRC", os.Getenv("FORGE_GOSRC"), "f.goWork =", f.IsGoWork())
// print out the settings that will be used
log.Info("forgepbb.Init() FORGE_CONFIG", os.Getenv("FORGE_CONFIG"))
log.Info("forgepbb.Init() FORGE_GOSRC ", os.Getenv("FORGE_GOSRC"), "f.goWork =", f.IsGoWork())
// cache.go has Do()
// f.initOnce.Do(f.initWork)
@ -65,12 +64,10 @@ func Init() *Forge {
start := f.Repos.Len()
f.ScanGoSrc()
end := f.Repos.Len()
log.Info("forge.ScanGoSrc() Found", end-start, "new repos in", f.goSrc)
testenv := os.Getenv("GO111MODULE")
if testenv == "off" {
log.Info("GO111MODULE=off", "f.goWork =", f.IsGoWork(), "f.gosrc =", f.GetGoSrc())
if (end - start) == 0 {
log.Info("Scan of", f.GetGoSrc(), "did not find new git repositories")
} else {
log.Info("GO111MODULE=", testenv, "f.goWork =", f.IsGoWork(), "f.gosrc =", f.GetGoSrc())
log.Info("Scan of", f.GetGoSrc(), "Found", end-start, "new git repositories")
}
return f
}