Newer
Older
#include <stdio.h>
#include <string>
#include <ctime>
#include <fstream>
#include <dirent.h>
#include <unistd.h>
#ifndef PROJECT_NAME
#define PROJECT_NAME "UNDEF_PROJECT"
//terminal colors
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
bool DirectoryExists( const char* pzPath )
{
if ( pzPath == NULL) return false;
DIR *pDir;
bool bExists = false;
pDir = opendir (pzPath);
if (pDir != NULL)
{
bExists = true;
(void) closedir (pDir);
}
return bExists;
}
machineIdFile.open("/etc/machine-id");
if(machineIdFile.good())
{
getline(machineIdFile, id);
std::cout << BOLDGREEN << "This machine ID is: " << RESET << GREEN << id << RESET << std::endl;
}
else
std::cout << BOLDYELLOW << "Machine ID was not identified, " << RESET << YELLOW << "value \"NULL\" will be used instead" << RESET<< std::endl;
{
std::cout << BOLDRED << " --- ERROR: " << RESET << RED << "Command \"" << cmd << "\" returned bad value" << RESET << std::endl;
std::cout << BOLDRED << "Backing data to git failed" << RESET << std::endl;
std::cout << BOLDYELLOW << "Error details are in gitlog.txt:" << RESET << std::endl;
system("cat gitlog.txt");
}
void processGit(std::string filename,std::string algorithm)
{
//get time for backed-up file name
time_t curr_time;
tm * curr_tm;
time(&curr_time);
curr_tm = localtime(&curr_time);
char nf[100];
strftime(nf, 100, "_%d-%m-%Y_%Hh%Mm%Ss", curr_tm);
std::string date = nf;
std::string newfile = oldname + "_" + algorithm + "_" + date + "__" + GetMachineID() + ".json";
if (system("git --git-dir=./caenslowcontrol_json/.git pull https://autopush_token:N4XGrjZrQXTFikvtZ3Co@baltig.infn.it/gamma/caenslowcontrol_json.git > gitlog.txt") != 0)
ShowError("git --git-dir=./caenslowcontrol_json/.git pull > gitlog.txt");
if(!DirectoryExists(path.c_str())) system(cmd.c_str());
//copy to project folder
cmd = "cp " + filename + " " + path + "/" + newfile;
if (system(cmd.c_str()) != 0)
{
ShowError(cmd);
return;
}
//git add
cmd = "git --git-dir=./caenslowcontrol_json/.git add " + path + "/" + newfile + " > gitlog.txt";
if (system(cmd.c_str()) != 0)
{
ShowError(cmd);
return;
}
//git commit
std::string commit = "\"autopush of " + newfile + "\"";
cmd = "git --git-dir=./caenslowcontrol_json/.git commit -m " + commit + " > gitlog.txt";
if (system(cmd.c_str()) != 0)
{
ShowError(cmd);
return;
}
//git push
cmd = "git --git-dir=./caenslowcontrol_json/.git push https://autopush_token:N4XGrjZrQXTFikvtZ3Co@baltig.infn.it/gamma/caenslowcontrol_json.git main > gitlog.txt";
if (system(cmd.c_str()) != 0)
{
ShowError(cmd);
return;
}