Avoid code duplication

Signed-off-by: Guillaume Giamarchi <guillaume.giamarchi@gmail.com>
This commit is contained in:
Guillaume Giamarchi 2014-12-14 02:04:48 +01:00
parent 8613b941e8
commit d730d39c48
1 changed files with 30 additions and 172 deletions

View File

@ -128,26 +128,7 @@ var Commands = []cli.Command{
Name: "inspect", Name: "inspect",
Usage: "Inspect information about a machine", Usage: "Inspect information about a machine",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
name := c.Args().First() prettyJson, err := json.MarshalIndent(getHost(c), "", " ")
store := NewStore()
if name == "" {
host, err := store.GetActive()
if err != nil {
log.Errorf("error unable to get active host")
os.Exit(1)
}
name = host.Name
}
host, err := store.Load(name)
if err != nil {
log.Errorf("error loading data")
os.Exit(1)
}
prettyJson, err := json.MarshalIndent(host, "", " ")
if err != nil { if err != nil {
log.Error("error with json") log.Error("error with json")
os.Exit(1) os.Exit(1)
@ -160,32 +141,7 @@ var Commands = []cli.Command{
Name: "ip", Name: "ip",
Usage: "Get the IP address of a machine", Usage: "Get the IP address of a machine",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
name := c.Args().First() ip, err := getHost(c).Driver.GetIP()
var (
err error
host *Host
store = NewStore()
)
if name != "" {
host, err = store.Load(name)
if err != nil {
log.Errorf("error unable to load data")
os.Exit(1)
}
} else {
host, err = store.GetActive()
if err != nil {
log.Errorf("error")
os.Exit(1)
}
if host == nil {
os.Exit(1)
}
}
ip, err := host.Driver.GetIP()
if err != nil { if err != nil {
log.Errorf("error unable to get IP") log.Errorf("error unable to get IP")
os.Exit(1) os.Exit(1)
@ -198,26 +154,7 @@ var Commands = []cli.Command{
Name: "kill", Name: "kill",
Usage: "Kill a machine", Usage: "Kill a machine",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
name := c.Args().First() getHost(c).Driver.Kill()
store := NewStore()
if name == "" {
host, err := store.GetActive()
if err != nil {
log.Errorf("error unable to get active host")
os.Exit(1)
}
name = host.Name
}
host, err := store.Load(name)
if err != nil {
log.Errorf("error unable to load data")
os.Exit(1)
}
host.Driver.Kill()
}, },
}, },
{ {
@ -294,26 +231,7 @@ var Commands = []cli.Command{
Name: "restart", Name: "restart",
Usage: "Restart a machine", Usage: "Restart a machine",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
name := c.Args().First() getHost(c).Driver.Restart()
store := NewStore()
if name == "" {
host, err := store.GetActive()
if err != nil {
log.Errorf("error unable to get active host")
os.Exit(1)
}
name = host.Name
}
host, err := store.Load(name)
if err != nil {
log.Errorf("error unable to load data")
os.Exit(1)
}
host.Driver.Restart()
}, },
}, },
{ {
@ -401,110 +319,28 @@ var Commands = []cli.Command{
Name: "start", Name: "start",
Usage: "Start a machine", Usage: "Start a machine",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
name := c.Args().First() getHost(c).Start()
store := NewStore()
if name == "" {
host, err := store.GetActive()
if err != nil {
log.Errorf("error unable to get active host")
os.Exit(1)
}
name = host.Name
}
host, err := store.Load(name)
if err != nil {
log.Errorf("error unable to load data")
os.Exit(1)
}
host.Start()
}, },
}, },
{ {
Name: "stop", Name: "stop",
Usage: "Stop a machine", Usage: "Stop a machine",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
name := c.Args().First() getHost(c).Stop()
store := NewStore()
if name == "" {
host, err := store.GetActive()
if err != nil {
log.Errorf("error unable to get active host")
os.Exit(1)
}
name = host.Name
}
host, err := store.Load(name)
if err != nil {
log.Errorf("error unable to load data")
os.Exit(1)
}
host.Stop()
}, },
}, },
{ {
Name: "upgrade", Name: "upgrade",
Usage: "Upgrade a machine to the latest version of Docker", Usage: "Upgrade a machine to the latest version of Docker",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
name := c.Args().First() getHost(c).Driver.Upgrade()
store := NewStore()
if name == "" {
host, err := store.GetActive()
if err != nil {
log.Errorf("error unable to get active host")
os.Exit(1)
}
name = host.Name
}
host, err := store.Load(name)
if err != nil {
log.Errorf("error unable to load host")
os.Exit(1)
}
host.Driver.Upgrade()
}, },
}, },
{ {
Name: "url", Name: "url",
Usage: "Get the URL of a machine", Usage: "Get the URL of a machine",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
name := c.Args().First() url, err := getHost(c).GetURL()
var (
err error
host *Host
store = NewStore()
)
if name != "" {
host, err = store.Load(name)
if err != nil {
log.Errorf("error unable to load data")
os.Exit(1)
}
} else {
host, err = store.GetActive()
if err != nil {
log.Errorf("error unable to get active host")
os.Exit(1)
}
if host == nil {
os.Exit(1)
}
}
url, err := host.GetURL()
if err != nil { if err != nil {
log.Errorf("error unable to get url for host") log.Errorf("error unable to get url for host")
os.Exit(1) os.Exit(1)
@ -514,3 +350,25 @@ var Commands = []cli.Command{
}, },
}, },
} }
func getHost(c *cli.Context) *Host {
name := c.Args().First()
store := NewStore()
if name == "" {
host, err := store.GetActive()
if err != nil {
log.Errorf("error unable to get active host")
os.Exit(1)
}
name = host.Name
}
host, err := store.Load(name)
if err != nil {
log.Errorf("error unable to load host")
os.Exit(1)
}
return host
}