diff --git a/commands/ls.go b/commands/ls.go index 6c84893d42..f548f17916 100644 --- a/commands/ls.go +++ b/commands/ls.go @@ -33,16 +33,23 @@ const ( var ( stateTimeoutDuration = lsDefaultTimeout * time.Second -) -// FilterOptions - -type FilterOptions struct { - SwarmName []string - DriverName []string - State []string - Name []string - Labels []string -} + headers = map[string]string{ + "Name": "NAME", + "Active": "ACTIVE", + "ActiveHost": "DRIVER", + "ActiveSwarm": "STATE", + "DriverName": "URL", + "State": "STATE", + "URL": "URL", + "SwarmOptions": "SWARM_OPTIONS", + "Swarm": "SWARM", + "EngineOptions": "ENGINE_OPTIONS", + "Error": "ERRORS", + "DockerVersion": "DOCKER", + "ResponseTime": "RESPONSE", + } +) type HostListItem struct { Name string @@ -57,21 +64,16 @@ type HostListItem struct { EngineOptions *engine.Options Error string DockerVersion string + ResponseTime time.Duration } -type Headers struct { - Name string - Active string - ActiveHost string - ActiveSwarm string - DriverName string - State string - URL string - SwarmOptions string - Swarm string - EngineOptions string - Error string - DockerVersion string +// FilterOptions - +type FilterOptions struct { + SwarmName []string + DriverName []string + State []string + Name []string + Labels []string } func cmdLs(c CommandLine, api libmachine.API) error { @@ -110,21 +112,6 @@ func cmdLs(c CommandLine, api libmachine.API) error { w = tabWriter - headers := &Headers{ - Name: "NAME", - Active: "ACTIVE", - ActiveHost: "DRIVER", - ActiveSwarm: "STATE", - DriverName: "URL", - State: "STATE", - URL: "URL", - SwarmOptions: "SWARM_OPTIONS", - Swarm: "SWARM", - EngineOptions: "ENGINE_OPTIONS", - Error: "ERRORS", - DockerVersion: "DOCKER", - } - if err := template.Execute(w, headers); err != nil { return err } @@ -349,6 +336,7 @@ func matchesLabel(host *host.Host, labels []string) bool { // to call the underlying drivers as less as possible to get the information // we need. func attemptGetHostState(h *host.Host, stateQueryChan chan<- HostListItem) { + requestBeginning := time.Now() url := "" currentState := state.None dockerVersion := "Unknown" @@ -427,6 +415,7 @@ func attemptGetHostState(h *host.Host, stateQueryChan chan<- HostListItem) { EngineOptions: engineOptions, DockerVersion: dockerVersion, Error: hostError, + ResponseTime: time.Now().Round(time.Millisecond).Sub(requestBeginning.Round(time.Millisecond)), } } @@ -446,9 +435,10 @@ func getHostState(h *host.Host, hostListItemsChan chan<- HostListItem) { // Otherwise, give up after a predetermined duration. case <-time.After(stateTimeoutDuration): hostListItemsChan <- HostListItem{ - Name: h.Name, - DriverName: h.Driver.DriverName(), - State: state.Timeout, + Name: h.Name, + DriverName: h.Driver.DriverName(), + State: state.Timeout, + ResponseTime: stateTimeoutDuration, } } } diff --git a/commands/ls_test.go b/commands/ls_test.go index 437b30fd9a..514dd00e5d 100644 --- a/commands/ls_test.go +++ b/commands/ls_test.go @@ -475,6 +475,7 @@ func TestGetHostStateTimeout(t *testing.T) { assert.Equal(t, "foo", hostItem.Name) assert.Equal(t, state.Timeout, hostItem.State) assert.Equal(t, "Driver", hostItem.DriverName) + assert.Equal(t, stateTimeoutDuration, hostItem.ResponseTime) } func TestGetHostStateError(t *testing.T) { diff --git a/docs/reference/ls.md b/docs/reference/ls.md index 3020a54f1e..6de1d6c8ff 100644 --- a/docs/reference/ls.md +++ b/docs/reference/ls.md @@ -88,6 +88,7 @@ Valid placeholders for the Go template are listed below: | .Swarm | Machine swarm name | | .Error | Machine errors | | .DockerVersion | Docker Daemon version | +| .ResponseTime | Time taken by the host to respond | When using the `--format` option, the `ls` command will either output the data exactly as the template declares or, when using the table directive, will include column headers as well.