From 93e292438ceec18a8aba46bcd4cfa09b7cb000c2 Mon Sep 17 00:00:00 2001 From: Aaron Lehmann Date: Thu, 1 Dec 2016 14:08:06 -0800 Subject: [PATCH] api: Hide UpdateStatus when it is not present When UpdateStatus was not present, the empty values of the timestamps would be present: "UpdateStatus": { "StartedAt": "0001-01-01T00:00:00Z", "CompletedAt": "0001-01-01T00:00:00Z" } To fix this, make the timestamps pointers, so they can be set to nil when they should not be shown. Also make UpdateStatus itself a pointer, so an empty object does not show up when there is no UpdateStatus. Signed-off-by: Aaron Lehmann Upstream-commit: 8a379d7bcebea5415e462f85ee2c1c0bf704178a Component: cli --- components/cli/command/formatter/service.go | 14 ++++++++++---- components/cli/command/service/inspect_test.go | 6 +++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/components/cli/command/formatter/service.go b/components/cli/command/formatter/service.go index aaa78386cb..2690029ce4 100644 --- a/components/cli/command/formatter/service.go +++ b/components/cli/command/formatter/service.go @@ -28,7 +28,9 @@ Service Mode: {{- if .HasUpdateStatus }} UpdateStatus: State: {{ .UpdateStatusState }} +{{- if .HasUpdateStatusStarted }} Started: {{ .UpdateStatusStarted }} +{{- end }} {{- if .UpdateIsCompleted }} Completed: {{ .UpdateStatusCompleted }} {{- end }} @@ -172,23 +174,27 @@ func (ctx *serviceInspectContext) ModeReplicatedReplicas() *uint64 { } func (ctx *serviceInspectContext) HasUpdateStatus() bool { - return ctx.Service.UpdateStatus.State != "" + return ctx.Service.UpdateStatus != nil && ctx.Service.UpdateStatus.State != "" } func (ctx *serviceInspectContext) UpdateStatusState() swarm.UpdateState { return ctx.Service.UpdateStatus.State } +func (ctx *serviceInspectContext) HasUpdateStatusStarted() bool { + return ctx.Service.UpdateStatus.StartedAt != nil +} + func (ctx *serviceInspectContext) UpdateStatusStarted() string { - return units.HumanDuration(time.Since(ctx.Service.UpdateStatus.StartedAt)) + return units.HumanDuration(time.Since(*ctx.Service.UpdateStatus.StartedAt)) } func (ctx *serviceInspectContext) UpdateIsCompleted() bool { - return ctx.Service.UpdateStatus.State == swarm.UpdateStateCompleted + return ctx.Service.UpdateStatus.State == swarm.UpdateStateCompleted && ctx.Service.UpdateStatus.CompletedAt != nil } func (ctx *serviceInspectContext) UpdateStatusCompleted() string { - return units.HumanDuration(time.Since(ctx.Service.UpdateStatus.CompletedAt)) + return units.HumanDuration(time.Since(*ctx.Service.UpdateStatus.CompletedAt)) } func (ctx *serviceInspectContext) UpdateStatusMessage() string { diff --git a/components/cli/command/service/inspect_test.go b/components/cli/command/service/inspect_test.go index 04a65080c7..34c41ee78a 100644 --- a/components/cli/command/service/inspect_test.go +++ b/components/cli/command/service/inspect_test.go @@ -74,9 +74,9 @@ func formatServiceInspect(t *testing.T, format formatter.Format, now time.Time) }, }, }, - UpdateStatus: swarm.UpdateStatus{ - StartedAt: now, - CompletedAt: now, + UpdateStatus: &swarm.UpdateStatus{ + StartedAt: &now, + CompletedAt: &now, }, }