Compare commits

...

13 Commits

Author SHA1 Message Date
Jeff Carr f6b19522c6 deprecate old ENV var 2025-09-13 05:32:51 -05:00
Jeff Carr de7b14f3c6 DumpENV() 2025-09-12 10:12:21 -05:00
Jeff Carr 5ae5010b00 cleaning up ENV handling 2025-09-12 02:03:07 -05:00
Jeff Carr 2add723f12 rm GOSRC 2025-09-11 23:10:33 -05:00
Jeff Carr c5780cc333 code notes for later 2025-09-11 01:53:44 -05:00
Jeff Carr 2b7457b2f7 can auto create ~/.local/share/bash file 2025-09-08 15:36:48 -05:00
Jeff Carr 5f480a1583 general bash help for go-args 2025-09-08 14:58:08 -05:00
Jeff Carr b97c502b54 make Global. duh. 2025-09-06 20:08:54 -05:00
Jeff Carr fc6b049b37 help the user build the plugins 2025-09-04 14:44:35 -05:00
Jeff Carr 1096571f39 duh. use an ENV for this 2025-09-04 08:35:43 -05:00
Jeff Carr 41851ed584 a verbose option 2025-09-03 20:50:44 -05:00
Jeff Carr fda4b5448b fix env PATCHDIR 2025-09-03 14:04:29 -05:00
Jeff Carr fc25cd8b05 rewrite a standard ENV function 2025-09-03 13:54:12 -05:00
7 changed files with 532 additions and 119 deletions

86
bash.go Normal file
View File

@ -0,0 +1,86 @@
package fhelp
// for use with Alex Flint's go-args package
import (
"fmt"
"os"
"path/filepath"
"go.wit.com/lib/gui/shell"
)
// makes a bash autocomplete file for your command
func DoBash(argname string) {
if homeDir, err := os.UserHomeDir(); err == nil {
filename := filepath.Join(homeDir, ".local/share/bash-completion/completions", argname)
if shell.Exists(filename) {
} else {
if f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
f.Write([]byte(MakeBashCompletionText(argname)))
f.Close()
}
fmt.Println("bash file created:", filename)
}
}
fmt.Println(MakeBashCompletionText(argname))
fmt.Println("restart bash")
os.Exit(0)
}
func MakeBashCompletionText(argname string) string {
var out string
out += fmt.Sprintf("# add this in your bashrc:\n")
out += fmt.Sprintf("\n")
out += fmt.Sprintf("# todo: add this to go-arg as a 'hidden' go-arg option --bash\n")
out += fmt.Sprintf("#\n")
out += fmt.Sprintf("# Put the below in the file: ~/.local/share/bash-completion/completions/%s\n", argname)
out += fmt.Sprintf("#\n")
out += fmt.Sprintf("# todo: make this output work/parse with:\n")
out += fmt.Sprintf("# complete -C %s --bash go\n", argname)
out += fmt.Sprintf("\n")
out += fmt.Sprintf("_%s_complete()\n", argname)
out += fmt.Sprintf("{\n")
out += fmt.Sprintf(" # sets local to this func vars\n")
out += fmt.Sprintf(" local cur prev all\n")
out += fmt.Sprintf(" cur=${COMP_WORDS[COMP_CWORD]}\n")
out += fmt.Sprintf(" prev=${COMP_WORDS[COMP_CWORD-1]}\n")
out += fmt.Sprintf(" all=${COMP_WORDS[@]}\n")
out += fmt.Sprintf("\n")
out += fmt.Sprintf(" # this is where we generate the go-arg output\n")
out += fmt.Sprintf(" GOARGS=$(%s --auto-complete $prev \\'$cur\\' $all)\n", argname)
out += fmt.Sprintf("\n")
out += fmt.Sprintf(" # this compares the command line input from the user\n")
out += fmt.Sprintf(" # to whatever strings we output\n")
out += fmt.Sprintf(" COMPREPLY=( $(compgen -W \"$GOARGS\" -- $cur) ) # THIS WORKS\n")
out += fmt.Sprintf(" return 0\n")
out += fmt.Sprintf("}\n")
out += fmt.Sprintf("complete -F _%s_complete %s\n", argname, argname)
out += fmt.Sprintf("\n")
out += fmt.Sprintf("# copy and paste the above into your bash shell should work\n")
return out
}
/*
// prints help to STDERR // TODO: move everything below this to go-args
func (args) doBashHelp() {
if argv.BashAuto[1] != "''" {
// if this is not blank, then the user has typed something
return
}
if argv.BashAuto[0] != ARGNAME {
// if this is not the name of the command, the user already started doing something
return
}
if argv.BashAuto[0] == ARGNAME {
me.pp.WriteHelp(os.Stderr)
return
}
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "hello world")
fmt.Fprintln(os.Stderr, "")
}
*/

39
buildPlugin.go Normal file
View File

@ -0,0 +1,39 @@
package fhelp
import (
"go.wit.com/lib/gui/shell"
"go.wit.com/log"
)
func BuildPlugin(pname string) bool {
if CheckProtoc() {
log.Info("You have the right tools for protobufs")
} else {
log.Info("You do not have the right tools for protobufs")
}
log.Info("")
log.Info("### Error ####")
log.Info("The GUI golang plugins did not load for the", pname)
log.Info("You will have to rebuild them")
log.Info("go-clone go.wit.com/toolkits/<pick one>/")
log.Info("TODO: try to rebuild them here")
log.Info("TODO: falling back to STDIN interface")
log.Info("### Error ####")
log.Info("")
log.Infof("To attempt to build the %s plugin, run:\n", pname)
log.Info("forge --build go.wit.com/toolkits/gocui")
log.Info("")
if !QuestionUser("Would you like to run that now?") {
return false
}
r := shell.RunRealtime([]string{"forge", "--build", "go.wit.com/toolkits/" + pname})
if r.Error == nil {
log.Info("build worked")
log.Info("You must copy that file to ~/go/lib/go-gui/")
return true
} else {
log.Info("build failed")
}
return false
}

219
configureENV.go Normal file
View File

@ -0,0 +1,219 @@
package fhelp
import (
"os"
"path/filepath"
"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
//
// 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_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
//
// 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" {
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
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
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
}
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) {
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
}

158
doDebug.go Normal file
View File

@ -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
}
*/

View File

@ -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")
}

View File

@ -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
}

30
questionUser.go Normal file
View File

@ -0,0 +1,30 @@
package fhelp
import (
"bufio"
"fmt"
"os"
"strings"
"go.wit.com/log"
)
func QuestionUser(msg string) bool {
log.Info(msg)
fmt.Fprintf(os.Stdout, "(y)es or (n)o ? ")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
line = strings.ToLower(line)
switch line {
case "y":
return true
case "n":
return false
default:
}
}
return false
}