Skip to content

Commit da95ea3

Browse files
committed
Merge pull request docker-archive-public#103 from ggiamarchi/machine-name-optional
Make machine name optional in CLI
2 parents e401c44 + d730d39 commit da95ea3

File tree

1 file changed

+30
-163
lines changed

1 file changed

+30
-163
lines changed

commands.go

Lines changed: 30 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,7 @@ var Commands = []cli.Command{
129129
Name: "inspect",
130130
Usage: "Inspect information about a machine",
131131
Action: func(c *cli.Context) {
132-
name := c.Args().First()
133-
134-
if name == "" {
135-
cli.ShowCommandHelp(c, "inspect")
136-
os.Exit(1)
137-
}
138-
139-
store := NewStore()
140-
host, err := store.Load(name)
141-
if err != nil {
142-
log.Errorf("error loading data")
143-
os.Exit(1)
144-
}
145-
146-
prettyJson, err := json.MarshalIndent(host, "", " ")
132+
prettyJson, err := json.MarshalIndent(getHost(c), "", " ")
147133
if err != nil {
148134
log.Error("error with json")
149135
os.Exit(1)
@@ -156,37 +142,7 @@ var Commands = []cli.Command{
156142
Name: "ip",
157143
Usage: "Get the IP address of a machine",
158144
Action: func(c *cli.Context) {
159-
name := c.Args().First()
160-
161-
if name == "" {
162-
cli.ShowCommandHelp(c, "ip")
163-
os.Exit(1)
164-
}
165-
166-
var (
167-
err error
168-
host *Host
169-
store = NewStore()
170-
)
171-
172-
if name != "" {
173-
host, err = store.Load(name)
174-
if err != nil {
175-
log.Errorf("error unable to load data")
176-
os.Exit(1)
177-
}
178-
} else {
179-
host, err = store.GetActive()
180-
if err != nil {
181-
log.Errorf("error")
182-
os.Exit(1)
183-
}
184-
if host == nil {
185-
os.Exit(1)
186-
}
187-
}
188-
189-
ip, err := host.Driver.GetIP()
145+
ip, err := getHost(c).Driver.GetIP()
190146
if err != nil {
191147
log.Errorf("error unable to get IP")
192148
os.Exit(1)
@@ -199,22 +155,7 @@ var Commands = []cli.Command{
199155
Name: "kill",
200156
Usage: "Kill a machine",
201157
Action: func(c *cli.Context) {
202-
name := c.Args().First()
203-
204-
if name == "" {
205-
cli.ShowCommandHelp(c, "kill")
206-
os.Exit(1)
207-
}
208-
209-
store := NewStore()
210-
211-
host, err := store.Load(name)
212-
if err != nil {
213-
log.Errorf("error unable to load data")
214-
os.Exit(1)
215-
}
216-
217-
host.Driver.Kill()
158+
getHost(c).Driver.Kill()
218159
},
219160
},
220161
{
@@ -291,21 +232,7 @@ var Commands = []cli.Command{
291232
Name: "restart",
292233
Usage: "Restart a machine",
293234
Action: func(c *cli.Context) {
294-
name := c.Args().First()
295-
if name == "" {
296-
cli.ShowCommandHelp(c, "restart")
297-
os.Exit(1)
298-
}
299-
300-
store := NewStore()
301-
302-
host, err := store.Load(name)
303-
if err != nil {
304-
log.Errorf("error unable to load data")
305-
os.Exit(1)
306-
}
307-
308-
host.Driver.Restart()
235+
getHost(c).Driver.Restart()
309236
},
310237
},
311238
{
@@ -398,110 +325,28 @@ var Commands = []cli.Command{
398325
Name: "start",
399326
Usage: "Start a machine",
400327
Action: func(c *cli.Context) {
401-
name := c.Args().First()
402-
store := NewStore()
403-
404-
if name == "" {
405-
host, err := store.GetActive()
406-
if err != nil {
407-
log.Errorf("error unable to get active host")
408-
os.Exit(1)
409-
}
410-
411-
name = host.Name
412-
}
413-
414-
host, err := store.Load(name)
415-
if err != nil {
416-
log.Errorf("error unable to load data")
417-
os.Exit(1)
418-
}
419-
420-
host.Start()
328+
getHost(c).Start()
421329
},
422330
},
423331
{
424332
Name: "stop",
425333
Usage: "Stop a machine",
426334
Action: func(c *cli.Context) {
427-
name := c.Args().First()
428-
store := NewStore()
429-
430-
if name == "" {
431-
host, err := store.GetActive()
432-
if err != nil {
433-
log.Errorf("error unable to get active host")
434-
os.Exit(1)
435-
}
436-
437-
name = host.Name
438-
}
439-
440-
host, err := store.Load(name)
441-
if err != nil {
442-
log.Errorf("error unable to load data")
443-
os.Exit(1)
444-
}
445-
446-
host.Stop()
335+
getHost(c).Stop()
447336
},
448337
},
449338
{
450339
Name: "upgrade",
451340
Usage: "Upgrade a machine to the latest version of Docker",
452341
Action: func(c *cli.Context) {
453-
name := c.Args().First()
454-
store := NewStore()
455-
456-
if name == "" {
457-
host, err := store.GetActive()
458-
if err != nil {
459-
log.Errorf("error unable to get active host")
460-
os.Exit(1)
461-
}
462-
463-
name = host.Name
464-
}
465-
466-
host, err := store.Load(name)
467-
if err != nil {
468-
log.Errorf("error unable to load host")
469-
os.Exit(1)
470-
}
471-
472-
host.Driver.Upgrade()
342+
getHost(c).Driver.Upgrade()
473343
},
474344
},
475345
{
476346
Name: "url",
477347
Usage: "Get the URL of a machine",
478348
Action: func(c *cli.Context) {
479-
name := c.Args().First()
480-
481-
var (
482-
err error
483-
host *Host
484-
store = NewStore()
485-
)
486-
487-
if name != "" {
488-
host, err = store.Load(name)
489-
if err != nil {
490-
log.Errorf("error unable to load data")
491-
os.Exit(1)
492-
}
493-
} else {
494-
host, err = store.GetActive()
495-
if err != nil {
496-
log.Errorf("error unable to get active host")
497-
os.Exit(1)
498-
}
499-
if host == nil {
500-
os.Exit(1)
501-
}
502-
}
503-
504-
url, err := host.GetURL()
349+
url, err := getHost(c).GetURL()
505350
if err != nil {
506351
log.Errorf("error unable to get url for host")
507352
os.Exit(1)
@@ -511,3 +356,25 @@ var Commands = []cli.Command{
511356
},
512357
},
513358
}
359+
360+
func getHost(c *cli.Context) *Host {
361+
name := c.Args().First()
362+
store := NewStore()
363+
364+
if name == "" {
365+
host, err := store.GetActive()
366+
if err != nil {
367+
log.Errorf("error unable to get active host")
368+
os.Exit(1)
369+
}
370+
name = host.Name
371+
}
372+
373+
host, err := store.Load(name)
374+
if err != nil {
375+
log.Errorf("error unable to load host")
376+
os.Exit(1)
377+
}
378+
379+
return host
380+
}

0 commit comments

Comments
 (0)