From e0d6d35b56fdbe562e6e7be6c888b57e018264a2 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Fri, 22 Mar 2024 01:34:43 -0400 Subject: [PATCH] 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. --- entry/entry.go | 6 ++++++ main.go | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/entry/entry.go b/entry/entry.go index c2eef116..33a0bef9 100644 --- a/entry/entry.go +++ b/entry/entry.go @@ -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. diff --git a/main.go b/main.go index 71483dee..09c4ab98 100644 --- a/main.go +++ b/main.go @@ -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)