Skip to content

Commit 23ce8e5

Browse files
committed
Always expand inst.Message field in store.Inspect
There is no reason to let the user deal with template expansion when we can do this automatically after loading. This is especially useful in `limactl list` output. Signed-off-by: Jan Dubois <[email protected]>
1 parent de546da commit 23ce8e5

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

pkg/store/instance.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strconv"
1111
"strings"
1212
"syscall"
13+
"text/template"
1314
"time"
1415

1516
"github.com/docker/go-units"
@@ -88,7 +89,6 @@ func Inspect(instName string) (*Instance, error) {
8889
if err == nil {
8990
inst.Disk = disk
9091
}
91-
inst.Message = y.Message
9292
inst.Networks = y.Networks
9393
inst.SSHLocalPort = *y.SSH.LocalPort // maybe 0
9494

@@ -137,6 +137,26 @@ func Inspect(instName string) (*Instance, error) {
137137
}
138138
}
139139

140+
tmpl, err := template.New("format").Parse(y.Message)
141+
if err != nil {
142+
inst.Errors = append(inst.Errors, fmt.Errorf("message %q is not a valid template: %w", y.Message, err))
143+
inst.Status = StatusBroken
144+
} else {
145+
data, err := AddGlobalFields(inst)
146+
if err != nil {
147+
inst.Errors = append(inst.Errors, fmt.Errorf("cannot add global fields to instance data: %w", err))
148+
inst.Status = StatusBroken
149+
} else {
150+
var message strings.Builder
151+
err = tmpl.Execute(&message, data)
152+
if err != nil {
153+
inst.Errors = append(inst.Errors, fmt.Errorf("cannot execute template %q: %w", y.Message, err))
154+
inst.Status = StatusBroken
155+
} else {
156+
inst.Message = message.String()
157+
}
158+
}
159+
}
140160
return inst, nil
141161
}
142162

0 commit comments

Comments
 (0)