Skip to content

Commit

Permalink
Merge pull request #571 from branupama/json_in_vpd_tool
Browse files Browse the repository at this point in the history
parse json file for vpd-tool
  • Loading branch information
SunnySrivastava1984 authored Jan 10, 2025
2 parents 4ce9305 + 315a1a4 commit e9cdb93
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
55 changes: 55 additions & 0 deletions vpd-tool/include/tool_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,5 +355,60 @@ inline types::BinaryVector convertToBinary(const std::string& i_value)
}
return l_binaryValue;
}

/**
* @brief API to parse respective JSON.
*
* @param[in] i_pathToJson - Path to JSON.
*
* @return Parsed JSON, throws exception in case of error.
*
* @throw std::runtime_error
*/
inline nlohmann::json getParsedJson(const std::string& i_pathToJson)
{
if (i_pathToJson.empty())
{
throw std::runtime_error("Path to JSON is missing");
}

std::error_code l_ec;
if (!std::filesystem::exists(i_pathToJson, l_ec))
{
std::string l_message{"file system call failed for file: " +
i_pathToJson};

if (l_ec)
{
l_message += ", error: " + l_ec.message();
}
throw std::runtime_error(l_message);
}

if (std::filesystem::is_empty(i_pathToJson, l_ec))
{
throw std::runtime_error("Empty file: " + i_pathToJson);
}
else if (l_ec)
{
throw std::runtime_error("is_empty file system call failed for file: " +
i_pathToJson + ", error: " + l_ec.message());
}

std::ifstream l_jsonFile(i_pathToJson);
if (!l_jsonFile)
{
throw std::runtime_error("Failed to access Json path: " + i_pathToJson);
}

try
{
return nlohmann::json::parse(l_jsonFile);
}
catch (const nlohmann::json::parse_error& l_ex)
{
throw std::runtime_error("Failed to parse JSON file: " + i_pathToJson);
}
}
} // namespace utils
} // namespace vpd
14 changes: 14 additions & 0 deletions vpd-tool/include/vpd_tool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ class VpdTool
*/
nlohmann::json getFruProperties(const std::string& i_fruPath) const;

/**
* @brief An API to get backup-restore config JSON of the system.
*
* API gets this file by prasing system config JSON file and reading
* backupRestoreConfigPath tag.
*
* @return On success returns valid JSON object, otherwise returns empty
* JSON object.
*
* Note: The caller of this API should verify, is received JSON object is
* empty or not.
*/
nlohmann::json getBackupRestoreCfgJsonObj() const noexcept;

public:
/**
* @brief Read keyword value.
Expand Down
2 changes: 1 addition & 1 deletion vpd-tool/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ sources = ['src/vpd_tool_main.cpp',

vpd_tool_exe = executable('vpd-tool',
sources,
include_directories : ['include/'],
include_directories : ['../', 'include/'],
dependencies: dependency_list,
install: true
)
30 changes: 30 additions & 0 deletions vpd-tool/src/vpd_tool.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "config.h"

#include "vpd_tool.hpp"

#include "tool_constants.hpp"
Expand Down Expand Up @@ -161,4 +163,32 @@ int VpdTool::writeKeyword(std::string i_vpdPath,
}
return l_rc;
}

nlohmann::json VpdTool::getBackupRestoreCfgJsonObj() const noexcept
{
nlohmann::json l_parsedBackupRestoreJson{};
try
{
nlohmann::json l_parsedSystemJson =
utils::getParsedJson(INVENTORY_JSON_SYM_LINK);

// check for mandatory fields at this point itself.
if (!l_parsedSystemJson.contains("backupRestoreConfigPath"))
{
throw std::runtime_error(
"backupRestoreConfigPath tag is missing from system config JSON : " +
std::string(INVENTORY_JSON_SYM_LINK));
}

l_parsedBackupRestoreJson =
utils::getParsedJson(l_parsedSystemJson["backupRestoreConfigPath"]);
}
catch (const std::exception& l_ex)
{
// TODO: Enable logging when verbose is enabled.
std::cerr << l_ex.what() << std::endl;
}

return l_parsedBackupRestoreJson;
}
} // namespace vpd

0 comments on commit e9cdb93

Please sign in to comment.