fhelp/configureENV.go

130 lines
3.7 KiB
Go

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
//
func ConfigureENV() error {
err := doConfigureENV()
if os.Getenv("FORGE_VERBOSE") == "true" {
log.Printf("FORGE_CONFIG = %s\n", os.Getenv("FORGE_CONFIG"))
log.Printf("FORGE_REPOPB = %s\n", os.Getenv("FORGE_REPOPB"))
log.Printf("FORGE_PATCHDIR = %s\n", os.Getenv("FORGE_PATCHDIR"))
log.Printf("FORGE_URL = %s\n", os.Getenv("FORGE_URL"))
log.Printf("FORGE_GOWORK = %v\n", os.Getenv("FORGE_GOWORK"))
log.Printf("FORGE_VERBOSE = %s\n", os.Getenv("FORGE_VERBOSE"))
log.Printf("HOSTNAME = %s\n", os.Getenv("HOSTNAME"))
}
return err
}
// set the ENV vars
// always set them to _something_ even when everything seems to be failing
func doConfigureENV() 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")
}
}
return anyerr
}
// 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
}