Skip to content

Commit

Permalink
Fix config file overriding environment variable
Browse files Browse the repository at this point in the history
The convention for most programs is that command line options override
environment variables which in turn override config files. Removed the
test for empty as the option should be overridden unconditionally if it
exists in the environment.

In the old implementation, the host was set from the environment
variable in the first pass and then overridden by the config file in the
second pass.
  • Loading branch information
sotpapathe committed Aug 18, 2024
1 parent f4f28d1 commit 1e15a0c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ ncmpc 0.50 - not yet released
* build: require Meson 0.56
* lyrics/musixmatch: add new lyrics extension
* lyrics/google: fix partial loading of lyrics
* fix config file overriding MPD_HOST environment variable

ncmpc 0.49 - (2023-08-04)
* fix UI freeze if lyrics plugin is stuck
Expand Down
3 changes: 3 additions & 0 deletions src/Main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ try {
GetGlobalKeyBindings().Check(nullptr, 0);
#endif

/* set options from environment variables */
options_env();

/* parse command line options - 2 pass */
options_parse(argc, argv);

Expand Down
6 changes: 5 additions & 1 deletion src/Options.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,11 @@ options_parse(int argc, const char *argv[])
handle_option(opt->shortopt, nullptr);
else if (opt && opt->argument)
option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
}

if (options.host.empty() && getenv("MPD_HOST"))
void
options_env()
{
if (getenv("MPD_HOST"))
options.host = getenv("MPD_HOST");
}
2 changes: 2 additions & 0 deletions src/Options.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,6 @@ extern Options options;

void options_parse(int argc, const char **argv);

void options_env();

#endif

0 comments on commit 1e15a0c

Please sign in to comment.