55 "fmt"
66 "os"
77 "os/exec"
8+ "sort"
89 "strings"
910 "sync"
1011 "text/tabwriter"
@@ -21,25 +22,25 @@ import (
2122 "github.com/docker/machine/state"
2223)
2324
24- type HostListItem struct {
25+ type hostListItem struct {
2526 Name string
2627 Active bool
2728 DriverName string
2829 State state.State
2930 URL string
3031}
3132
32- type HostListItemByName []HostListItem
33+ type hostListItemByName []hostListItem
3334
34- func (h HostListItemByName ) Len () int {
35+ func (h hostListItemByName ) Len () int {
3536 return len (h )
3637}
3738
38- func (h HostListItemByName ) Swap (i , j int ) {
39+ func (h hostListItemByName ) Swap (i , j int ) {
3940 h [i ], h [j ] = h [j ], h [i ]
4041}
4142
42- func (h HostListItemByName ) Less (i , j int ) bool {
43+ func (h hostListItemByName ) Less (i , j int ) bool {
4344 return strings .ToLower (h [i ].Name ) < strings .ToLower (h [j ].Name )
4445}
4546
@@ -54,22 +55,19 @@ var Commands = []cli.Command{
5455 if name == "" {
5556 host , err := store .GetActive ()
5657 if err != nil {
57- log .Errorf ("error finding active host" )
58+ log .Fatalf ("error getting active host: %v" , err )
5859 }
5960 if host != nil {
6061 fmt .Println (host .Name )
6162 }
6263 } else if name != "" {
6364 host , err := store .Load (name )
6465 if err != nil {
65- log .Errorln (err )
66- log .Errorf ("error loading new active host" )
67- os .Exit (1 )
66+ log .Fatalf ("error loading host: %v" , err )
6867 }
6968
7069 if err := store .SetActive (host ); err != nil {
71- log .Errorf ("error setting new active host" )
72- os .Exit (1 )
70+ log .Fatalf ("error setting active host: %v" , err )
7371 }
7472 } else {
7573 cli .ShowCommandHelp (c , "active" )
@@ -101,25 +99,21 @@ var Commands = []cli.Command{
10199
102100 keyExists , err := drivers .PublicKeyExists ()
103101 if err != nil {
104- log .Errorf ("error" )
105- os .Exit (1 )
102+ log .Fatal (err )
106103 }
107104
108105 if ! keyExists {
109- log .Errorf ("error key doesn't exist" )
110- os .Exit (1 )
106+ log .Fatalf ("Identity authentication public key doesn't exist at %q. Create your public key by running the \" docker\" command." , drivers .PublicKeyPath ())
111107 }
112108
113109 store := NewStore ()
114110
115111 host , err := store .Create (name , driver , c )
116112 if err != nil {
117- log .Errorf ("%s" , err )
118- os .Exit (1 )
113+ log .Fatal (err )
119114 }
120115 if err := store .SetActive (host ); err != nil {
121- log .Errorf ("%s" , err )
122- os .Exit (1 )
116+ log .Fatalf ("error setting active host: %v" , err )
123117 }
124118
125119 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 )
@@ -129,13 +123,12 @@ var Commands = []cli.Command{
129123 Name : "inspect" ,
130124 Usage : "Inspect information about a machine" ,
131125 Action : func (c * cli.Context ) {
132- prettyJson , err := json .MarshalIndent (getHost (c ), "" , " " )
126+ prettyJSON , err := json .MarshalIndent (getHost (c ), "" , " " )
133127 if err != nil {
134- log .Error ("error with json" )
135- os .Exit (1 )
128+ log .Fatal (err )
136129 }
137130
138- fmt .Println (string (prettyJson ))
131+ fmt .Println (string (prettyJSON ))
139132 },
140133 },
141134 {
@@ -144,8 +137,7 @@ var Commands = []cli.Command{
144137 Action : func (c * cli.Context ) {
145138 ip , err := getHost (c ).Driver .GetIP ()
146139 if err != nil {
147- log .Errorf ("error unable to get IP" )
148- os .Exit (1 )
140+ log .Fatal (err )
149141 }
150142
151143 fmt .Println (ip )
@@ -155,7 +147,9 @@ var Commands = []cli.Command{
155147 Name : "kill" ,
156148 Usage : "Kill a machine" ,
157149 Action : func (c * cli.Context ) {
158- getHost (c ).Driver .Kill ()
150+ if err := getHost (c ).Driver .Kill (); err != nil {
151+ log .Fatal (err )
152+ }
159153 },
160154 },
161155 {
@@ -173,8 +167,7 @@ var Commands = []cli.Command{
173167
174168 hostList , err := store .List ()
175169 if err != nil {
176- log .Errorf ("error unable to list hosts" )
177- os .Exit (1 )
170+ log .Fatal (err )
178171 }
179172
180173 w := tabwriter .NewWriter (os .Stdout , 5 , 1 , 3 , ' ' , 0 )
@@ -184,6 +177,7 @@ var Commands = []cli.Command{
184177 }
185178
186179 wg := sync.WaitGroup {}
180+ items := []hostListItem {}
187181
188182 for _ , host := range hostList {
189183 host := host
@@ -212,27 +206,42 @@ var Commands = []cli.Command{
212206 host .Name , err )
213207 }
214208
215- activeString := ""
216- if isActive {
217- activeString = "*"
218- }
209+ items = append (items , hostListItem {
210+ Name : host .Name ,
211+ Active : isActive ,
212+ DriverName : host .Driver .DriverName (),
213+ State : currentState ,
214+ URL : url ,
215+ })
219216
220- fmt .Fprintf (w , "%s\t %s\t %s\t %s\t %s\n " ,
221- host .Name , activeString , host .Driver .DriverName (), currentState , url )
222217 wg .Done ()
223218 }()
224219 }
225220 }
226221
227222 wg .Wait ()
223+
224+ sort .Sort (hostListItemByName (items ))
225+
226+ for _ , item := range items {
227+ activeString := ""
228+ if item .Active {
229+ activeString = "*"
230+ }
231+ fmt .Fprintf (w , "%s\t %s\t %s\t %s\t %s\n " ,
232+ item .Name , activeString , item .DriverName , item .State , item .URL )
233+ }
234+
228235 w .Flush ()
229236 },
230237 },
231238 {
232239 Name : "restart" ,
233240 Usage : "Restart a machine" ,
234241 Action : func (c * cli.Context ) {
235- getHost (c ).Driver .Restart ()
242+ if err := getHost (c ).Driver .Restart (); err != nil {
243+ log .Fatal (err )
244+ }
236245 },
237246 },
238247 {
@@ -262,7 +271,7 @@ var Commands = []cli.Command{
262271 }
263272 }
264273 if isError {
265- log .Errorf ("There was an error removing a machine. To force remove it, pass the -f option. Warning: this might leave it running on the provider." )
274+ 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." )
266275 }
267276 },
268277 },
@@ -283,8 +292,7 @@ var Commands = []cli.Command{
283292 if name == "" {
284293 host , err := store .GetActive ()
285294 if err != nil {
286- log .Errorf ("error unable to get active host" )
287- os .Exit (1 )
295+ log .Fatalf ("unable to get active host: %v" , err )
288296 }
289297
290298 name = host .Name
@@ -297,8 +305,7 @@ var Commands = []cli.Command{
297305
298306 host , err := store .Load (name )
299307 if err != nil {
300- log .Errorf ("%s" , err )
301- os .Exit (1 )
308+ log .Fatal (err )
302309 }
303310
304311 var sshCmd * exec.Cmd
@@ -308,38 +315,42 @@ var Commands = []cli.Command{
308315 sshCmd , err = host .Driver .GetSSHCommand (c .String ("command" ))
309316 }
310317 if err != nil {
311- log .Errorf ("%s" , err )
312- os .Exit (1 )
318+ log .Fatal (err )
313319 }
314320
315321 sshCmd .Stdin = os .Stdin
316322 sshCmd .Stdout = os .Stdout
317323 sshCmd .Stderr = os .Stderr
318324 if err := sshCmd .Run (); err != nil {
319- log .Errorf ("%s" , err )
320- os .Exit (1 )
325+ log .Fatal (err )
321326 }
322327 },
323328 },
324329 {
325330 Name : "start" ,
326331 Usage : "Start a machine" ,
327332 Action : func (c * cli.Context ) {
328- getHost (c ).Start ()
333+ if err := getHost (c ).Start (); err != nil {
334+ log .Fatal (err )
335+ }
329336 },
330337 },
331338 {
332339 Name : "stop" ,
333340 Usage : "Stop a machine" ,
334341 Action : func (c * cli.Context ) {
335- getHost (c ).Stop ()
342+ if err := getHost (c ).Stop (); err != nil {
343+ log .Fatal (err )
344+ }
336345 },
337346 },
338347 {
339348 Name : "upgrade" ,
340349 Usage : "Upgrade a machine to the latest version of Docker" ,
341350 Action : func (c * cli.Context ) {
342- getHost (c ).Driver .Upgrade ()
351+ if err := getHost (c ).Upgrade (); err != nil {
352+ log .Fatal (err )
353+ }
343354 },
344355 },
345356 {
@@ -348,8 +359,7 @@ var Commands = []cli.Command{
348359 Action : func (c * cli.Context ) {
349360 url , err := getHost (c ).GetURL ()
350361 if err != nil {
351- log .Errorf ("error unable to get url for host" )
352- os .Exit (1 )
362+ log .Fatal (err )
353363 }
354364
355365 fmt .Println (url )
@@ -364,17 +374,14 @@ func getHost(c *cli.Context) *Host {
364374 if name == "" {
365375 host , err := store .GetActive ()
366376 if err != nil {
367- log .Errorf ("error unable to get active host" )
368- os .Exit (1 )
377+ log .Fatalf ("unable to get active host: %v" , err )
369378 }
370- name = host . Name
379+ return host
371380 }
372381
373382 host , err := store .Load (name )
374383 if err != nil {
375- log .Errorf ("error unable to load host" )
376- os .Exit (1 )
384+ log .Fatalf ("unable to load host: %v" , err )
377385 }
378-
379386 return host
380387}
0 commit comments