This commit is contained in:
Ashley Rosch
2021-12-24 03:50:27 -06:00
parent dec55f18f6
commit 059f8f67de
8 changed files with 156 additions and 31 deletions

View File

@@ -1,13 +1,24 @@
#include <algorithm>
#include <cctype>
#include <fstream>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
static inline string trim(string &s) {
class DATA {
public:
string loc = "";
string script = "";
string name = "";
};
static inline void trim(string &s) {
s.erase(s.begin(), find_if(s.begin(), s.end(), [](unsigned char ch) {
return !isspace(ch);
}));
@@ -16,8 +27,48 @@ static inline string trim(string &s) {
}).base(), s.end());
}
void loadfile(vector<string[2]> &scripts) {
ifstream infile("bots");
string exec(const char* cmd) {
array<char, 128> buffer;
string result;
unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
trim(result);
return result;
}
const int DATALEN = 2;
void splitData(string str, DATA &data) {
stringstream ssin(str);
while (ssin.good()) {
if (data.loc.length() == 0) {
ssin >> data.loc;
continue;
}
if (data.script.length() > 0)
data.loc += " " + data.script;
ssin >> data.script;
}
if (data.loc[0] == '~')
data.loc = getenv("HOME") + data.loc.substr(1);
string cmd = "cd \"" + data.loc + "\" && cut -d \"=\" -f 2 <<< $(npm run env | grep npm_package_name)";
data.name = exec(cmd.c_str());
}
void loadfile(vector<DATA> &scripts) {
ifstream infile("scripts");
string line;
int linenum = 0;
while (getline(infile, line)) {
@@ -26,29 +77,17 @@ void loadfile(vector<string[2]> &scripts) {
string str = iss.str();
if (str.length() == 0 || str.substr(0,1) == "#")
break;
continue;
string loc = "";
string script = "start";
DATA data;
splitData(str, data);
size_t pos = 0;
string token;
while ((pos = str.find(":")) != string::npos) {
token = str.substr(0, pos);
trim(token);
if (loc.length() == 0)
loc = token;
else
script = token;
str.erase(0, pos + 1);
}
if (loc.length() == 0) {
if (data.loc.length() == 0) {
cout << "WARNING: skipping line " << linenum << endl;
break;
continue;
}
scripts.push_back({ loc, script });
scripts.push_back(data);
}
@@ -56,10 +95,14 @@ void loadfile(vector<string[2]> &scripts) {
int main() {
vector<string[2]> scripts;
vector<DATA> scripts;
loadfile(scripts);
for (int i = 0; i < scripts.size(); i++) {
DATA data = scripts.at(i);
}
return 0;
}