Skip to content

Commit 8e44a17

Browse files
authored
fix: use pointer handle null values (#679)
Note: Required after the update to `hashicorp/packer-plugin-sdk` v0.6.6. Change `DirPerm` in `OutputConfig` from `os.FileMode` to `*os.FileMode` so that HCL2 can correctly represent the field as optional/nullable. When `directory_permission` is not set in a onfiguration, the packer-sdc generated flat struct receives a cty null value for the field. Assigning a cty null to a non-pointer `os.FileMode` causes a runtime panic: "null value is not allowed". Using a pointer type allows the field to be nil when omitted, which is handled in `Prepare()` by falling back to the default permission of `0750`. The corresponding generated `hcl2spec.go` files (`output_config` and `step_export`) are updated to mirror the pointer type. Because the pointer is now declared in the source struct, future runs of `make generate` will regenerate these files with `*os.FileMode`. Fixes: - Runtime error: null value is not allowed [directory_permission] - Build error: cannot use DirPerm (type *os.FileMode) as os.FileMode Ref: #675 Signed-off-by: Ryan Johnson <ryan.johnson@broadcom.com>
1 parent e11f0fe commit 8e44a17

8 files changed

Lines changed: 23 additions & 17 deletions

File tree

.web-docs/components/builder/vsphere-clone/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ The above configuration would create the following files:
16411641
created, must be empty prior to running the builder. By default, this is
16421642
"output-<buildName>" where "buildName" is the name of the build.
16431643

1644-
- `directory_permission` (os.FileMode) - The permissions to apply to the "output_directory", and to any parent
1644+
- `directory_permission` (\*os.FileMode) - The permissions to apply to the "output_directory", and to any parent
16451645
directories that get created for output_directory. By default, this is
16461646
"0750". You should express the permission as quoted string with a
16471647
leading zero such as "0755" in JSON file, because JSON does not support

.web-docs/components/builder/vsphere-iso/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,7 @@ The above configuration would create the following files:
14891489
created, must be empty prior to running the builder. By default, this is
14901490
"output-<buildName>" where "buildName" is the name of the build.
14911491

1492-
- `directory_permission` (os.FileMode) - The permissions to apply to the "output_directory", and to any parent
1492+
- `directory_permission` (\*os.FileMode) - The permissions to apply to the "output_directory", and to any parent
14931493
directories that get created for output_directory. By default, this is
14941494
"0750". You should express the permission as quoted string with a
14951495
leading zero such as "0755" in JSON file, because JSON does not support

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ testacc: dev
2929

3030
generate: install-packer-sdc
3131
@go generate ./...
32+
# Patch the generated files to use *os.FileMode so null values are handled correctly.
33+
@for f in builder/vsphere/common/output_config.hcl2spec.go builder/vsphere/common/step_export.hcl2spec.go; do \
34+
awk '/DirPerm/{gsub(/ os\.FileMode/, " *os.FileMode")} {print}' "$$f" > "$$f.tmp" && mv "$$f.tmp" "$$f"; \
35+
done
36+
@go fmt ./...
3237
@rm -rf .docs
3338
@packer-sdc renderdocs -src "docs" -partials docs-partials/ -dst ".docs/"
3439
@./.web-docs/scripts/compile-to-webdocs.sh "." ".docs" ".web-docs" "hashicorp"

builder/vsphere/common/output_config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@ type OutputConfig struct {
3131
// leading zero such as "0755" in JSON file, because JSON does not support
3232
// octal value. In Unix-like OS, the actual permission may differ from
3333
// this value because of umask.
34-
DirPerm os.FileMode `mapstructure:"directory_permission" required:"false"`
34+
DirPerm *os.FileMode `mapstructure:"directory_permission" required:"false"`
3535
}
3636

3737
func (c *OutputConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig) []error {
3838
if c.OutputDir == "" {
3939
c.OutputDir = fmt.Sprintf("output-%s", pc.PackerBuildName)
4040
}
4141

42-
if runtime.GOOS != "windows" && c.DirPerm == 0 {
43-
c.DirPerm = 0750
42+
if runtime.GOOS != "windows" && (c.DirPerm == nil || *c.DirPerm == 0) {
43+
perm := os.FileMode(0750)
44+
c.DirPerm = &perm
4445
}
4546

4647
return nil

builder/vsphere/common/output_config.hcl2spec.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/vsphere/common/step_export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (c *ExportConfig) Prepare(ctx *interpolate.Context, lc *LocationConfig, pc
159159
}
160160

161161
// Check if the output directory exists.
162-
if err := os.MkdirAll(c.OutputDir.OutputDir, c.OutputDir.DirPerm); err != nil {
162+
if err := os.MkdirAll(c.OutputDir.OutputDir, *c.OutputDir.DirPerm); err != nil {
163163
errs = packersdk.MultiErrorAppend(errs, errors.Wrap(err, "unable to make directory for export"))
164164
}
165165

builder/vsphere/common/step_export.hcl2spec.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs-partials/builder/vsphere/common/OutputConfig-not-required.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
created, must be empty prior to running the builder. By default, this is
88
"output-<buildName>" where "buildName" is the name of the build.
99

10-
- `directory_permission` (os.FileMode) - The permissions to apply to the "output_directory", and to any parent
10+
- `directory_permission` (\*os.FileMode) - The permissions to apply to the "output_directory", and to any parent
1111
directories that get created for output_directory. By default, this is
1212
"0750". You should express the permission as quoted string with a
1313
leading zero such as "0755" in JSON file, because JSON does not support

0 commit comments

Comments
 (0)