Skip to content

Fix issues with config file/directory logic#20

Merged
nathanjcochran merged 6 commits intomainfrom
nathan/fix-config-flag
Sep 23, 2025
Merged

Fix issues with config file/directory logic#20
nathanjcochran merged 6 commits intomainfrom
nathan/fix-config-flag

Conversation

@nathanjcochran
Copy link
Member

@nathanjcochran nathanjcochran commented Sep 23, 2025

This PR fixes several issues with the config file/directory logic:

  1. The config file path passed as a flag was being used when initializing viper (see here and here), but the value loaded into the Config struct was using the result of GetConfigDir(), which does not take the flag into consideration. As a result, when the config file was saved, it would not be saved back to the location it was loaded from (i.e. the location provided via the --config flag).
  2. The default value for the --config flag was being provided by GetConfigDir() (see here). That function returned either the value provided via the TIGER_CONFIG_DIR env var or the default value. As a result, the flag's default value (i.e. in the help text) would change if the TIGER_CONFIG_DIR env var was present, which is not how the defaults for any of the other flags work if their corresponding env vars are set (they always show the "true" default value).
  3. The --config flag did not match the naming convention of the TIGER_CONFIG_DIR env var. Furthermore, the CLI flag expected a full file path, whereas the env var just expected the path to the directory. This was inconsistent with the other flags, which all have a 1:1 correspondence with their env vars.
  4. The functions for writing and reading the API key to/from a fall back file used the GetConfigDir() function to get the path to the config dir where the API key file is stored (e.g. see here). However, that meant there was no way to control this via a flag, since that function did not take the flag into account (only the TIGER_CONFIG_DIR env var).

I also fixed and cleaned up a bunch of tests, and added some new tests to cover some of these issues and prevent them from happening again.

@nathanjcochran nathanjcochran self-assigned this Sep 23, 2025
Comment on lines -80 to -81
defaultConfigFile := filepath.Join(config.GetConfigDir(), config.ConfigFileName)
cmd.PersistentFlags().StringVar(&cfgFile, "config", defaultConfigFile, "config file")
Copy link
Member Author

@nathanjcochran nathanjcochran Sep 23, 2025

Choose a reason for hiding this comment

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

I changed the flag from --config (which accepted the full file path to the config file) to --config-dir (which accepts the path to the directory containing the config file) to match the TIGER_CONFIG_DIR env var, and to make it clear that this config directory is used for both the config.yaml file as well as the API key fallback file. This is also in-line with the original spec.

I also updated it to use GetDefaultConfigDir as the default value (which is shown in the help output), which returns the "true" default value whether or not TIGER_CONFIG_DIR is set.

Comment on lines +63 to +66
// Configure viper to watch for file changes and update its in-memory
// representation of the config. Note that this won't automatically
// update [Config] structs already returned from [Load].
viper.WatchConfig()
Copy link
Member Author

Choose a reason for hiding this comment

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

This ensures that config file changes made while the MCP server is running take effect immediately (i.e. for the next tool call). This depends on the fact that the config is re-loaded via a call to Load() for each MCP tool call.

ConfigDir: GetConfigDir(),
}

// Use the global Viper instance that's already configured by initConfig() and bindFlags()
Copy link
Member Author

Choose a reason for hiding this comment

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

Neither of these function (initConfig() or bindFlags()) exist anywhere in the code base. Not sure where this comment came from, but I updated it to refer to the SetupViper function above, which seems most accurate.

return expandPath(dir)
}
return filepath.Dir(viper.ConfigFileUsed())
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This function was being called to construct the file path that the API key is stored in when the keychain is not available (e.g. here), but was not taking the flag into consideration. I redefined it to accurately reflect the value of the flag (if provided), or env var, or default. It would probably be better to pass the config struct into those functions and use the ConfigDir field, but that's a bit of a larger refactor I don't want to undertake at the present moment.

Comment on lines +199 to +209
func GetEffectiveConfigDir(configDirFlag *pflag.Flag) string {
if configDirFlag.Changed {
return expandPath(configDirFlag.Value.String())
}

if dir := os.Getenv("TIGER_CONFIG_DIR"); dir != "" {
return expandPath(dir)
}

return GetDefaultConfigDir()
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This new function is responsible for returning the --config-dir flag value (if set), or the TIGER_CONFIG_DIR env var value (if set), or the default value.

@nathanjcochran nathanjcochran marked this pull request as ready for review September 23, 2025 03:00
}
}

func TestStoreAPIKeyToFile(t *testing.T) {
Copy link
Member Author

@nathanjcochran nathanjcochran Sep 23, 2025

Choose a reason for hiding this comment

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

Moved into ./config/api_key_test.go. Should have moved these tests when I moved the corresponding functions in #12.

Comment on lines -70 to -74
if debug {
if configFile := viper.ConfigFileUsed(); configFile != "" {
fmt.Fprintln(os.Stderr, "Using config file:", configFile)
}
}
Copy link
Member Author

@nathanjcochran nathanjcochran Sep 23, 2025

Choose a reason for hiding this comment

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

This was duplicative with the information logged here, and it was generating a lot of noise in the go test output.

@nathanjcochran nathanjcochran merged commit 14b831c into main Sep 23, 2025
@nathanjcochran nathanjcochran deleted the nathan/fix-config-flag branch September 23, 2025 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants