Skip to content

Commit 78d65b4

Browse files
committed
Fix nil pointer error in swarmctl print task
Fix nil pointer error when task.Spec.GetContainer() is nil. Signed-off-by: Xinfeng Liu <[email protected]>
1 parent 5a55460 commit 78d65b4

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

cmd/swarmctl/task/inspect.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,32 @@ func printTaskSummary(task *api.Task, res *common.Resolver) {
6464

6565
fmt.Fprintln(w, "Spec\t")
6666
ctr := task.Spec.GetContainer()
67-
common.FprintfIfNotEmpty(w, " Image\t: %s\n", ctr.Image)
68-
common.FprintfIfNotEmpty(w, " Command\t: %q\n", strings.Join(ctr.Command, " "))
69-
common.FprintfIfNotEmpty(w, " Args\t: [%s]\n", strings.Join(ctr.Args, ", "))
70-
common.FprintfIfNotEmpty(w, " Env\t: [%s]\n", strings.Join(ctr.Env, ", "))
71-
if len(ctr.Secrets) > 0 {
72-
fmt.Fprintln(w, " Secrets:")
73-
for _, sr := range ctr.Secrets {
74-
var targetName, mode string
75-
if sr.GetFile() != nil {
76-
targetName = sr.GetFile().Name
77-
mode = "FILE"
67+
if ctr != nil {
68+
common.FprintfIfNotEmpty(w, " Image\t: %s\n", ctr.Image)
69+
common.FprintfIfNotEmpty(w, " Command\t: %q\n", strings.Join(ctr.Command, " "))
70+
common.FprintfIfNotEmpty(w, " Args\t: [%s]\n", strings.Join(ctr.Args, ", "))
71+
common.FprintfIfNotEmpty(w, " Env\t: [%s]\n", strings.Join(ctr.Env, ", "))
72+
if len(ctr.Secrets) > 0 {
73+
fmt.Fprintln(w, " Secrets:")
74+
for _, sr := range ctr.Secrets {
75+
var targetName, mode string
76+
if sr.GetFile() != nil {
77+
targetName = sr.GetFile().Name
78+
mode = "FILE"
79+
}
80+
fmt.Fprintf(w, " [%s] %s:%s\n", mode, sr.SecretName, targetName)
7881
}
79-
fmt.Fprintf(w, " [%s] %s:%s\n", mode, sr.SecretName, targetName)
8082
}
81-
}
82-
if len(ctr.Configs) > 0 {
83-
fmt.Fprintln(w, " Configs:")
84-
for _, cr := range ctr.Configs {
85-
var targetName, mode string
86-
if cr.GetFile() != nil {
87-
targetName = cr.GetFile().Name
88-
mode = "FILE"
83+
if len(ctr.Configs) > 0 {
84+
fmt.Fprintln(w, " Configs:")
85+
for _, cr := range ctr.Configs {
86+
var targetName, mode string
87+
if cr.GetFile() != nil {
88+
targetName = cr.GetFile().Name
89+
mode = "FILE"
90+
}
91+
fmt.Fprintf(w, " [%s] %s:%s\n", mode, cr.ConfigName, targetName)
8992
}
90-
fmt.Fprintf(w, " [%s] %s:%s\n", mode, cr.ConfigName, targetName)
9193
}
9294
}
9395
}

cmd/swarmctl/task/print.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,15 @@ func Print(tasks []*api.Task, all bool, res *common.Resolver) {
4949
continue
5050
}
5151
c := t.Spec.GetContainer()
52+
var image string
53+
if c != nil {
54+
image = c.Image
55+
}
5256
fmt.Fprintf(w, "%s\t%s\t%d\t%s\t%s\t%s %s\t%s\n",
5357
t.ID,
5458
t.ServiceAnnotations.Name,
5559
t.Slot,
56-
c.Image,
60+
image,
5761
t.DesiredState.String(),
5862
t.Status.State.String(),
5963
common.TimestampAgo(t.Status.Timestamp),

0 commit comments

Comments
 (0)