summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-01-13 08:55:48 -0600
committerJeff Carr <[email protected]>2025-01-13 08:55:48 -0600
commitbd54fa707a03125878a23f1ad4c93f25708cf4d3 (patch)
tree4ee592e9ae7c0fe13e4c08aa91b47e143c8b26ca
parent24200adb8ab373582e1d66703e7c3db103945640 (diff)
code to determine the go src directory
-rw-r--r--goSrcFind.go85
-rw-r--r--goWork.go34
2 files changed, 119 insertions, 0 deletions
diff --git a/goSrcFind.go b/goSrcFind.go
new file mode 100644
index 0000000..ba77a33
--- /dev/null
+++ b/goSrcFind.go
@@ -0,0 +1,85 @@
+package fhelp
+
+// 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
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+
+ "go.wit.com/log"
+)
+
+// look for a go.work file
+// otherwise use ~/go/src
+func 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 {
+ // 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 goWorkExists(dir string) bool {
+ var err error
+ workFilePath := filepath.Join(dir, "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")
+}
diff --git a/goWork.go b/goWork.go
new file mode 100644
index 0000000..7dbfac9
--- /dev/null
+++ b/goWork.go
@@ -0,0 +1,34 @@
+package fhelp
+
+import (
+ "os"
+
+ "go.wit.com/log"
+)
+
+/*
+try to determine the GO working dir
+this will look for a go.work file, otherwise
+it will default to ~/go/src
+
+returns:
+string # ~/go/src or the path to the go.work file
+bool # true if the user is using a go.work file
+err # if everything goes wrong, the error
+*/
+func DetermineGoPath() (string, bool, error) {
+ gosrc := os.Getenv("FORGE_GOSRC")
+ if gosrc != "" {
+ hasWork := goWorkExists(gosrc)
+ log.Info("Using ENV{FORGE_GOSRC} =", gosrc)
+ return gosrc, hasWork, nil
+ }
+
+ gosrc, err := findGoSrc()
+ if err != nil {
+ log.Info("fhelp.DetermineGoPath()", err)
+ }
+ hasWork := goWorkExists(gosrc)
+
+ return gosrc, hasWork, err
+}