Skip to content

Commit 3207075

Browse files
authored
Merge pull request #4211 from vax-r/provision_pointer
Make Provision.Script a pointer
2 parents eeeb471 + e7cb6aa commit 3207075

File tree

6 files changed

+19
-18
lines changed

6 files changed

+19
-18
lines changed

pkg/cidata/cidata.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ func GenerateISO9660(ctx context.Context, drv driver.Driver, instDir, name strin
400400
case limatype.ProvisionModeSystem, limatype.ProvisionModeUser, limatype.ProvisionModeDependency:
401401
layout = append(layout, iso9660util.Entry{
402402
Path: fmt.Sprintf("provision.%s/%08d", f.Mode, i),
403-
Reader: strings.NewReader(f.Script),
403+
Reader: strings.NewReader(*f.Script),
404404
})
405405
case limatype.ProvisionModeData:
406406
layout = append(layout, iso9660util.Entry{
@@ -490,7 +490,7 @@ func getBootCmds(p []limatype.Provision) []BootCmds {
490490
for _, f := range p {
491491
if f.Mode == limatype.ProvisionModeBoot {
492492
lines := []string{}
493-
for _, line := range strings.Split(f.Script, "\n") {
493+
for _, line := range strings.Split(*f.Script, "\n") {
494494
if line == "" {
495495
continue
496496
}

pkg/limatmpl/embed.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ func (tmpl *Template) embedAllScripts(ctx context.Context, embedAll bool) error
663663
continue
664664
}
665665
default:
666-
if p.Script != "" {
666+
if p.Script != nil && *p.Script != "" {
667667
continue
668668
}
669669
}

pkg/limatype/lima_yaml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ const (
238238
type Provision struct {
239239
Mode ProvisionMode `yaml:"mode,omitempty" json:"mode,omitempty" jsonschema:"default=system"`
240240
SkipDefaultDependencyResolution *bool `yaml:"skipDefaultDependencyResolution,omitempty" json:"skipDefaultDependencyResolution,omitempty"`
241-
Script string `yaml:"script,omitempty" json:"script,omitempty"`
241+
Script *string `yaml:"script,omitempty" json:"script,omitempty"`
242242
File *LocatorWithDigest `yaml:"file,omitempty" json:"file,omitempty" jsonschema:"nullable"`
243243
Playbook string `yaml:"playbook,omitempty" json:"playbook,omitempty"` // DEPRECATED
244244
// All ProvisionData fields must be nil unless Mode is ProvisionModeData

pkg/limayaml/defaults.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,12 +439,11 @@ func FillDefault(ctx context.Context, y, d, o *limatype.LimaYAML, filePath strin
439439
provision.Permissions = ptr.Of("644")
440440
}
441441
}
442-
// TODO Turn Script into a pointer; it is a plain string for historical reasons only
443-
if provision.Script != "" {
444-
if out, err := executeGuestTemplate(provision.Script, instDir, y.User, y.Param); err == nil {
445-
provision.Script = out.String()
442+
if provision.Script != nil && *provision.Script != "" {
443+
if out, err := executeGuestTemplate(*provision.Script, instDir, y.User, y.Param); err == nil {
444+
*provision.Script = out.String()
446445
} else {
447-
logrus.WithError(err).Warnf("Couldn't process provisioning script %q as a template", provision.Script)
446+
logrus.WithError(err).Warnf("Couldn't process provisioning script %q as a template", *provision.Script)
448447
}
449448
}
450449
}

pkg/limayaml/defaults_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func TestFillDefault(t *testing.T) {
150150
},
151151
MountType: ptr.Of(limatype.NINEP),
152152
Provision: []limatype.Provision{
153-
{Script: "#!/bin/true # {{.Param.ONE}}"},
153+
{Script: ptr.Of("#!/bin/true # {{.Param.ONE}}")},
154154
},
155155
Probes: []limatype.Probe{
156156
{Script: "#!/bin/false # {{.Param.ONE}}"},
@@ -234,7 +234,7 @@ func TestFillDefault(t *testing.T) {
234234

235235
expect.Provision = slices.Clone(y.Provision)
236236
expect.Provision[0].Mode = limatype.ProvisionModeSystem
237-
expect.Provision[0].Script = "#!/bin/true # Eins"
237+
expect.Provision[0].Script = ptr.Of("#!/bin/true # Eins")
238238

239239
expect.Probes = slices.Clone(y.Probes)
240240
expect.Probes[0].Mode = limatype.ProbeModeReadiness
@@ -364,7 +364,7 @@ func TestFillDefault(t *testing.T) {
364364
},
365365
Provision: []limatype.Provision{
366366
{
367-
Script: "#!/bin/true",
367+
Script: ptr.Of("#!/bin/true"),
368368
Mode: limatype.ProvisionModeUser,
369369
},
370370
},
@@ -572,7 +572,7 @@ func TestFillDefault(t *testing.T) {
572572
MountInotify: ptr.Of(true),
573573
Provision: []limatype.Provision{
574574
{
575-
Script: "#!/bin/true",
575+
Script: ptr.Of("#!/bin/true"),
576576
Mode: limatype.ProvisionModeSystem,
577577
},
578578
},

pkg/limayaml/validate.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func Validate(y *limatype.LimaYAML, warn bool) error {
212212
errs = errors.Join(errs, fmt.Errorf("field `provision[%d].permissions` must be an octal number: %w", i, err))
213213
}
214214
default:
215-
if p.Script == "" && p.Mode != limatype.ProvisionModeAnsible {
215+
if (p.Script == nil || *p.Script == "") && p.Mode != limatype.ProvisionModeAnsible {
216216
errs = errors.Join(errs, fmt.Errorf("field `provision[%d].script` must not be empty", i))
217217
}
218218
if p.Content != nil {
@@ -238,7 +238,7 @@ func Validate(y *limatype.LimaYAML, warn bool) error {
238238
if p.Mode != limatype.ProvisionModeAnsible {
239239
errs = errors.Join(errs, fmt.Errorf("field `provision[%d].playbook can only be set when mode is %q", i, limatype.ProvisionModeAnsible))
240240
}
241-
if p.Script != "" {
241+
if p.Script != nil && *p.Script != "" {
242242
errs = errors.Join(errs, fmt.Errorf("field `provision[%d].script must be empty if playbook is set", i))
243243
}
244244
playbook := p.Playbook
@@ -247,8 +247,10 @@ func Validate(y *limatype.LimaYAML, warn bool) error {
247247
}
248248
logrus.Warnf("provision mode %q is deprecated, use `ansible-playbook %q` instead", limatype.ProvisionModeAnsible, playbook)
249249
}
250-
if strings.Contains(p.Script, "LIMA_CIDATA") {
251-
logrus.Warn("provisioning scripts should not reference the LIMA_CIDATA variables")
250+
if p.Script != nil {
251+
if strings.Contains(*p.Script, "LIMA_CIDATA") {
252+
logrus.Warn("provisioning scripts should not reference the LIMA_CIDATA variables")
253+
}
252254
}
253255
}
254256
needsContainerdArchives := (y.Containerd.User != nil && *y.Containerd.User) || (y.Containerd.System != nil && *y.Containerd.System)
@@ -521,7 +523,7 @@ func validateParamIsUsed(y *limatype.LimaYAML) error {
521523
}
522524
keyIsUsed := false
523525
for _, p := range y.Provision {
524-
for _, ptr := range []*string{&p.Script, p.Content, p.Expression, p.Owner, p.Path, p.Permissions} {
526+
for _, ptr := range []*string{p.Script, p.Content, p.Expression, p.Owner, p.Path, p.Permissions} {
525527
if ptr != nil && re.MatchString(*ptr) {
526528
keyIsUsed = true
527529
break

0 commit comments

Comments
 (0)