more interpolation
parent
35248176cb
commit
130d539f81
5
Rex.cpp
5
Rex.cpp
|
@ -15,12 +15,15 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <cstring>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include "src/logger/Logger.h"
|
#include "src/logger/Logger.h"
|
||||||
#include "src/config/Config.h"
|
#include "src/config/Config.h"
|
||||||
#include "src/suite/Suite.h"
|
#include "src/suite/Suite.h"
|
||||||
#include "src/plan/Plan.h"
|
#include "src/plan/Plan.h"
|
||||||
|
#include "src/misc/helpers.h"
|
||||||
|
|
||||||
void version_info()
|
void version_info()
|
||||||
{
|
{
|
||||||
|
@ -162,6 +165,8 @@ int main( int argc, char * argv[] )
|
||||||
interpolate( config_path );
|
interpolate( config_path );
|
||||||
interpolate( plan_path );
|
interpolate( plan_path );
|
||||||
|
|
||||||
|
plan_path = get_absolute_path( plan_path );
|
||||||
|
|
||||||
// default logging level
|
// default logging level
|
||||||
int L_LEVEL = E_INFO;
|
int L_LEVEL = E_INFO;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"config": {
|
"config": {
|
||||||
"project_root": "/tmp/sample",
|
"project_root": "$HOME/development/internal/rex/sample",
|
||||||
"units_path": "units/",
|
"units_path": "units/",
|
||||||
"logs_path": "logs/",
|
"logs_path": "logs/",
|
||||||
"shells_path": "shells/shells.definitions",
|
"shells_path": "shells/shells.definitions",
|
||||||
|
|
|
@ -13,8 +13,8 @@
|
||||||
"active": true,
|
"active": true,
|
||||||
"required": true,
|
"required": true,
|
||||||
"set_user_context": true,
|
"set_user_context": true,
|
||||||
"user": "bagira",
|
"user": "$USER",
|
||||||
"group": "bagira",
|
"group": "$USER",
|
||||||
"supply_environment": true,
|
"supply_environment": true,
|
||||||
"environment": "environments/rex.variables"
|
"environment": "environments/rex.variables"
|
||||||
}
|
}
|
||||||
|
|
|
@ -157,29 +157,7 @@ std::string Conf::prepend_project_root( std::string relative_path)
|
||||||
return this->project_root + "/" + relative_path;
|
return this->project_root + "/" + relative_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Get the absolute path from a relative path
|
|
||||||
*
|
|
||||||
* This function takes a relative path and returns the corresponding absolute path.
|
|
||||||
* The absolute path is obtained by calling the `realpath` function.
|
|
||||||
* If the `realpath` function returns a null pointer, an error message is printed to the standard error stream and an empty string is returned.
|
|
||||||
*
|
|
||||||
* @param relative_path The relative path to be converted to an absolute path
|
|
||||||
*
|
|
||||||
* @return The absolute path corresponding to the relative path
|
|
||||||
*/
|
|
||||||
std::string get_absolute_path(const std::string &relative_path)
|
|
||||||
{
|
|
||||||
char resolved_path[1024];
|
|
||||||
memset(resolved_path, 0, sizeof(resolved_path));
|
|
||||||
|
|
||||||
if( realpath( relative_path.c_str(), resolved_path) == nullptr ) {
|
|
||||||
std::cerr << "Error resolving path: " << relative_path << std::endl;
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::string(resolved_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if a path exists
|
* @brief Check if a path exists
|
||||||
|
@ -306,15 +284,19 @@ Conf::Conf(std::string filename, int LOG_LEVEL ): JSON_Loader(LOG_LEVEL ), slog(
|
||||||
}
|
}
|
||||||
|
|
||||||
set_object_s( "project_root", this->project_root, filename );
|
set_object_s( "project_root", this->project_root, filename );
|
||||||
|
interpolate( project_root );
|
||||||
// convert to an absolute path after all the interpolation is done.
|
// convert to an absolute path after all the interpolation is done.
|
||||||
this->project_root = get_absolute_path( this->project_root );
|
this->project_root = get_absolute_path( this->project_root );
|
||||||
set_object_s( "logs_path", this->logs_path, filename );
|
|
||||||
this->logs_path = get_absolute_path( this->logs_path );
|
|
||||||
|
|
||||||
|
set_object_s( "logs_path", this->logs_path, filename );
|
||||||
|
interpolate( this->logs_path );
|
||||||
|
|
||||||
// all other paths are relative to project_root
|
// all other paths are relative to project_root
|
||||||
set_object_s_derivedpath( "units_path", this->units_path, filename );
|
set_object_s_derivedpath( "units_path", this->units_path, filename );
|
||||||
|
interpolate( this->units_path );
|
||||||
|
|
||||||
set_object_s_derivedpath( "shells_path", this->shell_definitions_path, filename );
|
set_object_s_derivedpath( "shells_path", this->shell_definitions_path, filename );
|
||||||
|
interpolate( this->shell_definitions_path );
|
||||||
|
|
||||||
// ensure these paths exists, with exception to the logs_path, which will be created at runtime
|
// ensure these paths exists, with exception to the logs_path, which will be created at runtime
|
||||||
this->slog.log_task( E_DEBUG, "SANITY_CHECKS", "Checking for sanity..." );
|
this->slog.log_task( E_DEBUG, "SANITY_CHECKS", "Checking for sanity..." );
|
||||||
|
|
|
@ -24,7 +24,6 @@
|
||||||
|
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstring>
|
|
||||||
#include "../json_support/JSON.h"
|
#include "../json_support/JSON.h"
|
||||||
#include "../logger/Logger.h"
|
#include "../logger/Logger.h"
|
||||||
#include "../misc/helpers.h"
|
#include "../misc/helpers.h"
|
||||||
|
|
|
@ -96,19 +96,44 @@ std::string get_8601()
|
||||||
* @brief Interpolates the environment variables in the input text
|
* @brief Interpolates the environment variables in the input text
|
||||||
*
|
*
|
||||||
* This function takes a string reference as input and replaces all occurrences of
|
* This function takes a string reference as input and replaces all occurrences of
|
||||||
* environment variables in the format `${VAR_NAME}` with their corresponding values.
|
* environment variables in the format `${VAR_NAME}` or `$VAR_NAME` with their corresponding values.
|
||||||
* If an environment variable is not set, it is replaced with an empty string.
|
* If an environment variable is not set, it is replaced with an empty string.
|
||||||
*
|
*
|
||||||
* @param text The input text to be processed
|
* @param text The input text to be processed
|
||||||
*/
|
*/
|
||||||
void interpolate( std::string & text )
|
void interpolate( std::string & text )
|
||||||
{
|
{
|
||||||
static std::regex env( "\\$\\{([^}]+)\\}" );
|
static std::regex env1( "\\$\\{([^}]+)\\}" );
|
||||||
|
static std::regex env2( "\\$([^/]+)" ); // matches $VAR_NAME until a / is found
|
||||||
std::smatch match;
|
std::smatch match;
|
||||||
while ( std::regex_search( text, match, env ) )
|
while ( std::regex_search( text, match, env1 ) || std::regex_search( text, match, env2 ) )
|
||||||
{
|
{
|
||||||
const char * s = getenv( match[1].str().c_str() );
|
const char * s = getenv( match[1].str().c_str() );
|
||||||
const std::string var( s == NULL ? "" : s );
|
const std::string var( s == NULL ? "" : s );
|
||||||
text.replace( match[0].first, match[0].second, var );
|
text.replace( match[0].first, match[0].second, var );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the absolute path from a relative path
|
||||||
|
*
|
||||||
|
* This function takes a relative path and returns the corresponding absolute path.
|
||||||
|
* The absolute path is obtained by calling the `realpath` function.
|
||||||
|
* If the `realpath` function returns a null pointer, an error message is printed to the standard error stream and an empty string is returned.
|
||||||
|
*
|
||||||
|
* @param relative_path The relative path to be converted to an absolute path
|
||||||
|
*
|
||||||
|
* @return The absolute path corresponding to the relative path
|
||||||
|
*/
|
||||||
|
std::string get_absolute_path(const std::string &relative_path)
|
||||||
|
{
|
||||||
|
char resolved_path[1024];
|
||||||
|
memset(resolved_path, 0, sizeof(resolved_path));
|
||||||
|
|
||||||
|
if( realpath( relative_path.c_str(), resolved_path) == nullptr ) {
|
||||||
|
std::cerr << "Error resolving path: " << relative_path << std::endl;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::string(resolved_path);
|
||||||
|
}
|
|
@ -23,6 +23,9 @@
|
||||||
#define REX_HELPERS_H
|
#define REX_HELPERS_H
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <cstring>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
@ -51,4 +54,17 @@ std::string get_8601();
|
||||||
|
|
||||||
const char * command2args( std::string input_string );
|
const char * command2args( std::string input_string );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the absolute path from a relative path
|
||||||
|
*
|
||||||
|
* This function takes a relative path and returns the corresponding absolute path.
|
||||||
|
* The absolute path is obtained by calling the `realpath` function.
|
||||||
|
* If the `realpath` function returns a null pointer, an error message is printed to the standard error stream and an empty string is returned.
|
||||||
|
*
|
||||||
|
* @param relative_path The relative path to be converted to an absolute path
|
||||||
|
*
|
||||||
|
* @return The absolute path corresponding to the relative path
|
||||||
|
*/
|
||||||
|
std::string get_absolute_path(const std::string &relative_path);
|
||||||
|
|
||||||
#endif //REX_HELPERS_H
|
#endif //REX_HELPERS_H
|
||||||
|
|
Loading…
Reference in New Issue