refactor command router initialization

Issue #74 closed
Jon
repo owner created an issue

The current command router is currently initialized with

// Initialize uru's main command router
func initCommandRouter(r *command.CommandRouter) {
    r.Handle([]string{`admin`}, command.Admin)
    r.Handle([]string{`gem`}, command.Gem)
    r.Handle([]string{`help`}, command.Help)
    r.Handle([]string{`ls`, `list`}, command.List)
    r.Handle([]string{`ruby`, `rb`}, command.Ruby)
    r.Handle([]string{`ver`, `version`}, command.Version)
}

in which this initialization code must have a priori knowledge of supported sub-commands. This is exactly opposite of what is needed. Sub-commands (and plugins) should inject themselves by registering with the router during sub-command initialization.

In addition to changes required to the router and overall initialization, implement a Command type similar to the following:

package cmd

type Command struct {
    // Command name
    Name string

    // Command aliases
    Aliases []string

    // Single line usage demonstrating how to use this command
    Usage string

    // Single line summarizing this command
    Short string

    // Multiple line description of this command
    Long string

    // The command's function invoked by the command router
    Run func(ctx *env.Context)
}

// Runnable indicates whether the command can be invoked. Non runnable commands are
// information only commands.
func (t *Command) Runnable() bool { return t.Run != nil }

Comments (2)

  1. Jon reporter
    • edited description

    Remove CommandRegistry and AdminCmdRegistry maps as the updated command router also functions as a command registry.

  2. Log in to comment