Skip to content

Commit

Permalink
Reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
wichtounet committed Oct 11, 2023
1 parent 806e752 commit 7183bce
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 61 deletions.
2 changes: 2 additions & 0 deletions include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ std::string base64_encode(std::string_view in);
std::string html_base64_decode(std::string_view in);
std::string html_base64_encode(std::string_view in);

std::string exec_command(const std::string& command);

} //end of namespace budget
18 changes: 1 addition & 17 deletions src/budget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,8 @@ struct aliases_collector {
}
};

std::string exec_command(const std::string& command) {
std::stringstream output;

char buffer[1024];

FILE* stream = popen(command.c_str(), "r");

while (fgets(buffer, 1024, stream) != nullptr) {
output << buffer;
}

pclose(stream);

return output.str();
}

bool has_enough_colors(){
auto colors = exec_command("tput colors");
auto colors = budget::exec_command("tput colors");
colors = colors.substr(0, colors.length() - 1);

return to_number<int>(colors) > 4;
Expand Down
18 changes: 1 addition & 17 deletions src/share.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,30 +96,14 @@ share_cache_value get_invalid_value(const share_price_cache_key & key) {
return {budget::money(1), false};
}

std::string exec_command(const std::string& command) {
std::stringstream output;

char buffer[1024];

FILE* stream = popen(command.c_str(), "r");

while (fgets(buffer, 1024, stream) != nullptr) {
output << buffer;
}

pclose(stream);

return output.str();
}

// V3 is using Yahoo Finance
// Starting from this version, the get_share_price function must be thread
// safe. This means, it cannot touch the cache itself
std::map<share_price_cache_key, budget::money, std::less<>> get_share_price_v3(const std::string & ticker, budget::date start_date, budget::date end_date) {
std::string const command =
"yfinance_quote.py " + ticker + " " + date_to_string(start_date) + " " + date_to_string(end_date);

auto result = exec_command(command);
auto result = budget::exec_command(command);

if (result.empty()) {
LOG_F(ERROR, "Price(v3): yfinance_quote.py returned nothing");
Expand Down
16 changes: 16 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@
#include "expenses.hpp"
#include "earnings.hpp"

std::string budget::exec_command(const std::string& command) {
std::stringstream output;

std::array<char, 1024> buffer{};

FILE* stream = popen(command.c_str(), "r");

while (fgets(buffer.data(), 1024, stream) != nullptr) {
output << buffer.data();
}

pclose(stream);

return output.str();
}

unsigned short budget::terminal_width(){
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO csbi;
Expand Down
35 changes: 8 additions & 27 deletions src/versioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,7 @@
#include "versioning.hpp"
#include "budget_exception.hpp"
#include "config.hpp"

namespace {

std::string exec_command(const std::string& command) {
std::stringstream output;

char buffer[1024];

FILE* stream = popen(command.c_str(), "r");

while (fgets(buffer, 1024, stream) != nullptr) {
output << buffer;
}

pclose(stream);

return output.str();
}

} //end of anonymous namespace
#include "utils.hpp"

void budget::versioning_module::handle(const std::vector<std::string>& args){
// versioning does not make sense in server mode
Expand All @@ -46,17 +27,17 @@ void budget::versioning_module::handle(const std::vector<std::string>& args){
const auto& subcommand = args[1];

if(subcommand == "save"){
std::cout << exec_command("git -C " + budget_folder().string() + " commit -a -m Update" ) << std::endl;
std::cout << budget::exec_command("git -C " + budget_folder().string() + " commit -a -m Update" ) << std::endl;
} else if(subcommand == "sync"){
std::cout << exec_command("git -C " + budget_folder().string() + " commit -a -m Update" ) << std::endl;
std::cout << exec_command("git -C " + budget_folder().string() + " pull" ) << std::endl;
std::cout << exec_command("git -C " + budget_folder().string() + " push" ) << std::endl;
std::cout << budget::exec_command("git -C " + budget_folder().string() + " commit -a -m Update" ) << std::endl;
std::cout << budget::exec_command("git -C " + budget_folder().string() + " pull" ) << std::endl;
std::cout << budget::exec_command("git -C " + budget_folder().string() + " push" ) << std::endl;
} else if(subcommand == "pull"){
std::cout << exec_command("git -C " + budget_folder().string() + " pull" ) << std::endl;
std::cout << budget::exec_command("git -C " + budget_folder().string() + " pull" ) << std::endl;
} else if(subcommand == "push"){
std::cout << exec_command("git -C " + budget_folder().string() + " push" ) << std::endl;
std::cout << budget::exec_command("git -C " + budget_folder().string() + " push" ) << std::endl;
} else if(subcommand == "status"){
std::cout << exec_command("git -C " + budget_folder().string() + " status" ) << std::endl;
std::cout << budget::exec_command("git -C " + budget_folder().string() + " status" ) << std::endl;
} else {
throw budget_exception("Invalid subcommand \"" + subcommand + "\"");
}
Expand Down

0 comments on commit 7183bce

Please sign in to comment.