rewrite a standard ENV function
This commit is contained in:
parent
cc5db9ba7e
commit
fc25cd8b05
|
@ -0,0 +1,196 @@
|
|||
package fhelp
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
// This defines the "default" behavior for forge when doing GO lang development
|
||||
//
|
||||
// Since this code is common, it's in a seperate package so it can be used elsewhere
|
||||
//
|
||||
// The default behavior is:
|
||||
//
|
||||
// * If your current directory or parent directory has a go.work file, make that your default spot
|
||||
// * Otherwise, set the default to ~/go/src
|
||||
//
|
||||
// This routine ensures the following ENV vars are set:
|
||||
//
|
||||
// FORGE_CONFIG == where forge's configs are stored (normally ~/.config/forge)
|
||||
// FORGE_REPOPB == where the repos.pb protobuf cache file is stored (normally ~/.cache/forge/repos.pb)
|
||||
// FORGE_GOSRC == based on the path, what the user probably want for developing in GO (Defaults to ~/go/src)
|
||||
// FORGE_GOWORK == true or false depending on the GOSRC result
|
||||
//
|
||||
// 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
|
||||
//
|
||||
|
||||
// set the ENV vars
|
||||
// always set them to _something_ even when everything seems to be failing
|
||||
func ConfigureENV() error {
|
||||
var anyerr error
|
||||
// setup the forge config dir
|
||||
if os.Getenv("FORGE_CONFIG") == "" {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err == nil {
|
||||
fullpath := filepath.Join(homeDir, ".config/forge")
|
||||
os.MkdirAll(fullpath, os.ModePerm)
|
||||
os.Setenv("FORGE_CONFIG", fullpath)
|
||||
} else {
|
||||
log.Info("user home dir error", err)
|
||||
os.Setenv("FORGE_CONFIG", "/tmp")
|
||||
anyerr = err
|
||||
}
|
||||
}
|
||||
|
||||
// setting FORGE_URL
|
||||
if os.Getenv("FORGE_URL") == "" {
|
||||
os.Setenv("FORGE_URL", "https://forge.wit.com/")
|
||||
}
|
||||
|
||||
// hostname is needed. allow ENV to pass it in
|
||||
if os.Getenv("HOSTNAME") == "" {
|
||||
if hname, err := os.Hostname(); err == nil {
|
||||
os.Setenv("HOSTNAME", hname)
|
||||
} else {
|
||||
os.Setenv("HOSTNAME", "unconfigured.hostname.forge")
|
||||
}
|
||||
}
|
||||
|
||||
// if this env is already set, just use what is there
|
||||
if os.Getenv("FORGE_GOSRC") != "" {
|
||||
// always use this ENV if it is set
|
||||
os.Setenv("FORGE_REPOPB", os.Getenv("FORGE_GOSRC")) // store repos.pb in the same dir
|
||||
if goWorkExists(os.Getenv("FORGE_GOSRC")) {
|
||||
os.Setenv("FORGE_GOWORK", "true")
|
||||
}
|
||||
if os.Getenv("FORGE_PATCHDIR") == "" {
|
||||
os.Setenv("FORGE_PATCHDIR", os.Getenv("FORGE_GOSRC"))
|
||||
}
|
||||
return anyerr
|
||||
}
|
||||
|
||||
// if a go.work file is found, use that location
|
||||
if gowork, ok := findGoWork(); ok {
|
||||
os.Setenv("FORGE_GOSRC", gowork)
|
||||
if os.Getenv("FORGE_GOSRC") == "" {
|
||||
// everything went wrong. use /tmp maybe ?
|
||||
os.Setenv("FORGE_GOSRC", "/tmp")
|
||||
}
|
||||
os.Setenv("FORGE_REPOPB", os.Getenv("FORGE_GOSRC")) // store repos.pb in the same dir
|
||||
os.Setenv("FORGE_GOWORK", "true")
|
||||
if os.Getenv("FORGE_PATCHDIR") == "" {
|
||||
os.Setenv("FORGE_PATCHDIR", os.Getenv("FORGE_GOSRC"))
|
||||
}
|
||||
return anyerr
|
||||
} else {
|
||||
// log.Info("fhelp.findGoWork() didn't find a go.work file")
|
||||
}
|
||||
|
||||
// there are no go.work files, use the default ~/go/src behavior
|
||||
if gosrc, err := useGoSrc(); err == nil {
|
||||
os.Setenv("FORGE_GOSRC", gosrc)
|
||||
os.Unsetenv("FORGE_GOWORK")
|
||||
}
|
||||
|
||||
// set to user home dir unless this is already set
|
||||
if os.Getenv("FORGE_REPOPB") == "" {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err == nil {
|
||||
fullpath := filepath.Join(homeDir, ".cache/forge")
|
||||
if err := os.MkdirAll(fullpath, os.ModePerm); err == nil {
|
||||
os.Setenv("FORGE_REPOPB", fullpath)
|
||||
} else {
|
||||
log.Warn("mkdir failed", fullpath, err)
|
||||
os.Setenv("FORGE_REPOPB", os.Getenv("FORGE_GOSRC")) // store repos.pb in the same dir
|
||||
anyerr = err
|
||||
}
|
||||
} else {
|
||||
log.Info("user home dir error", err)
|
||||
os.Setenv("FORGE_REPOPB", "/tmp")
|
||||
anyerr = err
|
||||
}
|
||||
}
|
||||
if os.Getenv("FORGE_PATCHDIR") == "" {
|
||||
os.Setenv("FORGE_PATCHDIR", os.Getenv("FORGE_GOSRC"))
|
||||
}
|
||||
|
||||
return anyerr
|
||||
}
|
||||
|
||||
/*
|
||||
// look for a go.work file
|
||||
// otherwise use ~/go/src
|
||||
func findGoWork() (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 := digupWorkfile(pwd); err == nil {
|
||||
// log.Info("digupWorkfile() found an existing go.work file", pwd)
|
||||
return pwd, nil
|
||||
} else {
|
||||
// log.Info("digupWorkfile() go.work file error", err)
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
// this shouldn't really happen. maybe your working directory got deleted
|
||||
// log.Info("forge.findGoWork() os.Getwd() was probably deleted", pwd, err)
|
||||
return "", 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
|
||||
}
|
||||
|
||||
// findGoWork searches for a "go.work" file starting from the current directory
|
||||
// and moving up the directory tree. It returns the path to the directory containing
|
||||
// the file and a boolean indicating whether the file was found.
|
||||
func findGoWork() (string, bool) {
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
for {
|
||||
workFilePath := filepath.Join(dir, "go.work")
|
||||
if _, err := os.Stat(workFilePath); err == nil {
|
||||
return dir, true
|
||||
}
|
||||
parent := filepath.Dir(dir)
|
||||
if parent == dir {
|
||||
break // Reached root
|
||||
}
|
||||
dir = parent
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
85
goSrcFind.go
85
goSrcFind.go
|
@ -1,85 +0,0 @@
|
|||
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")
|
||||
}
|
34
goWork.go
34
goWork.go
|
@ -1,34 +0,0 @@
|
|||
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
|
||||
}
|
Loading…
Reference in New Issue