Fix bug with caller

This commit is contained in:
rwxrob 2022-03-26 17:12:50 -04:00
parent 276e1cfa63
commit d821e800d4
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77
4 changed files with 28 additions and 20 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
go.work

View File

@ -34,24 +34,30 @@ import (
func init() {
var err error
// get the full path to current running process executable
Exe, err = os.Executable()
ExePath, err = os.Executable()
if err != nil {
log.Print(err)
return
}
Exe, err = filepath.EvalSymlinks(Exe)
ExePath, err = filepath.EvalSymlinks(ExePath)
if err != nil {
log.Print(err)
}
ExeName = strings.TrimSuffix(
filepath.Base(ExePath), filepath.Ext(ExePath))
}
// Exe holds the full path to the current running process executable
// ExePath holds the full path to the current running process executable
// which is determined at init() time by calling os.Executable and
// passing it to path/filepath.EvalSymlinks to ensure it is the actual
// binary executable file. Errors are reported to stderr, but there
// should never be an error logged unless something is wrong with the Go
// runtime environment.
var Exe string
var ExePath string
// ExeName holds just the base name of the executable without any suffix
// (ex: .exe).
var ExeName string
// ReplaceSelf replaces the current running executable at its current
// location with the successfully retrieved file at the specified URL or

View File

@ -8,7 +8,6 @@ import (
"os"
"github.com/rwxrob/bonzai"
"github.com/rwxrob/bonzai/util"
"github.com/rwxrob/fn/each"
)
@ -21,7 +20,7 @@ func ExampleArgsFrom() {
}
func ExampleFiles() {
each.Println(util.Files("testdata/files"))
each.Println(bonzai.Files("testdata/files"))
// Output:
// testdata/files/bar
// testdata/files/blah
@ -32,7 +31,7 @@ func ExampleFiles() {
}
func ExampleFiles_spaces() {
each.Println(util.Files("testdata/files/dir1"))
each.Println(bonzai.Files("testdata/files/dir1"))
// Output:
// testdata/files/dir1/some\ thing
}
@ -40,7 +39,7 @@ func ExampleFiles_spaces() {
func ExampleFiles_empty() {
os.Chdir("testdata/files")
defer os.Chdir("../..")
each.Println(util.Files(""))
each.Println(bonzai.Files(""))
// Output:
// bar
// blah
@ -51,7 +50,7 @@ func ExampleFiles_empty() {
}
func ExampleFiles_not_Directory() {
fmt.Println(util.Files("none"))
fmt.Println(bonzai.Files("none"))
// Output:
// []
}

24
cmd.go
View File

@ -95,21 +95,18 @@ func (x *Cmd) Run() {
// default to first Command if no Call defined
if cmd.Call == nil {
if cmd.Commands != nil {
def := cmd.Commands[0]
if def.Call == nil {
ExitError("default command \"%v\" must be callable", def.Name)
}
if err := def.Call(x, args...); err != nil {
ExitError(err)
}
Exit()
if len(cmd.Commands) > 0 {
os.Args = append(os.Args, cmd.Commands[0].Name)
cmd.Commands[0].Caller = cmd
cmd.Commands[0].Run()
return
} else {
ExitError(x.Unimplemented())
}
ExitError(x.UsageError())
}
// delegate
if err := cmd.Call(x, args...); err != nil {
if err := cmd.Call(cmd, args...); err != nil {
ExitError(err)
}
Exit()
@ -120,6 +117,11 @@ func (x *Cmd) UsageError() error {
return fmt.Errorf("usage: %v %v\n", x.Name, x.Usage)
}
// Unimplemented returns an error with a single-line usage string.
func (x *Cmd) Unimplemented() error {
return fmt.Errorf("%q has not yet been implemented", x.Name)
}
// Add creates a new Cmd and sets the name and aliases and adds to
// Commands returning a reference to the new Cmd. The name must be
// first.