From cd124494bc746c9e95160ec0ece9904f372ae479 Mon Sep 17 00:00:00 2001 From: Chris Gunn Date: Tue, 7 Jan 2025 15:00:32 -0800 Subject: [PATCH] Prevalidate the --rpm-source args. (#67) Before starting the customization process, do some basic validation of the `--rpm-source` args. --- .../pkg/imagecustomizerlib/imagecustomizer.go | 5 +++++ .../pkg/imagecustomizerlib/rpmsourcesmounts.go | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/toolkit/tools/pkg/imagecustomizerlib/imagecustomizer.go b/toolkit/tools/pkg/imagecustomizerlib/imagecustomizer.go index 55f2007a7a..006ee31e2c 100644 --- a/toolkit/tools/pkg/imagecustomizerlib/imagecustomizer.go +++ b/toolkit/tools/pkg/imagecustomizerlib/imagecustomizer.go @@ -125,6 +125,11 @@ func createImageCustomizerParameters(buildDir string, ic.useBaseImageRpmRepos = useBaseImageRpmRepos ic.rpmsSources = rpmsSources + err = validateRpmSources(rpmsSources) + if err != nil { + return nil, err + } + ic.enableShrinkFilesystems = enableShrinkFilesystems ic.outputSplitPartitionsFormat = outputSplitPartitionsFormat diff --git a/toolkit/tools/pkg/imagecustomizerlib/rpmsourcesmounts.go b/toolkit/tools/pkg/imagecustomizerlib/rpmsourcesmounts.go index af28fe44b2..fe80e7a03b 100644 --- a/toolkit/tools/pkg/imagecustomizerlib/rpmsourcesmounts.go +++ b/toolkit/tools/pkg/imagecustomizerlib/rpmsourcesmounts.go @@ -110,9 +110,6 @@ func (m *rpmSourcesMounts) mountRpmSourcesHelper(buildDir string, imageChroot *s case "repo": err = m.createRepoFromRepoConfig(rpmSource, true, allReposConfig, imageChroot) - - default: - return fmt.Errorf("unknown RPM source type (%s):\nmust be a .repo file or a directory", rpmSource) } if err != nil { return err @@ -274,6 +271,17 @@ func (m *rpmSourcesMounts) close() error { return nil } +func validateRpmSources(rpmsSources []string) error { + for _, rpmSource := range rpmsSources { + _, err := getRpmSourceFileType(rpmSource) + if err != nil { + return err + } + } + + return nil +} + func getRpmSourceFileType(rpmSourcePath string) (string, error) { // First, check if path points to a directory. isDir, err := file.IsDir(rpmSourcePath) @@ -297,7 +305,7 @@ func getRpmSourceFileType(rpmSourcePath string) (string, error) { return "repo", nil default: - return "", nil + return "", fmt.Errorf("unknown RPM source type (%s):\nmust be a .repo file or a directory", rpmSourcePath) } }