Compare commits
5 Commits
Author | SHA1 | Date |
---|---|---|
|
f6b19522c6 | |
|
de7b14f3c6 | |
|
5ae5010b00 | |
|
2add723f12 | |
|
c5780cc333 |
190
configureENV.go
190
configureENV.go
|
@ -7,6 +7,27 @@ import (
|
|||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
// BEFORE YOU READ ANYTHING UNDERSTAND THIS IS THE DEFAULT:
|
||||
//
|
||||
// ~/.config/ # put things you might care about here
|
||||
// ~/.cache/ # this is never imporant and can be deleted at any time
|
||||
//
|
||||
//
|
||||
// Additionally:
|
||||
//
|
||||
// NEVER WRITE OUT ANYTHING TO ~/ EVER. The ONLY DIRECTORIES YOU ARE EVER ALLOWED TO USE ARE ~/.config and ~/.cache/ (maybe ~/.local but really, why?)
|
||||
//
|
||||
// There are no exceptions to this unless you are a jerk or don't know better (in which you will be easily forgiven -- don't worry. there isn't a manual)
|
||||
//
|
||||
//
|
||||
// ADVICE FOR ALL FUTURE PROGRAMMERS:
|
||||
//
|
||||
//
|
||||
// Config files are a perfect way to stop things from proceeding. You do NOT want options in your programs. Configuration options, if you do
|
||||
// things correctly, should never actually be needed. Nonetheless, there are initial states. ISOLATE ALL OPTIONS TO THE ITIAL STATES.
|
||||
// then, never let anthing change anything. If your code is correct, then all is good.
|
||||
//
|
||||
//
|
||||
// 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
|
||||
|
@ -18,8 +39,7 @@ import (
|
|||
//
|
||||
// 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_REPOSPB == 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
|
||||
//
|
||||
|
@ -32,34 +52,47 @@ import (
|
|||
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_GOSRC = %s\n", os.Getenv("FORGE_GOSRC"))
|
||||
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"))
|
||||
DumpENV("fhelp:")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func DumpENV(what string) {
|
||||
log.Printf("%s FORGE_REPOSDIR = %s\n", what, os.Getenv("FORGE_REPOSDIR"))
|
||||
log.Printf("%s FORGE_REPOSPB = %s\n", what, os.Getenv("FORGE_REPOSPB"))
|
||||
log.Printf("%s FORGE_PATCHDIR = %s\n", what, os.Getenv("FORGE_PATCHDIR"))
|
||||
log.Printf("%s FORGE_URL = %s\n", what, os.Getenv("FORGE_URL"))
|
||||
log.Printf("%s FORGE_GOWORK = %s\n", what, os.Getenv("FORGE_GOWORK"))
|
||||
log.Printf("%s FORGE_VERBOSE = %s\n", what, os.Getenv("FORGE_VERBOSE"))
|
||||
log.Printf("%s HOSTNAME = %s\n", what, os.Getenv("HOSTNAME"))
|
||||
}
|
||||
|
||||
// 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
|
||||
if os.Getenv("FORGE_REPOSDIR") == "" {
|
||||
reposDir, err := getReposDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
os.Setenv("FORGE_REPOSDIR", reposDir)
|
||||
}
|
||||
|
||||
if os.Getenv("FORGE_REPOSPB") == "" {
|
||||
pbdir, err := getCacheDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
os.Setenv("FORGE_REPOSPB", filepath.Join(pbdir, "repos.pb"))
|
||||
}
|
||||
|
||||
if os.Getenv("FORGE_PATCHDIR") == "" {
|
||||
pbdir, err := getCacheDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
os.Setenv("FORGE_PATCHDIR", pbdir)
|
||||
}
|
||||
|
||||
// setting FORGE_URL
|
||||
|
@ -75,68 +108,65 @@ func doConfigureENV() error {
|
|||
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_REPOPB"))
|
||||
}
|
||||
|
||||
return anyerr
|
||||
}
|
||||
|
||||
func getConfigDir() (string, error) {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "/tmp", err
|
||||
}
|
||||
fullpath := filepath.Join(homeDir, ".config/forge")
|
||||
err = os.MkdirAll(fullpath, os.ModePerm)
|
||||
if err != nil {
|
||||
return "/tmp", err
|
||||
}
|
||||
|
||||
return fullpath, nil
|
||||
}
|
||||
|
||||
func getCacheDir() (string, error) {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "/tmp", err
|
||||
}
|
||||
fullpath := filepath.Join(homeDir, ".cache/forge")
|
||||
err = os.MkdirAll(fullpath, os.ModePerm)
|
||||
if err != nil {
|
||||
return "/tmp", err
|
||||
}
|
||||
|
||||
return fullpath, nil
|
||||
}
|
||||
|
||||
func getReposDir() (string, error) {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "/tmp", err
|
||||
}
|
||||
fullpath := filepath.Join(homeDir, "go/src")
|
||||
err = os.MkdirAll(fullpath, os.ModePerm)
|
||||
if err != nil {
|
||||
return "/tmp", err
|
||||
}
|
||||
|
||||
return fullpath, nil
|
||||
}
|
||||
|
||||
func getCachegDir() (string, error) {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "/tmp", err
|
||||
}
|
||||
fullpath := filepath.Join(homeDir, ".cache/forge")
|
||||
err = os.MkdirAll(fullpath, os.ModePerm)
|
||||
if err != nil {
|
||||
return "/tmp", err
|
||||
}
|
||||
|
||||
return fullpath, nil
|
||||
}
|
||||
|
||||
// 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) {
|
||||
|
|
|
@ -0,0 +1,158 @@
|
|||
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
|
||||
// Use of this source code is governed by the GPL 3.0
|
||||
|
||||
package fhelp
|
||||
|
||||
// just some old code. rm after the dust has settled
|
||||
|
||||
/*
|
||||
func doDebug() {
|
||||
me.forge = forgepb.InitPB()
|
||||
me.forge.ScanGoSrc()
|
||||
if err := me.forge.ConfigSave(); err != nil {
|
||||
if err := me.forge.Repos.ConfigSave(); err != nil {
|
||||
err := ValidateProtoUTF8(me.forge.Repos)
|
||||
if err != nil {
|
||||
log.Printf("Protobuf UTF-8 validation failed: %v\n", err)
|
||||
}
|
||||
if err := bugpb.SanitizeProtoUTF8(me.forge.Repos); err != nil {
|
||||
log.Fatalf("Sanitization failed: %v", err)
|
||||
}
|
||||
}
|
||||
// badExit(err)
|
||||
}
|
||||
me.forge.SetConfigSave(true)
|
||||
me.forge.Exit()
|
||||
okExit("this never runs")
|
||||
}
|
||||
|
||||
// ValidateProtoUTF8 checks all string fields in a proto.Message recursively.
|
||||
func ValidateProtoUTF8(msg proto.Message) error {
|
||||
return validateValue(reflect.ValueOf(msg), "")
|
||||
}
|
||||
|
||||
func validateValue(val reflect.Value, path string) error {
|
||||
if !val.IsValid() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if val.Kind() == reflect.Ptr {
|
||||
if val.IsNil() {
|
||||
return nil
|
||||
}
|
||||
return validateValue(val.Elem(), path)
|
||||
}
|
||||
|
||||
switch val.Kind() {
|
||||
case reflect.Struct:
|
||||
for i := 0; i < val.NumField(); i++ {
|
||||
field := val.Field(i)
|
||||
fieldType := val.Type().Field(i)
|
||||
fieldPath := fmt.Sprintf("%s.%s", path, fieldType.Name)
|
||||
if err := validateValue(field, fieldPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
case reflect.String:
|
||||
s := val.String()
|
||||
if !utf8.ValidString(s) {
|
||||
return fmt.Errorf("invalid UTF-8 string at %s: %q", path, s)
|
||||
}
|
||||
|
||||
case reflect.Slice:
|
||||
if val.Type().Elem().Kind() == reflect.Uint8 {
|
||||
return nil // skip []byte
|
||||
}
|
||||
for i := 0; i < val.Len(); i++ {
|
||||
if err := validateValue(val.Index(i), fmt.Sprintf("%s[%d]", path, i)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
case reflect.Map:
|
||||
for _, key := range val.MapKeys() {
|
||||
valItem := val.MapIndex(key)
|
||||
if err := validateValue(valItem, fmt.Sprintf("%s[%v]", path, key)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SanitizeProtoUTF8 fixes all invalid UTF-8 strings in a proto.Message recursively.
|
||||
func SanitizeProtoUTF8(msg proto.Message) error {
|
||||
return sanitizeValue(reflect.ValueOf(msg), "")
|
||||
}
|
||||
|
||||
func sanitizeValue(val reflect.Value, path string) error {
|
||||
if !val.IsValid() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if val.Kind() == reflect.Ptr {
|
||||
if val.IsNil() {
|
||||
return nil
|
||||
}
|
||||
return sanitizeValue(val.Elem(), path)
|
||||
}
|
||||
|
||||
switch val.Kind() {
|
||||
case reflect.Struct:
|
||||
for i := 0; i < val.NumField(); i++ {
|
||||
field := val.Field(i)
|
||||
fieldType := val.Type().Field(i)
|
||||
if !field.CanSet() {
|
||||
continue
|
||||
}
|
||||
if err := sanitizeValue(field, fmt.Sprintf("%s.%s", path, fieldType.Name)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
case reflect.String:
|
||||
s := val.String()
|
||||
if !utf8.ValidString(s) {
|
||||
utf8Str, err := latin1ToUTF8(s)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to convert %s to UTF-8: %v", path, err)
|
||||
}
|
||||
val.SetString(utf8Str)
|
||||
}
|
||||
|
||||
case reflect.Slice:
|
||||
if val.Type().Elem().Kind() == reflect.Uint8 {
|
||||
return nil // skip []byte
|
||||
}
|
||||
for i := 0; i < val.Len(); i++ {
|
||||
if err := sanitizeValue(val.Index(i), fmt.Sprintf("%s[%d]", path, i)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
case reflect.Map:
|
||||
for _, key := range val.MapKeys() {
|
||||
valItem := val.MapIndex(key)
|
||||
newItem := reflect.New(valItem.Type()).Elem()
|
||||
newItem.Set(valItem)
|
||||
if err := sanitizeValue(newItem, fmt.Sprintf("%s[%v]", path, key)); err != nil {
|
||||
return err
|
||||
}
|
||||
val.SetMapIndex(key, newItem)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func latin1ToUTF8(input string) (string, error) {
|
||||
reader := charmap.ISO8859_1.NewDecoder().Reader(bytes.NewReader([]byte(input)))
|
||||
result, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(result), nil
|
||||
}
|
||||
*/
|
Loading…
Reference in New Issue