Merge pull request #233 from nathanleclaire/funccmds

Move closures to be named functions
This commit is contained in:
Evan Hazlett 2015-01-07 17:17:10 -08:00
commit 68c80b5ae6
1 changed files with 264 additions and 238 deletions

View File

@ -48,7 +48,101 @@ var Commands = []cli.Command{
{ {
Name: "active", Name: "active",
Usage: "Get or set the active machine", Usage: "Get or set the active machine",
Action: func(c *cli.Context) { Action: cmdActive,
},
{
Flags: append(
drivers.GetCreateFlags(),
cli.StringFlag{
Name: "driver, d",
Usage: fmt.Sprintf(
"Driver to create machine with. Available drivers: %s",
strings.Join(drivers.GetDriverNames(), ", "),
),
Value: "none",
},
),
Name: "create",
Usage: "Create a machine",
Action: cmdCreate,
},
{
Name: "inspect",
Usage: "Inspect information about a machine",
Action: cmdInspect,
},
{
Name: "ip",
Usage: "Get the IP address of a machine",
Action: cmdIp,
},
{
Name: "kill",
Usage: "Kill a machine",
Action: cmdKill,
},
{
Flags: []cli.Flag{
cli.BoolFlag{
Name: "quiet, q",
Usage: "Enable quiet mode",
},
},
Name: "ls",
Usage: "List machines",
Action: cmdLs,
},
{
Name: "restart",
Usage: "Restart a machine",
Action: cmdRestart,
},
{
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force, f",
Usage: "Remove local configuration even if machine cannot be removed",
},
},
Name: "rm",
Usage: "Remove a machine",
Action: cmdRm,
},
{
Flags: []cli.Flag{
cli.StringFlag{
Name: "command, c",
Usage: "SSH Command",
Value: "",
},
},
Name: "ssh",
Usage: "Log into or run a command on a machine with SSH",
Action: cmdSsh,
},
{
Name: "start",
Usage: "Start a machine",
Action: cmdStart,
},
{
Name: "stop",
Usage: "Stop a machine",
Action: cmdStop,
},
{
Name: "upgrade",
Usage: "Upgrade a machine to the latest version of Docker",
Action: cmdUpgrade,
},
{
Name: "url",
Usage: "Get the URL of a machine",
Action: cmdUrl,
},
}
func cmdActive(c *cli.Context) {
name := c.Args().First() name := c.Args().First()
store := NewStore(c.GlobalString("storage-path")) store := NewStore(c.GlobalString("storage-path"))
@ -72,23 +166,9 @@ var Commands = []cli.Command{
} else { } else {
cli.ShowCommandHelp(c, "active") cli.ShowCommandHelp(c, "active")
} }
}, }
},
{ func cmdCreate(c *cli.Context) {
Flags: append(
drivers.GetCreateFlags(),
cli.StringFlag{
Name: "driver, d",
Usage: fmt.Sprintf(
"Driver to create machine with. Available drivers: %s",
strings.Join(drivers.GetDriverNames(), ", "),
),
Value: "none",
},
),
Name: "create",
Usage: "Create a machine",
Action: func(c *cli.Context) {
driver := c.String("driver") driver := c.String("driver")
name := c.Args().First() name := c.Args().First()
@ -117,51 +197,33 @@ var Commands = []cli.Command{
} }
log.Infof("%q has been created and is now the active machine. To point Docker at this machine, run: export DOCKER_HOST=$(machine url) DOCKER_AUTH=identity", name) log.Infof("%q has been created and is now the active machine. To point Docker at this machine, run: export DOCKER_HOST=$(machine url) DOCKER_AUTH=identity", name)
}, }
},
{ func cmdInspect(c *cli.Context) {
Name: "inspect",
Usage: "Inspect information about a machine",
Action: func(c *cli.Context) {
prettyJSON, err := json.MarshalIndent(getHost(c), "", " ") prettyJSON, err := json.MarshalIndent(getHost(c), "", " ")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Println(string(prettyJSON)) fmt.Println(string(prettyJSON))
}, }
},
{ func cmdIp(c *cli.Context) {
Name: "ip",
Usage: "Get the IP address of a machine",
Action: func(c *cli.Context) {
ip, err := getHost(c).Driver.GetIP() ip, err := getHost(c).Driver.GetIP()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Println(ip) fmt.Println(ip)
}, }
},
{ func cmdKill(c *cli.Context) {
Name: "kill",
Usage: "Kill a machine",
Action: func(c *cli.Context) {
if err := getHost(c).Driver.Kill(); err != nil { if err := getHost(c).Driver.Kill(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
}, }
},
{ func cmdLs(c *cli.Context) {
Flags: []cli.Flag{
cli.BoolFlag{
Name: "quiet, q",
Usage: "Enable quiet mode",
},
},
Name: "ls",
Usage: "List machines",
Action: func(c *cli.Context) {
quiet := c.Bool("quiet") quiet := c.Bool("quiet")
store := NewStore(c.GlobalString("storage-path")) store := NewStore(c.GlobalString("storage-path"))
@ -207,27 +269,15 @@ var Commands = []cli.Command{
} }
w.Flush() w.Flush()
}, }
},
{ func cmdRestart(c *cli.Context) {
Name: "restart",
Usage: "Restart a machine",
Action: func(c *cli.Context) {
if err := getHost(c).Driver.Restart(); err != nil { if err := getHost(c).Driver.Restart(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
}, }
},
{ func cmdRm(c *cli.Context) {
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force, f",
Usage: "Remove local configuration even if machine cannot be removed",
},
},
Name: "rm",
Usage: "Remove a machine",
Action: func(c *cli.Context) {
if len(c.Args()) == 0 { if len(c.Args()) == 0 {
cli.ShowCommandHelp(c, "rm") cli.ShowCommandHelp(c, "rm")
log.Fatal("You must specify a machine name") log.Fatal("You must specify a machine name")
@ -247,19 +297,9 @@ var Commands = []cli.Command{
if isError { if isError {
log.Fatal("There was an error removing a machine. To force remove it, pass the -f option. Warning: this might leave it running on the provider.") log.Fatal("There was an error removing a machine. To force remove it, pass the -f option. Warning: this might leave it running on the provider.")
} }
}, }
},
{ func cmdSsh(c *cli.Context) {
Flags: []cli.Flag{
cli.StringFlag{
Name: "command, c",
Usage: "SSH Command",
Value: "",
},
},
Name: "ssh",
Usage: "Log into or run a command on a machine with SSH",
Action: func(c *cli.Context) {
name := c.Args().First() name := c.Args().First()
store := NewStore(c.GlobalString("storage-path")) store := NewStore(c.GlobalString("storage-path"))
@ -298,47 +338,33 @@ var Commands = []cli.Command{
if err := sshCmd.Run(); err != nil { if err := sshCmd.Run(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
}, }
},
{ func cmdStart(c *cli.Context) {
Name: "start",
Usage: "Start a machine",
Action: func(c *cli.Context) {
if err := getHost(c).Start(); err != nil { if err := getHost(c).Start(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
}, }
},
{ func cmdStop(c *cli.Context) {
Name: "stop",
Usage: "Stop a machine",
Action: func(c *cli.Context) {
if err := getHost(c).Stop(); err != nil { if err := getHost(c).Stop(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
}, }
},
{ func cmdUpgrade(c *cli.Context) {
Name: "upgrade",
Usage: "Upgrade a machine to the latest version of Docker",
Action: func(c *cli.Context) {
if err := getHost(c).Upgrade(); err != nil { if err := getHost(c).Upgrade(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
}, }
},
{ func cmdUrl(c *cli.Context) {
Name: "url",
Usage: "Get the URL of a machine",
Action: func(c *cli.Context) {
url, err := getHost(c).GetURL() url, err := getHost(c).GetURL()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Println(url) fmt.Println(url)
},
},
} }
func getHost(c *cli.Context) *Host { func getHost(c *cli.Context) *Host {