entry: Execute embedded commands if you use their correct name

I think this is the more intuitive way to do things. Force the user the
explicitly choose the embedded command which prevents accidental running
of a different command, and allows us to potentially have multiple
commands in the future.
This commit is contained in:
James Shubin
2024-03-22 01:34:43 -04:00
parent 8cf7719476
commit e0d6d35b56
2 changed files with 8 additions and 1 deletions

View File

@@ -170,6 +170,12 @@ type Runner struct {
data *Data
}
// Name returns the name of the program, which should match the argv[1] of what
// we want to use to call this from the top-level main.
func (obj *Runner) Name() string {
return obj.data.Program
}
// CLI is the entry point for using any embedded package from the CLI. It is
// used as the main entry point from the top-level main function and kicks-off
// the CLI parser.

View File

@@ -90,7 +90,8 @@ func main() {
}
// is there an alternate entry point for the cli?
if cli, err := entry.Lookup(); err == nil {
if cli, err := entry.Lookup(); err == nil && len(data.Args) > 1 && cli.Name() == data.Args[1] {
data.Args = data.Args[1:] // pop off "argv[0]"
if err := cli.CLI(context.Background(), data); err != nil {
fmt.Println(err)
os.Exit(1)