Skip to content

Commit 6ba3a91

Browse files
authored
Merge pull request #2179 from AkihiroSuda/yaml-guesttemplate-instname
limayaml: allow `{{.Name}}` in provision scripts
2 parents 95324c9 + bb07d79 commit 6ba3a91

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

pkg/limayaml/defaults.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func defaultGuestInstallPrefix() string {
147147
// - DNS are picked from the highest priority where DNS is not empty.
148148
// - CACertificates Files and Certs are uniquely appended in d, y, o order
149149
func FillDefault(y, d, o *LimaYAML, filePath string) {
150+
instDir := filepath.Dir(filePath)
150151
if y.VMType == nil {
151152
y.VMType = d.VMType
152153
}
@@ -393,7 +394,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
393394
if provision.Mode == ProvisionModeDependency && provision.SkipDefaultDependencyResolution == nil {
394395
provision.SkipDefaultDependencyResolution = ptr.Of(false)
395396
}
396-
if out, err := executeGuestTemplate(provision.Script); err == nil {
397+
if out, err := executeGuestTemplate(provision.Script, instDir); err == nil {
397398
provision.Script = out.String()
398399
} else {
399400
logrus.WithError(err).Warnf("Couldn't process provisioning script %q as a template", provision.Script)
@@ -462,7 +463,6 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
462463
}
463464

464465
y.PortForwards = append(append(o.PortForwards, y.PortForwards...), d.PortForwards...)
465-
instDir := filepath.Dir(filePath)
466466
for i := range y.PortForwards {
467467
FillPortForwardDefaults(&y.PortForwards[i], instDir)
468468
// After defaults processing the singular HostPort and GuestPort values should not be used again.
@@ -745,12 +745,13 @@ func fixUpForPlainMode(y *LimaYAML) {
745745
y.TimeZone = ptr.Of("")
746746
}
747747

748-
func executeGuestTemplate(format string) (bytes.Buffer, error) {
748+
func executeGuestTemplate(format, instDir string) (bytes.Buffer, error) {
749749
tmpl, err := template.New("").Parse(format)
750750
if err == nil {
751751
user, _ := osutil.LimaUser(false)
752752
data := map[string]string{
753753
"Home": fmt.Sprintf("/home/%s.linux", user.Username),
754+
"Name": filepath.Base(instDir),
754755
"UID": user.Uid,
755756
"User": user.Username,
756757
}
@@ -818,7 +819,7 @@ func FillPortForwardDefaults(rule *PortForward, instDir string) {
818819
}
819820
}
820821
if rule.GuestSocket != "" {
821-
if out, err := executeGuestTemplate(rule.GuestSocket); err == nil {
822+
if out, err := executeGuestTemplate(rule.GuestSocket, instDir); err == nil {
822823
rule.GuestSocket = out.String()
823824
} else {
824825
logrus.WithError(err).Warnf("Couldn't process guestSocket %q as a template", rule.GuestSocket)
@@ -838,7 +839,7 @@ func FillPortForwardDefaults(rule *PortForward, instDir string) {
838839

839840
func FillCopyToHostDefaults(rule *CopyToHost, instDir string) {
840841
if rule.GuestFile != "" {
841-
if out, err := executeGuestTemplate(rule.GuestFile); err == nil {
842+
if out, err := executeGuestTemplate(rule.GuestFile, instDir); err == nil {
842843
rule.GuestFile = out.String()
843844
} else {
844845
logrus.WithError(err).Warnf("Couldn't process guest %q as a template", rule.GuestFile)

0 commit comments

Comments
 (0)