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

Enhance Atmos CLI: Add Support for Custom Base Path and Config Paths #1091

Open
wants to merge 97 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
7f13ce6
refactor InitCliConfig
haitham911 Feb 17, 2025
9ed97f7
add readConfig function
haitham911 Feb 18, 2025
5389ffc
Merge branch 'main' into Viper-built-in-configuration
haitham911 Feb 18, 2025
1fe3e66
add load config
haitham911 Feb 18, 2025
1cfec36
[autofix.ci] apply automated fixes
autofix-ci[bot] Feb 18, 2025
3ad2d48
add default.go & update snapshots
haitham911 Feb 18, 2025
263d14a
resolve comments
haitham911 Feb 18, 2025
2532cfc
remove unused function processConfigFile
haitham911 Feb 19, 2025
86bf3cc
improve log
haitham911 Feb 19, 2025
9de9d7b
[autofix.ci] apply automated fixes
autofix-ci[bot] Feb 19, 2025
037560c
add imports
haitham911 Feb 21, 2025
5e134f5
update snapshots
haitham911 Feb 21, 2025
413f955
add import config test
haitham911 Feb 21, 2025
eae3f97
[autofix.ci] apply automated fixes
autofix-ci[bot] Feb 21, 2025
6c2f103
refactor LoadConfig into smaller helper functions
haitham911 Feb 21, 2025
0bc7b4a
update Valid_Log_Level snapshots
haitham911 Feb 21, 2025
f87f1e9
update log snapshots
haitham911 Feb 21, 2025
d660739
update snapshots
haitham911 Feb 22, 2025
1858c2b
update snapshots Valid_Log_Level
haitham911 Feb 22, 2025
36cd759
Merge branch 'main' into atmos-import-config
haitham911 Feb 22, 2025
daaa03c
remove //nolint:revive
haitham911 Feb 22, 2025
c0db066
Exclude any line containing "log."
haitham911 Feb 22, 2025
8920e56
fix lint revive.add-constant
haitham911 Feb 22, 2025
09fe32d
update add-constant to include "error","path"
haitham911 Feb 22, 2025
e4b6f24
improve code lint
haitham911 Feb 22, 2025
da8006a
improve log
haitham911 Feb 22, 2025
54bb994
improve log charmbracelet aliased
haitham911 Feb 23, 2025
36ab8e8
Preprocess Atmos config with custom functions
haitham911 Feb 24, 2025
e42aa94
[autofix.ci] apply automated fixes
autofix-ci[bot] Feb 24, 2025
19136b0
remove print line
haitham911 Feb 24, 2025
0c95ad9
improve logs
haitham911 Feb 24, 2025
b2669c7
refactor processScalarNode
haitham911 Feb 24, 2025
ab18aab
improve logs
haitham911 Feb 24, 2025
0a9353a
[autofix.ci] apply automated fixes
autofix-ci[bot] Feb 24, 2025
462b5bf
fix linter error
haitham911 Feb 24, 2025
55a9b05
fix linter errors
haitham911 Feb 24, 2025
115006b
update snapshots
haitham911 Feb 24, 2025
6180ff5
fix linter errors
haitham911 Feb 24, 2025
6f5d329
fix linter
haitham911 Feb 24, 2025
21ae927
add global --base-path ,--config ,--config-path
haitham911 Feb 25, 2025
982d33f
add embed atmos config
haitham911 Feb 25, 2025
d5c4373
add global --base-path ,--config ,--config-path
haitham911 Feb 25, 2025
6a09b65
enhance code
haitham911 Feb 25, 2025
0b4f71d
go format file
haitham911 Feb 25, 2025
8afa420
[autofix.ci] apply automated fixes
autofix-ci[bot] Feb 25, 2025
1cb9623
disable atmos_vendor_pull
haitham911 Feb 25, 2025
d4f7630
update snapshots Valid_Log_Level_in_Environment_Variable
haitham911 Feb 25, 2025
ea71b2f
update snapshots
haitham911 Feb 25, 2025
7ebb430
Merge branch 'atmos-import-config' into DEV-2790
haitham911 Feb 25, 2025
483782e
updates snapshots
haitham911 Feb 25, 2025
729e119
update snapshots
haitham911 Feb 25, 2025
7cda8a4
Merge branch 'main' into atmos-import-config
haitham911 Mar 4, 2025
2ee18b9
set config path dir
haitham911 Mar 4, 2025
25688c6
update snapshots
haitham911 Mar 5, 2025
351c73b
add processStackConfigs on config.go
haitham911 Mar 5, 2025
9ab9d3c
[autofix.ci] apply automated fixes
autofix-ci[bot] Mar 5, 2025
eff1fd7
fix linter errors config.go
haitham911 Mar 9, 2025
6c661ac
add nolint:gocritic for InitCliConfig
haitham911 Mar 9, 2025
3bceb0b
Merge branch 'atmos-import-config' into DEV-2790
haitham911 Mar 9, 2025
4a3b34a
Merge branch 'main' into atmos-import-config
osterman Mar 12, 2025
60bed99
fix readEnvAmosConfigPath
haitham911 Mar 12, 2025
2cdd2b9
[autofix.ci] apply automated fixes
autofix-ci[bot] Mar 12, 2025
2742bb9
improve log
haitham911 Mar 12, 2025
33f1aec
Merge branch 'main' into atmos-import-config
aknysh Mar 12, 2025
498baca
Merge branch 'main' into atmos-import-config
haitham911 Mar 12, 2025
c8accb4
fix import
haitham911 Mar 12, 2025
23423b3
Merge branch 'main' into atmos-import-config
haitham911 Mar 13, 2025
69269ff
Merge branch 'main' into atmos-import-config
osterman Mar 14, 2025
0043a45
Merge branch 'main' into atmos-import-config
haitham911 Mar 15, 2025
a7ff480
update snapshots
haitham911 Mar 16, 2025
3548783
fix linter error
haitham911 Mar 16, 2025
76b4971
Apply suggestions from code review
osterman Mar 17, 2025
a33f5ab
[autofix.ci] apply automated fixes
autofix-ci[bot] Mar 17, 2025
df9c5a6
remove log
haitham911 Mar 17, 2025
a0b0e45
update snap shots
haitham911 Mar 17, 2025
31251b1
Merge branch 'main' into atmos-import-config
haitham911 Mar 17, 2025
501a94b
add ProcessSchemas
haitham911 Mar 17, 2025
2040b9e
resolve comments
haitham911 Mar 17, 2025
f78b7b0
fix base path
haitham911 Mar 17, 2025
85e3d04
add doc
haitham911 Mar 17, 2025
c860c89
move functions to helper
haitham911 Mar 17, 2025
9ffb03d
Merge branch 'main' into atmos-import-config
haitham911 Mar 17, 2025
c936af3
update snapshots
haitham911 Mar 17, 2025
8871f80
[autofix.ci] apply automated fixes
autofix-ci[bot] Mar 17, 2025
29d78c2
update snapshot
haitham911 Mar 18, 2025
2bf442b
update snapshots
haitham911 Mar 18, 2025
4b6baad
update snapshots
haitham911 Mar 18, 2025
2bb9e3f
remove repository root configuration from CLI docs
haitham911 Mar 18, 2025
4ca552c
Merge branch 'atmos-import-config' into DEV-2790
haitham911 Mar 18, 2025
2444a40
update snapshots
haitham911 Mar 18, 2025
751240c
update doc
haitham911 Mar 18, 2025
ff38c59
update snapshot
haitham911 Mar 18, 2025
fe627ac
fix test TestExecuteVendorPullCommand
haitham911 Mar 18, 2025
4b3c1cb
fix tests TestExecuteWorkflowCmd,TestExecuteVendorPull
haitham911 Mar 18, 2025
bf346ea
add test cases
haitham911 Mar 18, 2025
9534bd4
[autofix.ci] apply automated fixes
autofix-ci[bot] Mar 18, 2025
d3195a0
Merge branch 'main' into DEV-2790
haitham911 Mar 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ func init() {
"Errors can be redirected to any file or any standard file descriptor (including `/dev/null`)")

RootCmd.PersistentFlags().String("logs-level", "Info", "Logs level. Supported log levels are Trace, Debug, Info, Warning, Off. If the log level is set to Off, Atmos will not log any messages")
RootCmd.PersistentFlags().String("logs-file", "/dev/stderr", "The file to write Atmos logs to. Logs can be written to any file or any standard file descriptor, including `/dev/stdout`, `/dev/stderr` and `/dev/null`")
RootCmd.PersistentFlags().String("logs-file", "/dev/stderr", "The file to write Atmos logs to. Logs can be written to any file or any standard file descriptor, including '/dev/stdout', '/dev/stderr' and '/dev/null'")
RootCmd.PersistentFlags().String("base-path", "", "Base path for Atmos project")
RootCmd.PersistentFlags().StringSlice("config", []string{}, "Paths to configuration file")
RootCmd.PersistentFlags().StringSlice("config-path", []string{}, "Path to configuration directory")

// Set custom usage template
err := templates.SetCustomUsageFunc(RootCmd)
Expand Down
13 changes: 12 additions & 1 deletion internal/exec/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,18 @@
return configAndStacksInfo, err
}

configAndStacksInfo.BasePath, err = cmd.Flags().GetString("base-path")
if err != nil {
return configAndStacksInfo, err
}

Check warning on line 214 in internal/exec/utils.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/utils.go#L213-L214

Added lines #L213 - L214 were not covered by tests
configAndStacksInfo.AtmosConfigFilesFromArg, err = cmd.Flags().GetStringSlice("config")
if err != nil {
return configAndStacksInfo, err
}

Check warning on line 218 in internal/exec/utils.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/utils.go#L217-L218

Added lines #L217 - L218 were not covered by tests
configAndStacksInfo.AtmosConfigDirsFromArg, err = cmd.Flags().GetStringSlice("config-path")
if err != nil {
return configAndStacksInfo, err
}

Check warning on line 222 in internal/exec/utils.go

View check run for this annotation

Codecov / codecov/patch

internal/exec/utils.go#L221-L222

Added lines #L221 - L222 were not covered by tests
Comment on lines +211 to +222
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Implemented CLI flags for configuration but missing test coverage.

The implementation properly retrieves the new CLI flags (--base-path, --config, and --config-path) and handles potential errors. This aligns with the PR objectives of enhancing configuration flexibility.

However, the error handling code for these flags (lines 210-211, 214-215, 218-219) lacks test coverage according to static analysis.


🏁 Script executed:

#!/bin/bash
# Check if there are tests for the new flag handling

echo "Searching for tests of these new CLI flags..."
rg -A 10 "GetString.*base-path" --glob "**/*_test.go"
rg -A 10 "GetStringSlice.*config" --glob "**/*_test.go"
rg -A 10 "GetStringSlice.*config-path" --glob "**/*_test.go"

echo -e "\nChecking test coverage for ProcessCommandLineArgs function..."
rg -A 3 -B 3 "func TestProcessCommandLineArgs" --glob "**/*_test.go"

Length of output: 487


Action: Add missing tests for CLI flag error handling in internal/exec/utils.go

The implementation for retrieving the CLI flags (--base-path, --config, and --config-path) is correctly handling error conditions. However, our investigation didn't reveal any tests covering the error paths in the code (lines 208–219). Please add test cases simulating error scenarios for GetString() and GetStringSlice() to ensure that these branches are properly exercised.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 210-211: internal/exec/utils.go#L210-L211
Added lines #L210 - L211 were not covered by tests


[warning] 214-215: internal/exec/utils.go#L214-L215
Added lines #L214 - L215 were not covered by tests


[warning] 218-219: internal/exec/utils.go#L218-L219
Added lines #L218 - L219 were not covered by tests

finalAdditionalArgsAndFlags := argsAndFlagsInfo.AdditionalArgsAndFlags
if len(additionalArgsAndFlags) > 0 {
finalAdditionalArgsAndFlags = append(finalAdditionalArgsAndFlags, additionalArgsAndFlags...)
Expand All @@ -219,7 +231,6 @@
configAndStacksInfo.ComponentType = componentType
configAndStacksInfo.ComponentFromArg = argsAndFlagsInfo.ComponentFromArg
configAndStacksInfo.GlobalOptions = argsAndFlagsInfo.GlobalOptions
configAndStacksInfo.BasePath = argsAndFlagsInfo.BasePath
configAndStacksInfo.TerraformCommand = argsAndFlagsInfo.TerraformCommand
configAndStacksInfo.TerraformDir = argsAndFlagsInfo.TerraformDir
configAndStacksInfo.HelmfileCommand = argsAndFlagsInfo.HelmfileCommand
Expand Down
7 changes: 6 additions & 1 deletion internal/exec/vendor_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ func TestExecuteVendorPullCommand(t *testing.T) {
cmd.PersistentFlags().Bool("dry-run", false, "Simulate pulling the latest version of the specified component from the remote repository without making any changes.")
cmd.PersistentFlags().String("tags", "", "Only vendor the components that have the specified tags")
cmd.PersistentFlags().Bool("everything", false, "Vendor all components")

cmd.PersistentFlags().String("base-path", "", "Base path for Atmos project")
cmd.PersistentFlags().StringSlice("config", []string{}, "Paths to configuration file")
cmd.PersistentFlags().StringSlice("config-path", []string{}, "Path to configuration directory")
// Execute the command
err = cmd.RunE(cmd, []string{})
assert.NoError(t, err, "'atmos vendor pull' command should execute without error")
Expand Down Expand Up @@ -103,6 +105,9 @@ func TestExecuteVendorPull(t *testing.T) {
}
// set vendor pull command
cmd := cobra.Command{}
cmd.PersistentFlags().String("base-path", "", "Base path for Atmos project")
cmd.PersistentFlags().StringSlice("config", []string{}, "Paths to configuration file")
cmd.PersistentFlags().StringSlice("config-path", []string{}, "Path to configuration directory")
flags := cmd.Flags()
flags.String("component", "", "")
flags.String("stack", "", "")
Expand Down
4 changes: 3 additions & 1 deletion internal/exec/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ atmos describe component c1 -s test
cmd.PersistentFlags().Bool("dry-run", false, "Simulate the workflow without making any changes")
cmd.PersistentFlags().String("from-step", "", "Resume the workflow from the specified step")
cmd.PersistentFlags().String("stack", "", "Execute the workflow for the specified stack")

cmd.PersistentFlags().String("base-path", "", "Base path for Atmos project")
cmd.PersistentFlags().StringSlice("config", []string{}, "Paths to configuration file")
cmd.PersistentFlags().StringSlice("config-path", []string{}, "Path to configuration directory")
// Execute the command
cmd.SetArgs([]string{"--file", "workflows", "show-all-describe-component-commands"})
err = cmd.Execute()
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/atmos.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
logs:
# Can also be set using 'ATMOS_LOGS_FILE' ENV var, or '--logs-file' command-line argument
# File or standard file descriptor to write logs to
# Logs can be written to any file or any standard file descriptor, including `/dev/stdout`, `/dev/stderr` and `/dev/null`
file: "/dev/stderr"
# Supported log levels: Trace, Debug, Info, Warning, Off
# Can also be set using 'ATMOS_LOGS_LEVEL' ENV var, or '--logs-level' command-line argument
level: Info
4 changes: 3 additions & 1 deletion pkg/config/const.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package config

const (
CliConfigFileName = "atmos"
CliConfigFileName = "atmos"
DotCliConfigFileName = ".atmos"

SystemDirConfigFilePath = "/usr/local/etc/atmos"
WindowsAppDataEnvVar = "LOCALAPPDATA"

Expand Down
51 changes: 41 additions & 10 deletions pkg/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"bytes"
_ "embed"
"errors"
"fmt"
"os"
Expand All @@ -16,10 +17,14 @@
"gopkg.in/yaml.v3"
)

//go:embed atmos.yaml
var embeddedConfigData []byte

const MaximumImportLvL = 10

var ErrAtmosDIrConfigNotFound = errors.New("atmos config directory not found")

// * Embedded atmos.yaml (`atmos/pkg/config/atmos.yaml`)
// * System dir (`/usr/local/etc/atmos` on Linux, `%LOCALAPPDATA%/atmos` on Windows).
// * Home directory (~/.atmos).
// * Current working directory.
Expand All @@ -31,8 +36,20 @@
v.SetConfigType("yaml")
v.SetTypeByDefaultValue(true)
setDefaultConfiguration(v)
// Load embed atmos.yaml
if err := loadEmbeddedConfig(v); err != nil {
return atmosConfig, err
}

Check warning on line 42 in pkg/config/load.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/load.go#L41-L42

Added lines #L41 - L42 were not covered by tests
if len(configAndStacksInfo.AtmosConfigFilesFromArg) > 0 || len(configAndStacksInfo.AtmosConfigDirsFromArg) > 0 {
err := loadConfigFromCLIArgs(v, configAndStacksInfo, &atmosConfig)
if err != nil {
return atmosConfig, err
}
return atmosConfig, nil
}

// Load configuration from different sources.
if err := loadConfigSources(v, configAndStacksInfo.AtmosCliConfigPath); err != nil {
if err := loadConfigSources(v, configAndStacksInfo); err != nil {
return atmosConfig, err
}
// If no config file is used, fall back to the default CLI config.
Expand Down Expand Up @@ -81,7 +98,8 @@

// loadConfigSources delegates reading configs from each source,
// returning early if any step in the chain fails.
func loadConfigSources(v *viper.Viper, cliConfigPath string) error {
func loadConfigSources(v *viper.Viper, configAndStacksInfo *schema.ConfigAndStacksInfo) error {
// Check if --config flag is provided
if err := readSystemConfig(v); err != nil {
return err
}
Expand All @@ -98,7 +116,7 @@
return err
}

return readAtmosConfigCli(v, cliConfigPath)
return readAtmosConfigCli(v, configAndStacksInfo.AtmosCliConfigPath)
}

// readSystemConfig load config from system dir .
Expand All @@ -114,7 +132,7 @@
}

if len(configFilePath) > 0 {
err := mergeConfig(v, configFilePath, false)
err := mergeConfig(v, configFilePath, CliConfigFileName, false)
switch err.(type) {
case viper.ConfigFileNotFoundError:
return nil
Expand All @@ -132,7 +150,7 @@
return err
}
configFilePath := filepath.Join(home, ".atmos")
err = mergeConfig(v, configFilePath, true)
err = mergeConfig(v, configFilePath, CliConfigFileName, true)
if err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
Expand All @@ -151,7 +169,7 @@
if err != nil {
return err
}
err = mergeConfig(v, wd, true)
err = mergeConfig(v, wd, CliConfigFileName, true)
if err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
Expand All @@ -168,7 +186,7 @@
if atmosPath == "" {
return nil
}
err := mergeConfig(v, atmosPath, true)
err := mergeConfig(v, atmosPath, CliConfigFileName, true)

Check warning on line 189 in pkg/config/load.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/load.go#L189

Added line #L189 was not covered by tests
if err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
Expand All @@ -187,7 +205,7 @@
if len(atmosCliConfigPath) == 0 {
return nil
}
err := mergeConfig(v, atmosCliConfigPath, true)
err := mergeConfig(v, atmosCliConfigPath, CliConfigFileName, true)

Check warning on line 208 in pkg/config/load.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/load.go#L208

Added line #L208 was not covered by tests
switch err.(type) {
case viper.ConfigFileNotFoundError:
log.Debug("config not found", "file", atmosCliConfigPath)
Expand All @@ -199,9 +217,9 @@
}

// mergeConfig merge config from a specified path directory and process imports. Return error if config file does not exist.
func mergeConfig(v *viper.Viper, path string, processImports bool) error {
func mergeConfig(v *viper.Viper, path, fileName string, processImports bool) error {
v.AddConfigPath(path)
v.SetConfigName(CliConfigFileName)
v.SetConfigName(fileName)
err := v.MergeInConfig()
if err != nil {
return err
Expand Down Expand Up @@ -381,3 +399,16 @@

return nil
}

// loadEmbeddedConfig loads the embedded atmos.yaml configuration.
func loadEmbeddedConfig(v *viper.Viper) error {
// Create a reader from the embedded YAML data
reader := bytes.NewReader(embeddedConfigData)

// Merge the embedded configuration into Viper
if err := v.MergeConfig(reader); err != nil {
return fmt.Errorf("failed to merge embedded config: %w", err)
}

Check warning on line 411 in pkg/config/load.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/load.go#L410-L411

Added lines #L410 - L411 were not covered by tests

return nil
}
156 changes: 156 additions & 0 deletions pkg/config/load_config_args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package config

import (
"errors"
"fmt"
"os"
"path/filepath"

log "github.com/charmbracelet/log"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/spf13/viper"
)

var (
ErrExpectedDirOrPattern = errors.New("--config-path expected directory found file")
ErrFileNotFound = errors.New("file not found")
ErrExpectedFile = errors.New("--config expected file found directory")
ErrAtmosArgConfigNotFound = errors.New("atmos configuration not found")
ErrAtmosFilesDIrConfigNotFound = errors.New("`atmos.yaml` or `.atmos.yaml` configuration file not found on directory")
)

// loadConfigFromCLIArgs handles the loading of configurations provided via --config-path and --config.
func loadConfigFromCLIArgs(v *viper.Viper, configAndStacksInfo *schema.ConfigAndStacksInfo, atmosConfig *schema.AtmosConfiguration) error {
log.Debug("loading config from command line arguments")

configFilesArgs := configAndStacksInfo.AtmosConfigFilesFromArg
configDirsArgs := configAndStacksInfo.AtmosConfigDirsFromArg
var configPaths []string

// Merge all config from --config files
if len(configFilesArgs) > 0 {
if err := mergeFiles(v, configFilesArgs); err != nil {
return err
}
configPaths = append(configPaths, configFilesArgs...)
}

// Merge config from --config-path directories
if len(configDirsArgs) > 0 {
paths, err := mergeConfigFromDirectories(v, configDirsArgs)
if err != nil {
return err
}
configPaths = append(configPaths, paths...)
}

// Check if any config files were found from command line arguments
if len(configPaths) == 0 {
log.Debug("no config files found from command line arguments")
return ErrAtmosArgConfigNotFound
}

Check warning on line 51 in pkg/config/load_config_args.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/load_config_args.go#L49-L51

Added lines #L49 - L51 were not covered by tests

if err := v.Unmarshal(atmosConfig); err != nil {
return err
}

Check warning on line 55 in pkg/config/load_config_args.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/load_config_args.go#L54-L55

Added lines #L54 - L55 were not covered by tests

atmosConfig.CliConfigPath = connectPaths(configPaths)
return nil
}

// mergeFiles merges config files from the provided paths.
func mergeFiles(v *viper.Viper, configFilePaths []string) error {
err := validatedIsFiles(configFilePaths)
if err != nil {
return err
}
for _, configPath := range configFilePaths {
err := mergeConfigFile(configPath, v)
if err != nil {
log.Debug("error loading config file", "path", configPath, "error", err)
return err
}

Check warning on line 72 in pkg/config/load_config_args.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/load_config_args.go#L70-L72

Added lines #L70 - L72 were not covered by tests
log.Debug("config file merged", "path", configPath)
if err := mergeDefaultImports(configPath, v); err != nil {
log.Debug("error process imports", "path", configPath, "error", err)
}
if err := mergeImports(v); err != nil {
log.Debug("error process imports", "file", configPath, "error", err)
}

Check warning on line 79 in pkg/config/load_config_args.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/load_config_args.go#L78-L79

Added lines #L78 - L79 were not covered by tests
}
return nil
}
Comment on lines +61 to +82
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Merge files with partial error handling.

Errors in mergeDefaultImports and mergeImports only log a debug message. Consider returning errors if a partial merge is not acceptable.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 70-72: pkg/config/load_config_args.go#L70-L72
Added lines #L70 - L72 were not covered by tests


[warning] 78-79: pkg/config/load_config_args.go#L78-L79
Added lines #L78 - L79 were not covered by tests


// mergeConfigFromDirectories merges config files from the provided directories.
func mergeConfigFromDirectories(v *viper.Viper, dirPaths []string) ([]string, error) {
if err := validatedIsDirs(dirPaths); err != nil {
return nil, err
}
var configPaths []string
for _, confDirPath := range dirPaths {
err := mergeConfig(v, confDirPath, CliConfigFileName, true)
if err != nil {
log.Debug("Failed to find atmos config", "path", confDirPath, "error", err)
switch err.(type) {
case viper.ConfigFileNotFoundError:
log.Debug("Failed to found atmos config", "file", filepath.Join(confDirPath, CliConfigFileName))
default:
return nil, err

Check warning on line 98 in pkg/config/load_config_args.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/load_config_args.go#L93-L98

Added lines #L93 - L98 were not covered by tests
}
}
if err == nil {
log.Debug("atmos config file merged", "path", v.ConfigFileUsed())
configPaths = append(configPaths, confDirPath)
continue
}
err = mergeConfig(v, confDirPath, DotCliConfigFileName, true)
if err != nil {
log.Debug("Failed to found .atmos config", "path", filepath.Join(confDirPath, CliConfigFileName), "error", err)
return nil, fmt.Errorf("%w: %s", ErrAtmosFilesDIrConfigNotFound, confDirPath)
}
log.Debug(".atmos config file merged", "path", v.ConfigFileUsed())
configPaths = append(configPaths, confDirPath)

Check warning on line 112 in pkg/config/load_config_args.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/load_config_args.go#L106-L112

Added lines #L106 - L112 were not covered by tests
}
return configPaths, nil
}

func validatedIsDirs(dirPaths []string) error {
for _, dirPath := range dirPaths {
stat, err := os.Stat(dirPath)
if err != nil {
log.Debug("--config-path directory not found", "path", dirPath)
return err
}
if !stat.IsDir() {
log.Debug("--config-path expected directory found file", "path", dirPath)
return ErrAtmosDIrConfigNotFound
}

Check warning on line 127 in pkg/config/load_config_args.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/load_config_args.go#L125-L127

Added lines #L125 - L127 were not covered by tests
}
return nil
}

func validatedIsFiles(files []string) error {
for _, filePath := range files {
stat, err := os.Stat(filePath)
if err != nil {
log.Debug("--config file not found", "path", filePath)
return ErrFileNotFound
}
if stat.IsDir() {
log.Debug("--config expected file found directors", "path", filePath)
return ErrExpectedFile
}

Check warning on line 142 in pkg/config/load_config_args.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/load_config_args.go#L140-L142

Added lines #L140 - L142 were not covered by tests
}
return nil
}

func connectPaths(paths []string) string {
if len(paths) == 1 {
return paths[0]
}

Check warning on line 150 in pkg/config/load_config_args.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/load_config_args.go#L149-L150

Added lines #L149 - L150 were not covered by tests
var result string
for _, path := range paths {
result += path + ";"
}
return result
}
Loading
Loading