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",
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()
store := NewStore(c.GlobalString("storage-path"))
@ -72,23 +166,9 @@ var Commands = []cli.Command{
} else {
cli.ShowCommandHelp(c, "active")
}
},
},
{
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) {
}
func cmdCreate(c *cli.Context) {
driver := c.String("driver")
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)
},
},
{
Name: "inspect",
Usage: "Inspect information about a machine",
Action: func(c *cli.Context) {
}
func cmdInspect(c *cli.Context) {
prettyJSON, err := json.MarshalIndent(getHost(c), "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(prettyJSON))
},
},
{
Name: "ip",
Usage: "Get the IP address of a machine",
Action: func(c *cli.Context) {
}
func cmdIp(c *cli.Context) {
ip, err := getHost(c).Driver.GetIP()
if err != nil {
log.Fatal(err)
}
fmt.Println(ip)
},
},
{
Name: "kill",
Usage: "Kill a machine",
Action: func(c *cli.Context) {
}
func cmdKill(c *cli.Context) {
if err := getHost(c).Driver.Kill(); err != nil {
log.Fatal(err)
}
},
},
{
Flags: []cli.Flag{
cli.BoolFlag{
Name: "quiet, q",
Usage: "Enable quiet mode",
},
},
Name: "ls",
Usage: "List machines",
Action: func(c *cli.Context) {
}
func cmdLs(c *cli.Context) {
quiet := c.Bool("quiet")
store := NewStore(c.GlobalString("storage-path"))
@ -207,27 +269,15 @@ var Commands = []cli.Command{
}
w.Flush()
},
},
{
Name: "restart",
Usage: "Restart a machine",
Action: func(c *cli.Context) {
}
func cmdRestart(c *cli.Context) {
if err := getHost(c).Driver.Restart(); err != nil {
log.Fatal(err)
}
},
},
{
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) {
}
func cmdRm(c *cli.Context) {
if len(c.Args()) == 0 {
cli.ShowCommandHelp(c, "rm")
log.Fatal("You must specify a machine name")
@ -247,19 +297,9 @@ var Commands = []cli.Command{
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.")
}
},
},
{
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) {
}
func cmdSsh(c *cli.Context) {
name := c.Args().First()
store := NewStore(c.GlobalString("storage-path"))
@ -298,47 +338,33 @@ var Commands = []cli.Command{
if err := sshCmd.Run(); err != nil {
log.Fatal(err)
}
},
},
{
Name: "start",
Usage: "Start a machine",
Action: func(c *cli.Context) {
}
func cmdStart(c *cli.Context) {
if err := getHost(c).Start(); err != nil {
log.Fatal(err)
}
},
},
{
Name: "stop",
Usage: "Stop a machine",
Action: func(c *cli.Context) {
}
func cmdStop(c *cli.Context) {
if err := getHost(c).Stop(); err != nil {
log.Fatal(err)
}
},
},
{
Name: "upgrade",
Usage: "Upgrade a machine to the latest version of Docker",
Action: func(c *cli.Context) {
}
func cmdUpgrade(c *cli.Context) {
if err := getHost(c).Upgrade(); err != nil {
log.Fatal(err)
}
},
},
{
Name: "url",
Usage: "Get the URL of a machine",
Action: func(c *cli.Context) {
}
func cmdUrl(c *cli.Context) {
url, err := getHost(c).GetURL()
if err != nil {
log.Fatal(err)
}
fmt.Println(url)
},
},
}
func getHost(c *cli.Context) *Host {