Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take input from file for write keyword #598

Open
wants to merge 1 commit into
base: 1110
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions vpd-tool/include/tool_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,5 +723,75 @@ class Table
}
};

/**
* @brief API to read value from file.
*
* The API reads the file and returns the read value.
*
* @param[in] i_filePath - File path.
*
* @return - Value read.
*
* @throw std::runtime_error
*/
inline std::string readValueFromFile(const std::string& i_filePath)
{
std::error_code l_ec;
if (!std::filesystem::exists(i_filePath, l_ec))
{
std::string l_message{"filesystem call exists failed for file: " +
i_filePath};

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

throw std::runtime_error(l_message);
}

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

std::ifstream l_fileStream;
l_fileStream.exceptions(std::ifstream::badbit | std::ifstream::failbit);
try
{
l_fileStream.open(i_filePath, std::ifstream::in);

if (l_fileStream.is_open())
{
std::string l_valueRead;

std::getline(l_fileStream, l_valueRead);

l_fileStream.close();
return l_valueRead;
}
else
{
throw std::runtime_error("Error while opening the file " +
i_filePath);
}
}
catch (const std::ios_base::failure& l_ex)
{
if (l_fileStream.is_open())
{
l_fileStream.close();
}

throw std::runtime_error("Failed to read to file: " + i_filePath +
", error: " + l_ex.what());
}
}

} // namespace utils
} // namespace vpd
64 changes: 42 additions & 22 deletions vpd-tool/src/vpd_tool_main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "tool_constants.hpp"
#include "tool_utils.hpp"
#include "vpd_tool.hpp"

#include <CLI/CLI.hpp>
Expand Down Expand Up @@ -43,51 +44,70 @@ int doMfgClean(const auto& i_mfgCleanConfirmFlag)
* @param[in] i_recordName - Record to be updated.
* @param[in] i_keywordName - Keyword to be updated.
* @param[in] i_keywordValue - Value to be updated in keyword.
* @param[in] i_fileOption - Option to take keyword's value from file.
* @param[in] i_filePath - File path to take keyword's value.
*
* @return Status of writeKeyword operation, failure otherwise.
*/
int writeKeyword(const auto& i_hardwareFlag, const auto& i_keywordValueOption,
const std::string& i_vpdPath, const std::string& i_recordName,
const std::string& i_keywordName,
const std::string& i_keywordValue)
const std::string& i_keywordValue, const auto& i_fileOption,
const std::string& i_filePath)
{
int l_rc = vpd::constants::FAILURE;
std::error_code l_ec;

if (!i_hardwareFlag->empty() && !std::filesystem::exists(i_vpdPath, l_ec))
{
std::cerr << "Given EEPROM file path doesn't exist : " + i_vpdPath
<< std::endl;
return vpd::constants::FAILURE;
}

if (l_ec)
{
std::cerr << "filesystem call exists failed for file: " << i_vpdPath
<< ", reason: " + l_ec.message() << std::endl;
return vpd::constants::FAILURE;
std::string l_message{"Given EEPROM file path doesn't exist : " +
i_vpdPath};
if (l_ec)
{
l_message = "filesystem call exists failed for file: " + i_vpdPath +
", error: " + l_ec.message();
}
std::cerr << l_message << std::endl;
return l_rc;
}

if (!i_keywordValueOption->empty() && i_keywordValue.empty())
if (!i_keywordValueOption->empty() && !i_fileOption->empty())
{
std::cerr
<< "Please provide keyword value.\nUse --value/--file to give "
<< "Please provide keyword value.\nUse either --value or --file to give "
"keyword value. Refer --help."
<< std::endl;
return vpd::constants::FAILURE;
return l_rc;
}

if (i_keywordValueOption->empty())
else if ((i_keywordValueOption->empty() && i_fileOption->empty()) ||
(!i_keywordValueOption->empty() && i_keywordValue.empty()) ||
(!i_fileOption->empty() && i_filePath.empty()))
{
std::cerr
<< "Please provide keyword value.\nUse --value/--file to give "
"keyword value. Refer --help."
<< std::endl;
return vpd::constants::FAILURE;
return l_rc;
}

std::string l_keywordValue = i_keywordValue;

if (!i_filePath.empty())
{
try
{
l_keywordValue = vpd::utils::readValueFromFile(i_filePath);
}
catch (const std::exception& l_ex)
{
std::cerr << l_ex.what() << std::endl;
return l_rc;
}
}

vpd::VpdTool l_vpdToolObj;
return l_vpdToolObj.writeKeyword(i_vpdPath, i_recordName, i_keywordName,
i_keywordValue, !i_hardwareFlag->empty());
l_keywordValue, !i_hardwareFlag->empty());
}

/**
Expand Down Expand Up @@ -236,9 +256,8 @@ int main(int argc, char** argv)
auto l_keywordOption = l_app.add_option("--keyword, -K", l_keywordName,
"Keyword name");

// Enable when file option is implemented.
/*auto l_fileOption = l_app.add_option("--file", l_filePath,
"Absolute file path");*/
auto l_fileOption = l_app.add_option("--file", l_filePath,
"Absolute file path");

auto l_keywordValueOption =
l_app.add_option("--value, -V", l_keywordValue,
Expand Down Expand Up @@ -304,7 +323,8 @@ int main(int argc, char** argv)
if (!l_writeFlag->empty())
{
return writeKeyword(l_hardwareFlag, l_keywordValueOption, l_vpdPath,
l_recordName, l_keywordName, l_keywordValue);
l_recordName, l_keywordName, l_keywordValue,
l_fileOption, l_filePath);
}

if (!l_dumpObjFlag->empty())
Expand Down