Skip to content

Commit

Permalink
Fix nil pointer error in swarmctl print task
Browse files Browse the repository at this point in the history
Fix nil pointer error when task.Spec.GetContainer() is nil.

Signed-off-by: Xinfeng Liu <XinfengLiu@icloud.com>
  • Loading branch information
xinfengliu committed Oct 11, 2023
1 parent 5a55460 commit 78d65b4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
44 changes: 23 additions & 21 deletions cmd/swarmctl/task/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,32 @@ func printTaskSummary(task *api.Task, res *common.Resolver) {

fmt.Fprintln(w, "Spec\t")
ctr := task.Spec.GetContainer()
common.FprintfIfNotEmpty(w, " Image\t: %s\n", ctr.Image)
common.FprintfIfNotEmpty(w, " Command\t: %q\n", strings.Join(ctr.Command, " "))
common.FprintfIfNotEmpty(w, " Args\t: [%s]\n", strings.Join(ctr.Args, ", "))
common.FprintfIfNotEmpty(w, " Env\t: [%s]\n", strings.Join(ctr.Env, ", "))
if len(ctr.Secrets) > 0 {
fmt.Fprintln(w, " Secrets:")
for _, sr := range ctr.Secrets {
var targetName, mode string
if sr.GetFile() != nil {
targetName = sr.GetFile().Name
mode = "FILE"
if ctr != nil {
common.FprintfIfNotEmpty(w, " Image\t: %s\n", ctr.Image)
common.FprintfIfNotEmpty(w, " Command\t: %q\n", strings.Join(ctr.Command, " "))
common.FprintfIfNotEmpty(w, " Args\t: [%s]\n", strings.Join(ctr.Args, ", "))
common.FprintfIfNotEmpty(w, " Env\t: [%s]\n", strings.Join(ctr.Env, ", "))
if len(ctr.Secrets) > 0 {
fmt.Fprintln(w, " Secrets:")
for _, sr := range ctr.Secrets {
var targetName, mode string
if sr.GetFile() != nil {
targetName = sr.GetFile().Name
mode = "FILE"
}
fmt.Fprintf(w, " [%s] %s:%s\n", mode, sr.SecretName, targetName)
}
fmt.Fprintf(w, " [%s] %s:%s\n", mode, sr.SecretName, targetName)
}
}
if len(ctr.Configs) > 0 {
fmt.Fprintln(w, " Configs:")
for _, cr := range ctr.Configs {
var targetName, mode string
if cr.GetFile() != nil {
targetName = cr.GetFile().Name
mode = "FILE"
if len(ctr.Configs) > 0 {
fmt.Fprintln(w, " Configs:")
for _, cr := range ctr.Configs {
var targetName, mode string
if cr.GetFile() != nil {
targetName = cr.GetFile().Name
mode = "FILE"
}
fmt.Fprintf(w, " [%s] %s:%s\n", mode, cr.ConfigName, targetName)
}
fmt.Fprintf(w, " [%s] %s:%s\n", mode, cr.ConfigName, targetName)
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/swarmctl/task/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ func Print(tasks []*api.Task, all bool, res *common.Resolver) {
continue
}
c := t.Spec.GetContainer()
var image string
if c != nil {
image = c.Image
}
fmt.Fprintf(w, "%s\t%s\t%d\t%s\t%s\t%s %s\t%s\n",
t.ID,
t.ServiceAnnotations.Name,
t.Slot,
c.Image,
image,
t.DesiredState.String(),
t.Status.State.String(),
common.TimestampAgo(t.Status.Timestamp),
Expand Down

0 comments on commit 78d65b4

Please sign in to comment.