Skip to content

Commit

Permalink
cli: Add more nil checking to service pretty-printer
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
aaronlehmann committed Sep 26, 2016
1 parent e58c0c6 commit 56d92bf
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions command/formatter/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Labels:
{{- end }}{{ end }}
Mode:
{{- if .IsModeGlobal }} Global
{{- else }} Replicated
{{- else if .IsModeReplicated }} Replicated
{{- if .ModeReplicatedReplicas }}
Replicas: {{ .ModeReplicatedReplicas }}
{{- end }}{{ end }}
Expand Down Expand Up @@ -73,22 +73,20 @@ Mounts:
Resources:
{{- if .HasResourceReservations }}
Reservations:
{{- end }}
{{- if gt .ResourceReservationNanoCPUs 0.0 }}
CPU: {{ .ResourceReservationNanoCPUs }}
{{- end }}
{{- if .ResourceReservationMemory }}
Memory: {{ .ResourceReservationMemory }}
{{- end }}
{{- end }}{{ end }}
{{- if .HasResourceLimits }}
Limits:
{{- end }}
{{- if gt .ResourceLimitsNanoCPUs 0.0 }}
CPU: {{ .ResourceLimitsNanoCPUs }}
{{- end }}
{{- if .ResourceLimitMemory }}
Memory: {{ .ResourceLimitMemory }}
{{- end }}{{ end }}
{{- end }}{{ end }}{{ end }}
{{- if .Networks }}
Networks:
{{- range $network := .Networks }} {{ $network }}{{ end }} {{ end }}
Expand Down Expand Up @@ -156,6 +154,10 @@ func (ctx *serviceInspectContext) IsModeGlobal() bool {
return ctx.Service.Spec.Mode.Global != nil
}

func (ctx *serviceInspectContext) IsModeReplicated() bool {
return ctx.Service.Spec.Mode.Replicated != nil
}

func (ctx *serviceInspectContext) ModeReplicatedReplicas() *uint64 {
return ctx.Service.Spec.Mode.Replicated.Replicas
}
Expand Down

0 comments on commit 56d92bf

Please sign in to comment.