From 32d5a75433d1064e916191972e2dadf456c65b40 Mon Sep 17 00:00:00 2001 From: haitham911 Date: Sun, 20 Oct 2024 15:16:23 +0300 Subject: [PATCH 1/4] Add function to search for config file with specified extensions --- pkg/utils/file_utils.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkg/utils/file_utils.go b/pkg/utils/file_utils.go index 693ae10b7..5263ebf99 100644 --- a/pkg/utils/file_utils.go +++ b/pkg/utils/file_utils.go @@ -173,3 +173,18 @@ func IsSocket(path string) (bool, error) { isSocket := fileInfo.Mode().Type() == fs.ModeSocket return isSocket, nil } + +// search for a config file in the provided path with the provided extensions .yaml, .yml +func SearchConfigFile(path string) (string, bool) { + // Define the possible config file extensions + configExtensions := []string{".yaml", ".yml"} + // remove extension from path + path = strings.TrimSuffix(path, filepath.Ext(path)) + for _, ext := range configExtensions { + filePath := path + ext + if FileExists(filePath) { + return filePath, true + } + } + return "", false +} From a75fef00336c34db763392ddb910060d6bb1f5a8 Mon Sep 17 00:00:00 2001 From: haitham911 Date: Sun, 20 Oct 2024 15:16:47 +0300 Subject: [PATCH 2/4] Refactor file existence check --- internal/exec/vendor_utils.go | 5 +++-- pkg/config/config.go | 9 +++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/internal/exec/vendor_utils.go b/internal/exec/vendor_utils.go index 0f95f12bb..e7d291619 100644 --- a/internal/exec/vendor_utils.go +++ b/internal/exec/vendor_utils.go @@ -131,9 +131,10 @@ func ReadAndProcessVendorConfigFile(cliConfig schema.CliConfiguration, vendorCon } foundVendorConfigFile := vendorConfigFile - // Look for the vendoring manifest in the current directory - if !u.FileExists(vendorConfigFile) { + foundVendorConfigFile, fileExists := u.SearchConfigFile(foundVendorConfigFile) + + 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) diff --git a/pkg/config/config.go b/pkg/config/config.go index 0a719d79b..63014bf4d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -338,11 +338,12 @@ 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 } @@ -350,7 +351,7 @@ func processConfigFile( 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) From 19c054252871de099655dd6839ec88f38d56a760 Mon Sep 17 00:00:00 2001 From: haitham911 Date: Sun, 20 Oct 2024 15:49:20 +0300 Subject: [PATCH 3/4] Refactor file existence check and search for config file with specified extensions --- cmd/cmd_utils.go | 4 ++-- internal/exec/vendor_utils.go | 10 ++-------- pkg/config/const.go | 2 +- pkg/utils/file_utils.go | 10 +++++++--- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/cmd/cmd_utils.go b/cmd/cmd_utils.go index ad4debf4f..a8db5f541 100644 --- a/cmd/cmd_utils.go +++ b/cmd/cmd_utils.go @@ -225,7 +225,7 @@ func executeCustomCommand( } if component == "" || component == "" { 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' @@ -235,7 +235,7 @@ func executeCustomCommand( } if stack == "" || stack == "" { 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 diff --git a/internal/exec/vendor_utils.go b/internal/exec/vendor_utils.go index e7d291619..f776f5f75 100644 --- a/internal/exec/vendor_utils.go +++ b/internal/exec/vendor_utils.go @@ -125,14 +125,8 @@ 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 - // Look for the vendoring manifest in the current directory - foundVendorConfigFile, fileExists := u.SearchConfigFile(foundVendorConfigFile) + // Check if the vendoring manifest file exists + foundVendorConfigFile, fileExists := u.SearchConfigFile(vendorConfigFile) if !fileExists { // Look for the vendoring manifest in the directory pointed to by the `base_path` setting in the `atmos.yaml` diff --git a/pkg/config/const.go b/pkg/config/const.go index 530ce3192..1afe89630 100644 --- a/pkg/config/const.go +++ b/pkg/config/const.go @@ -3,7 +3,7 @@ package config const ( DefaultStackConfigFileExtension = ".yaml" DefaultVendoringManifestFileExtension = ".yaml" - CliConfigFileName = "atmos.yaml" + CliConfigFileName = "atmos" SystemDirConfigFilePath = "/usr/local/etc/atmos" WindowsAppDataEnvVar = "LOCALAPPDATA" diff --git a/pkg/utils/file_utils.go b/pkg/utils/file_utils.go index 5263ebf99..5cf32bb66 100644 --- a/pkg/utils/file_utils.go +++ b/pkg/utils/file_utils.go @@ -174,12 +174,16 @@ func IsSocket(path string) (bool, error) { return isSocket, nil } -// search for a config file in the provided path with the provided extensions .yaml, .yml +// 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"} - // remove extension from path - path = strings.TrimSuffix(path, filepath.Ext(path)) for _, ext := range configExtensions { filePath := path + ext if FileExists(filePath) { From 9a9afb68edea55d316bba8ba72d91ccc0a8e6b09 Mon Sep 17 00:00:00 2001 From: haitham911 Date: Sun, 20 Oct 2024 15:57:55 +0300 Subject: [PATCH 4/4] Refactor AtmosVendorConfigFileName constant to remove file extension --- pkg/config/const.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/const.go b/pkg/config/const.go index 1afe89630..720f53507 100644 --- a/pkg/config/const.go +++ b/pkg/config/const.go @@ -39,7 +39,7 @@ const ( HelpFlag2 = "--help" ComponentVendorConfigFileName = "component.yaml" - AtmosVendorConfigFileName = "vendor.yaml" + AtmosVendorConfigFileName = "vendor" ImportSectionName = "import" OverridesSectionName = "overrides"