Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: permit absolute paths for bundle create #554

Merged
merged 8 commits into from
Apr 23, 2024
12 changes: 6 additions & 6 deletions src/pkg/bundle/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ func (b *Bundle) ValidateBundleResources(spinner *message.Spinner) error {

// validate access to packages as well as components referenced in the package
for idx, pkg := range bundle.Packages {
// if package path is set, make it relative to source directory
if pkg.Path != "" {
pkg.Path = filepath.Join(b.cfg.CreateOpts.SourceDirectory, pkg.Path)
}

spinner.Updatef("Validating Bundle Package: %s", pkg.Name)
if pkg.Name == "" {
Expand Down Expand Up @@ -158,7 +154,7 @@ func (b *Bundle) ValidateBundleResources(spinner *message.Spinner) error {
if utils.IsRegistryURL(b.cfg.CreateOpts.Output) {
return fmt.Errorf("detected local Zarf package: %s, outputting to an OCI registry is not supported when using local Zarf packages", pkg.Name)
}
path := getPkgPath(pkg, bundle.Metadata.Architecture)
path := getPkgPath(pkg, bundle.Metadata.Architecture, b.cfg.CreateOpts.SourceDirectory)
bundle.Packages[idx].Path = path
}

Expand Down Expand Up @@ -212,9 +208,13 @@ func (b *Bundle) ValidateBundleResources(spinner *message.Spinner) error {
return nil
}

func getPkgPath(pkg types.Package, arch string) string {
func getPkgPath(pkg types.Package, arch string, srcDir string) string {
var fullPkgName string
var path string
// Set path relative to the source directory if not absolute
if !filepath.IsAbs(pkg.Path) {
pkg.Path = filepath.Join(srcDir, pkg.Path)
}
if strings.HasSuffix(pkg.Path, ".tar.zst") {
// use the provided pkg tarball
path = pkg.Path
Expand Down
42 changes: 28 additions & 14 deletions src/pkg/bundle/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ func Test_validateOverrides(t *testing.T) {

func Test_getPkgPath(t *testing.T) {
type args struct {
pkg types.Package
arch string
pkg types.Package
arch string
srcDir string
}
tests := []struct {
name string
Expand All @@ -160,39 +161,52 @@ func Test_getPkgPath(t *testing.T) {
{
name: "init full path",
args: args{
pkg: types.Package{Name: "init", Ref: "0.0.1", Path: "../fake/path/custom-init.tar.zst"},
arch: "fake64",
pkg: types.Package{Name: "init", Ref: "0.0.1", Path: "../fake/path/custom-init.tar.zst"},
arch: "fake64",
srcDir: "/mock/source",
},
want: "../fake/path/custom-init.tar.zst",
UncleGedd marked this conversation as resolved.
Show resolved Hide resolved
want: "/mock/fake/path/custom-init.tar.zst",
},
{
name: "init directory only path",
args: args{
pkg: types.Package{Name: "init", Ref: "0.0.1", Path: "../fake/path"},
arch: "fake64",
pkg: types.Package{Name: "init", Ref: "0.0.1", Path: "../fake/path"},
arch: "fake64",
srcDir: "/mock/source",
},
want: "../fake/path/zarf-init-fake64-0.0.1.tar.zst",
want: "/mock/fake/path/zarf-init-fake64-0.0.1.tar.zst",
},
{
name: "full path",
args: args{
pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "./fake/zarf-package-nginx-fake64-0.0.1.tar.zst"},
arch: "fake64",
pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "./fake/zarf-package-nginx-fake64-0.0.1.tar.zst"},
arch: "fake64",
srcDir: "/mock/source",
},
want: "./fake/zarf-package-nginx-fake64-0.0.1.tar.zst",
want: "/mock/source/fake/zarf-package-nginx-fake64-0.0.1.tar.zst",
},
{
name: "directory only path",
args: args{
pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "/fake"},
arch: "fake64",
pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "fake"},
arch: "fake64",
srcDir: "/mock/source",
},
want: "/mock/source/fake/zarf-package-nginx-fake64-0.0.1.tar.zst",
},
{
name: "absolute path",
args: args{
pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "/fake/zarf-package-nginx-fake64-0.0.1.tar.zst"},
arch: "fake64",
srcDir: "/mock/source",
},
want: "/fake/zarf-package-nginx-fake64-0.0.1.tar.zst",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := getPkgPath(tt.args.pkg, tt.args.arch)
path := getPkgPath(tt.args.pkg, tt.args.arch, tt.args.srcDir)
require.Equal(t, tt.want, path)
})
}
Expand Down
Loading