parse winixjson_* environment variables as json config options

master
Tomasz Sowa 2 months ago
parent 589e889822
commit 66fae598c2

@ -244,7 +244,7 @@ bool ReadConfigs(const pt::Space::TableType & table, size_t & config_read)
}
void ReadEnvOption(const char * option)
void ReadEnvStringOption(const char * option)
{
const char winix_prefix[] = "winix_";
size_t prefix_len = sizeof(winix_prefix) - 1; // null terminating table
@ -253,7 +253,6 @@ void ReadEnvOption(const char * option)
if( pt::is_substr_nc(winix_prefix, option) )
{
pt::utf8_to_wide(option + prefix_len, opt);
std::wstring::size_type sep = opt.find('=');
if( sep != std::wstring::npos )
@ -268,12 +267,57 @@ void ReadEnvOption(const char * option)
}
void ReadConfigFromEnv(const char ** env)
bool ReadEnvJsonOption(const char * option)
{
const char winix_prefix[] = "winixjson_";
size_t prefix_len = sizeof(winix_prefix) - 1; // null terminating table
std::wstring opt;
bool status = true;
if( pt::is_substr_nc(winix_prefix, option) )
{
pt::utf8_to_wide(option + prefix_len, opt);
std::wstring::size_type sep = opt.find('=');
if( sep != std::wstring::npos )
{
pt::SpaceParser parser;
pt::Space value_space;
pt::SpaceParser::Status parse_status = parser.parse_json(opt.c_str() + sep + 1, value_space, false);
opt.erase(sep);
TemplatesFunctions::locale.ToSmall(opt);
switch(parse_status)
{
case pt::SpaceParser::Status::ok:
app.config.space.add(opt, value_space);
break;
default:
std::cout << "I cannot parse the " << option << " environment variable as JSON" << std::endl;
status = false;
break;
}
}
}
return status;
}
bool ReadConfigFromEnv(const char ** env)
{
bool status = true;
for(size_t i = 0 ; env[i] ; ++i)
{
ReadEnvOption(env[i]);
ReadEnvStringOption(env[i]);
if( !ReadEnvJsonOption(env[i]) )
status = false;
}
return status;
}
@ -307,7 +351,7 @@ bool ReadConfigs(const pt::Space & options, const char ** env)
if( options.has_key(L"use-env") )
{
ReadConfigFromEnv(env);
status = status && ReadConfigFromEnv(env);
}
app.config.AssignValuesFromSpace();

Loading…
Cancel
Save