Skip to content
Snippets Groups Projects
push2git.cxx 3.96 KiB
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <string>
#include <ctime>
#include <fstream>
#include <dirent.h>
#include <unistd.h>

#ifndef PROJECT_NAME
	#define PROJECT_NAME "UNDEF_PROJECT"
#endif


//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 */


// using namespace std;

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;
}


std::string GetMachineID()
{
	std::string id;
	std::ifstream machineIdFile;
	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
	{
		id = "NULL";
		std::cout << BOLDYELLOW << "Machine ID was not identified, " << RESET << YELLOW << "value \"NULL\" will be used instead" << RESET<< std::endl;
	}
	return id;
}

void ShowError(std::string cmd)
{
  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];
	std::string oldname = filename;
	oldname.erase(filename.find(".json"),5);
	strftime(nf, 100, "_%d-%m-%Y_%Hh%Mm%Ss", curr_tm);
	std::string date = nf;
	std::string newfile = oldname + "_" + algorithm + "_" + date + "__" + GetMachineID() + ".json";
	//we must pull the submodule first to ensure all is up to date
	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");
		return;
	}
	sleep(1);

	//check if project folder exists
	std::string path = PROJECT_NAME;
	path = "./caenslowcontrol_json/" + path;
	std::string cmd = "mkdir " + path;
	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	
	path = PROJECT_NAME;
	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;
	}
}