Skip to content

Commit

Permalink
feat: support for .yml and .yaml file extensions for component ve…
Browse files Browse the repository at this point in the history
…ndoring (#725)

* feat: initial support for yml files for component vendoring

* add helper function
  • Loading branch information
RoseSecurity authored Oct 16, 2024
1 parent cfa2f21 commit 7a25ba3
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions internal/exec/vendor_component_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ import (
cp "github.com/otiai10/copy"
)

// findComponentConfigFile identifies the component vendoring config file (`component.yaml` or `component.yml`)
func findComponentConfigFile(basePath, fileName string) (string, error) {
componentConfigExtensions := []string{"yaml", "yml"}

for _, ext := range componentConfigExtensions {
configFilePath := path.Join(basePath, fmt.Sprintf("%s.%s", fileName, ext))
if u.FileExists(configFilePath) {
return configFilePath, nil
}
}
return "", fmt.Errorf("component vendoring config file does not exist in the '%s' folder", basePath)
}

// ReadAndProcessComponentVendorConfigFile reads and processes the component vendoring config file `component.yaml`
func ReadAndProcessComponentVendorConfigFile(
cliConfig schema.CliConfiguration,
Expand Down Expand Up @@ -50,12 +63,9 @@ func ReadAndProcessComponentVendorConfigFile(
return componentConfig, "", fmt.Errorf("folder '%s' does not exist", componentPath)
}

componentConfigFile := path.Join(componentPath, cfg.ComponentVendorConfigFileName)
if !u.FileExists(componentConfigFile) {
return componentConfig, "", fmt.Errorf("component vendoring config file '%s' does not exist in the '%s' folder",
cfg.ComponentVendorConfigFileName,
componentPath,
)
componentConfigFile, err := findComponentConfigFile(componentPath, strings.TrimSuffix(cfg.ComponentVendorConfigFileName, ".yaml"))
if err != nil {
return componentConfig, "", err
}

componentConfigFileContent, err := os.ReadFile(componentConfigFile)
Expand Down Expand Up @@ -92,7 +102,6 @@ func ExecuteComponentVendorInternal(
componentPath string,
dryRun bool,
) error {

var tempDir string
var err error
var t *template.Template
Expand Down Expand Up @@ -180,7 +189,7 @@ func ExecuteComponentVendorInternal(
},
}

var tempDir2 = tempDir
tempDir2 := tempDir
if sourceIsLocalFile {
tempDir2 = path.Join(tempDir, filepath.Base(uri))
}
Expand Down Expand Up @@ -287,7 +296,7 @@ func ExecuteComponentVendorInternal(
},
}

var componentPath2 = componentPath
componentPath2 := componentPath
if sourceIsLocalFile {
if filepath.Ext(componentPath) == "" {
componentPath2 = path.Join(componentPath, filepath.Base(uri))
Expand Down

0 comments on commit 7a25ba3

Please sign in to comment.