Skip to content

Commit

Permalink
Read environment variables when creating filename from template
Browse files Browse the repository at this point in the history
Now it's possible to read a value from environment when creating
filename. This is defined with {env:ENV_VARIABLE} as a part of the
filename template.

For example:

environment:
export FOO=bar

configuration:
{
  ...
  "filename_template": "{env:FOO}.grib2"
}

--> resulting file is bar.grib2
  • Loading branch information
mpartio committed Mar 15, 2024
1 parent 18e805b commit 766cdb8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ Allowed template values are:
* {producer_id:NUMBER_FORMAT_SPECIFIER} - radon producer id
* {file_type} - file type extension, like grib, grib2, fqd, ...
* {wall_time:DATE_FORMAT_SPECIFIER} - wall clock time
* {env:ENVIRONMENT_VARIABLE_NAME} - value from an environment variable

Format specifiers:
* DATE_FORMAT_SPECIFIER: usual c-style strftime formatting (%Y%m%d...)
Expand Down
28 changes: 25 additions & 3 deletions himan-lib/source/filename.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "util.h"
#include "filename.h"
#include "forecast_time.h"
#include "level.h"
#include "param.h"
#include "util.h"
#include <fmt/printf.h>
#include <iomanip>
#include <regex>
Expand Down Expand Up @@ -47,6 +47,7 @@ string MakeFileNameFromTemplate(const info<T>& info, const plugin_configuration&
// {wall_time:FORMAT_SPECIFIER} - current wall clock time
// {masala_base} - environment variable MASALA_PROCESSED_DATA_BASE or
// MASALA_REF_BASE, depending on program name
// {env:ENV_VARIABLE} - environment variable ENV_VARIABLE

enum class Component
{
Expand All @@ -72,7 +73,8 @@ string MakeFileNameFromTemplate(const info<T>& info, const plugin_configuration&
kForecastTypeValue,
kProducerId,
kFileType,
kWallTime
kWallTime,
kEnvironmentVariable
};

auto ComponentToString = [](Component c) -> string
Expand Down Expand Up @@ -125,6 +127,8 @@ string MakeFileNameFromTemplate(const info<T>& info, const plugin_configuration&
return "producer_id";
case Component::kWallTime:
return "wall_time";
case Component::kEnvironmentVariable:
return "env";
default:
return "unknown";
}
Expand Down Expand Up @@ -326,6 +330,23 @@ string MakeFileNameFromTemplate(const info<T>& info, const plugin_configuration&
case Component::kWallTime:
replacement = raw_time::Now().String(mask);
break;
case Component::kEnvironmentVariable:
{
const auto tokens = util::Split(string(what[1]), ":");

if (tokens.size() != 2)
{
throw invalid_argument(fmt::format("Invalid environment variable mask: {}", mask));
}

auto val = util::GetEnv(tokens[1]);
if (val.empty())
{
throw invalid_argument(fmt::format("Environment variable '{}' not defined", mask));
}
replacement = val;
break;
}
default:
break;
}
Expand Down Expand Up @@ -368,7 +389,8 @@ string MakeFileNameFromTemplate(const info<T>& info, const plugin_configuration&
make_pair(Component::kForecastTypeValue, R"(\{(forecast_type_value)(:[:\.%0-9a-zA-Z_/-]*)*\})"),
make_pair(Component::kProducerId, R"(\{(producer_id)(:[:\.%0-9a-zA-Z_/-]*)*\})"),
make_pair(Component::kFileType, R"(\{(file_type)\})"),
make_pair(Component::kWallTime, R"(\{(wall_time)(:[:%a-zA-Z_/-]*)*\})")};
make_pair(Component::kWallTime, R"(\{(wall_time)(:[:%a-zA-Z_/-]*)*\})"),
make_pair(Component::kEnvironmentVariable, R"(\{(env:[a-zA-Z0-9_]+)\})")};

for (const auto& p : regexs)
{
Expand Down

0 comments on commit 766cdb8

Please sign in to comment.