Skip to content

Commit

Permalink
get atmos config and vendor from .yaml or .yml (#736)
Browse files Browse the repository at this point in the history
* Add function to search for config file with specified extensions

* Refactor file existence check

* Refactor file existence check and search for config file with specified extensions

* Refactor AtmosVendorConfigFileName constant to remove file extension

---------

Co-authored-by: Andriy Knysh <aknysh@users.noreply.github.com>
  • Loading branch information
haitham911 and aknysh authored Oct 20, 2024
1 parent 332a1a9 commit 4fa97ea
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func executeCustomCommand(
}
if component == "" || component == "<no value>" {
u.LogErrorAndExit(cliConfig, fmt.Errorf("the command defines an invalid 'component_config.component: %s' in '%s'",
commandConfig.ComponentConfig.Component, cfg.CliConfigFileName))
commandConfig.ComponentConfig.Component, cfg.CliConfigFileName+cfg.DefaultStackConfigFileExtension))
}

// Process Go templates in the command's 'component_config.stack'
Expand All @@ -235,7 +235,7 @@ func executeCustomCommand(
}
if stack == "" || stack == "<no value>" {
u.LogErrorAndExit(cliConfig, fmt.Errorf("the command defines an invalid 'component_config.stack: %s' in '%s'",
commandConfig.ComponentConfig.Stack, cfg.CliConfigFileName))
commandConfig.ComponentConfig.Stack, cfg.CliConfigFileName+cfg.DefaultStackConfigFileExtension))
}

// Get the config for the component in the stack
Expand Down
11 changes: 3 additions & 8 deletions internal/exec/vendor_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,10 @@ func ReadAndProcessVendorConfigFile(cliConfig schema.CliConfiguration, vendorCon
var vendorConfig schema.AtmosVendorConfig
vendorConfigFileExists := true

// If the vendoring manifest is specified without an extension, use the default extension
if filepath.Ext(vendorConfigFile) == "" {
vendorConfigFile = vendorConfigFile + cfg.DefaultVendoringManifestFileExtension
}

foundVendorConfigFile := vendorConfigFile
// Check if the vendoring manifest file exists
foundVendorConfigFile, fileExists := u.SearchConfigFile(vendorConfigFile)

// Look for the vendoring manifest in the current directory
if !u.FileExists(vendorConfigFile) {
if !fileExists {
// Look for the vendoring manifest in the directory pointed to by the `base_path` setting in the `atmos.yaml`
pathToVendorConfig := path.Join(cliConfig.BasePath, vendorConfigFile)

Expand Down
9 changes: 5 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,19 +338,20 @@ func processConfigFile(
path string,
v *viper.Viper,
) (bool, error) {
if !u.FileExists(path) {
// Check if the config file exists
configPath, fileExists := u.SearchConfigFile(path)
if !fileExists {
return false, nil
}

reader, err := os.Open(path)
reader, err := os.Open(configPath)
if err != nil {
return false, err
}

defer func(reader *os.File) {
err := reader.Close()
if err != nil {
u.LogWarning(cliConfig, fmt.Sprintf("error closing file '"+path+"'. "+err.Error()))
u.LogWarning(cliConfig, fmt.Sprintf("error closing file '"+configPath+"'. "+err.Error()))
}
}(reader)

Expand Down
4 changes: 2 additions & 2 deletions pkg/config/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
const (
DefaultStackConfigFileExtension = ".yaml"
DefaultVendoringManifestFileExtension = ".yaml"
CliConfigFileName = "atmos.yaml"
CliConfigFileName = "atmos"
SystemDirConfigFilePath = "/usr/local/etc/atmos"
WindowsAppDataEnvVar = "LOCALAPPDATA"

Expand Down Expand Up @@ -39,7 +39,7 @@ const (
HelpFlag2 = "--help"

ComponentVendorConfigFileName = "component.yaml"
AtmosVendorConfigFileName = "vendor.yaml"
AtmosVendorConfigFileName = "vendor"

ImportSectionName = "import"
OverridesSectionName = "overrides"
Expand Down
19 changes: 19 additions & 0 deletions pkg/utils/file_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,22 @@ func IsSocket(path string) (bool, error) {
isSocket := fileInfo.Mode().Type() == fs.ModeSocket
return isSocket, nil
}

// SearchConfigFile searches for a config file in the provided path.
// If the path has a file extension, it checks if the file exists.
// If the path does not have a file extension, it checks for the existence of the file with the provided path and the possible config file extensions
func SearchConfigFile(path string) (string, bool) {
// check if the provided has a file extension
if filepath.Ext(path) != "" {
return path, FileExists(path)
}
// Define the possible config file extensions
configExtensions := []string{".yaml", ".yml"}
for _, ext := range configExtensions {
filePath := path + ext
if FileExists(filePath) {
return filePath, true
}
}
return "", false
}

0 comments on commit 4fa97ea

Please sign in to comment.