Browse code

cli: Add more nil checking to service pretty-printer

Currently, if the service mode is not "global", this code assumes that
Replicated is non-nil. This assumption may not be true in the future.
Instead of making the assumption, explicitly check that Replicated is
non-nil before using it.

Similarly, for limits and reservations, enclose methods that read from
Limits and Reservations within checks that those fields are non-nil.

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>

Aaron Lehmann authored on 2016/09/26 18:12:24
Showing 1 changed files
... ...
@@ -21,7 +21,7 @@ Labels:
21 21
 {{- end }}{{ end }}
22 22
 Mode:
23 23
 {{- if .IsModeGlobal }}		Global
24
-{{- else }}		Replicated
24
+{{- else if .IsModeReplicated }}		Replicated
25 25
 {{- if .ModeReplicatedReplicas }}
26 26
  Replicas:	{{ .ModeReplicatedReplicas }}
27 27
 {{- end }}{{ end }}
... ...
@@ -73,22 +73,20 @@ Mounts:
73 73
 Resources:
74 74
 {{- if .HasResourceReservations }}
75 75
  Reservations:
76
-{{- end }}
77 76
 {{- if gt .ResourceReservationNanoCPUs 0.0 }}
78 77
   CPU:		{{ .ResourceReservationNanoCPUs }}
79 78
 {{- end }}
80 79
 {{- if .ResourceReservationMemory }}
81 80
   Memory:	{{ .ResourceReservationMemory }}
82
-{{- end }}
81
+{{- end }}{{ end }}
83 82
 {{- if .HasResourceLimits }}
84 83
  Limits:
85
-{{- end }}
86 84
 {{- if gt .ResourceLimitsNanoCPUs 0.0 }}
87 85
   CPU:		{{ .ResourceLimitsNanoCPUs }}
88 86
 {{- end }}
89 87
 {{- if .ResourceLimitMemory }}
90 88
   Memory:	{{ .ResourceLimitMemory }}
91
-{{- end }}{{ end }}
89
+{{- end }}{{ end }}{{ end }}
92 90
 {{- if .Networks }}
93 91
 Networks:
94 92
 {{- range $network := .Networks }} {{ $network }}{{ end }} {{ end }}
... ...
@@ -156,6 +154,10 @@ func (ctx *serviceInspectContext) IsModeGlobal() bool {
156 156
 	return ctx.Service.Spec.Mode.Global != nil
157 157
 }
158 158
 
159
+func (ctx *serviceInspectContext) IsModeReplicated() bool {
160
+	return ctx.Service.Spec.Mode.Replicated != nil
161
+}
162
+
159 163
 func (ctx *serviceInspectContext) ModeReplicatedReplicas() *uint64 {
160 164
 	return ctx.Service.Spec.Mode.Replicated.Replicas
161 165
 }