Merge pull request #901 from djfarrelly/filter-create-help-cmd

Filter create command flags by driver
This commit is contained in:
Evan Hazlett 2015-04-10 16:20:49 -04:00
commit 7b70d83579
2 changed files with 79 additions and 31 deletions

View File

@ -161,15 +161,7 @@ func setupCertificates(caCertPath, caKeyPath, clientCertPath, clientKeyPath stri
return nil return nil
} }
var Commands = []cli.Command{ var sharedCreateFlags = []cli.Flag{
{
Name: "active",
Usage: "Get or set the active machine",
Action: cmdActive,
},
{
Flags: append(
drivers.GetCreateFlags(),
cli.StringFlag{ cli.StringFlag{
Name: "driver, d", Name: "driver, d",
Usage: fmt.Sprintf( Usage: fmt.Sprintf(
@ -201,6 +193,18 @@ var Commands = []cli.Command{
Usage: "addr to advertise for Swarm (default: detect and use the machine IP)", Usage: "addr to advertise for Swarm (default: detect and use the machine IP)",
Value: "", Value: "",
}, },
}
var Commands = []cli.Command{
{
Name: "active",
Usage: "Get or set the active machine",
Action: cmdActive,
},
{
Flags: append(
drivers.GetCreateFlags(),
sharedCreateFlags...,
), ),
Name: "create", Name: "create",
Usage: "Create a machine", Usage: "Create a machine",
@ -365,10 +369,40 @@ func cmdActive(c *cli.Context) {
} }
} }
// If the user has specified a driver, they should not see the flags for all
// of the drivers in `docker-machine create`. This method replaces the 100+
// create flags with only the ones applicable to the driver specified
func trimDriverFlags(driver string, cmds []cli.Command) ([]cli.Command, error) {
filteredCmds := cmds
driverFlags, err := drivers.GetCreateFlagsForDriver(driver)
if err != nil {
return nil, err
}
for i, cmd := range cmds {
if cmd.HasName("create") {
filteredCmds[i].Flags = append(driverFlags, sharedCreateFlags...)
}
}
return filteredCmds, nil
}
func cmdCreate(c *cli.Context) { func cmdCreate(c *cli.Context) {
var (
err error
)
driver := c.String("driver") driver := c.String("driver")
name := c.Args().First() name := c.Args().First()
// TODO: Not really a fan of "none" as the default driver...
if driver != "none" {
c.App.Commands, err = trimDriverFlags(driver, c.App.Commands)
if err != nil {
log.Fatal(err)
}
}
if name == "" { if name == "" {
cli.ShowCommandHelp(c, "create") cli.ShowCommandHelp(c, "create")
log.Fatal("You must specify a machine name") log.Fatal("You must specify a machine name")

View File

@ -144,6 +144,20 @@ func GetCreateFlags() []cli.Flag {
return flags return flags
} }
func GetCreateFlagsForDriver(name string) ([]cli.Flag, error) {
for driverName := range drivers {
if name == driverName {
driver := drivers[driverName]
flags := driver.GetCreateFlags()
sort.Sort(ByFlagName(flags))
return flags, nil
}
}
return nil, fmt.Errorf("Driver %s not found", name)
}
// GetDriverNames returns a slice of all registered driver names // GetDriverNames returns a slice of all registered driver names
func GetDriverNames() []string { func GetDriverNames() []string {
names := make([]string, 0, len(drivers)) names := make([]string, 0, len(drivers))