command: use meta struct for common options

This commit is contained in:
Ryan Uber
2015-09-14 13:13:52 -07:00
parent 34d77eeb04
commit 7e26f938a2
20 changed files with 173 additions and 300 deletions

View File

@@ -1,15 +1,12 @@
package command
import (
"flag"
"fmt"
"strings"
"github.com/mitchellh/cli"
)
type NodeDrainCommand struct {
Ui cli.Ui
Meta
}
func (c *NodeDrainCommand) Help() string {
@@ -19,21 +16,17 @@ Usage: nomad node-drain [options] <node>
Toggles node draining on a specified node. It is required
that either -enable or -disable is specified, but not both.
Options:
General Options:
` + generalOptionsUsage() + `
Node Drain Options:
-disable
Disable draining for the specified node.
-enable
Enable draining for the specified node.
-help
Display this message
-http-addr
Address of the Nomad API to connect. Can also be specified
using the environment variable NOMAD_HTTP_ADDR.
Default = http://127.0.0.1:4646
`
return strings.TrimSpace(helpText)
}
@@ -43,14 +36,12 @@ func (c *NodeDrainCommand) Synopsis() string {
}
func (c *NodeDrainCommand) Run(args []string) int {
var httpAddr *string
var enable, disable bool
flags := flag.NewFlagSet("node-drain", flag.ContinueOnError)
flags := c.Meta.FlagSet("node-drain", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&enable, "enable", false, "Enable drain mode")
flags.BoolVar(&disable, "disable", false, "Disable drain mode")
httpAddr = httpAddrFlag(flags)
if err := flags.Parse(args); err != nil {
return 1
@@ -63,22 +54,23 @@ func (c *NodeDrainCommand) Run(args []string) int {
}
// Check that we got a node ID
if len(flags.Args()) != 1 {
args = flags.Args()
if len(args) != 1 {
c.Ui.Error(c.Help())
return 1
}
nodeID := flags.Args()[0]
nodeID := args[0]
// Get the HTTP client
client, err := httpClient(*httpAddr)
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed initializing Nomad client: %s", err))
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
// Toggle node draining
if _, err := client.Nodes().ToggleDrain(nodeID, enable, nil); err != nil {
c.Ui.Error(fmt.Sprintf("Failed to toggle drain mode: %s", err))
c.Ui.Error(fmt.Sprintf("Error toggling drain mode: %s", err))
return 1
}
return 0