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:
@@ -170,6 +170,12 @@ type Runner struct {
|
|||||||
data *Data
|
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
|
// 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
|
// used as the main entry point from the top-level main function and kicks-off
|
||||||
// the CLI parser.
|
// the CLI parser.
|
||||||
|
|||||||
3
main.go
3
main.go
@@ -90,7 +90,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// is there an alternate entry point for the cli?
|
// 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 {
|
if err := cli.CLI(context.Background(), data); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user