Skip to content

Commit

Permalink
Revert "Accept yml as a possible file extension for the config file" (
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekthompson authored Dec 6, 2024
1 parent e7a5180 commit 89f9e68
Showing 1 changed file with 7 additions and 41 deletions.
48 changes: 7 additions & 41 deletions cmd/captain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ func setConfigContext(cmd *cobra.Command, cfg Config) error {

const (
captainDirectory = ".captain"
configFileName = "config"
configFileName = "config.yaml"
flakesFileName = "flakes.yaml"
quarantinesFileName = "quarantines.yaml"
timingsFileName = "timings.yaml"
)

var configFileExtensions = []string{"yaml", "yml"}

// findInParentDir starts at the current working directory and walk up to the root, trying
// to find the specified fileName
func findInParentDir(fileName string) (string, error) {
Expand Down Expand Up @@ -116,49 +114,17 @@ func findInParentDir(fileName string) (string, error) {
// Flags take precedence over all other options.
func InitConfig(cmd *cobra.Command, cliArgs CliArgs) (cfg Config, err error) {
if cliArgs.RootCliArgs.configFilePath == "" {
possibleConfigFilePaths := make([]string, 0, 2)
errs := make([]error, 0, 2)

for _, extension := range configFileExtensions {
configFilePath, err := findInParentDir(
filepath.Join(captainDirectory, fmt.Sprintf("%s.%s", configFileName, extension)),
)

if err == nil {
possibleConfigFilePaths = append(possibleConfigFilePaths, configFilePath)
} else {
errs = append(errs, err)
}
}

if len(possibleConfigFilePaths) > 1 {
cliArgs.RootCliArgs.configFilePath, err = findInParentDir(filepath.Join(captainDirectory, configFileName))
if err != nil && !errors.Is(err, os.ErrNotExist) {
return cfg, errors.NewConfigurationError(
"Unable to identify configuration file",
"Unable to read configuration file",
fmt.Sprintf(
"Captain found multiple configuration files in your environment: %s\n",
strings.Join(possibleConfigFilePaths, ", "),
"The following system error occurred while attempting to read the config file at %q: %s",
cliArgs.RootCliArgs.configFilePath, err.Error(),
),
"Please make sure only one config file is present in your environment or explicitly specify "+
"one using the '--config-file' flag.",
"Please make sure that Captain has the correct permissions to access the config file.",
)
}

if len(possibleConfigFilePaths) == 0 {
for _, err := range errs {
if err != nil && !errors.Is(err, os.ErrNotExist) {
return cfg, errors.NewConfigurationError(
"Unable to read configuration file",
fmt.Sprintf(
"The following system error occurred while attempting to read the config file at %q: %s",
cliArgs.RootCliArgs.configFilePath, err.Error(),
),
"Please make sure that Captain has the correct permissions to access the config file.",
)
}
}
}

cliArgs.RootCliArgs.configFilePath = possibleConfigFilePaths[0]
}

if cliArgs.RootCliArgs.configFilePath != "" {
Expand Down

0 comments on commit 89f9e68

Please sign in to comment.