update: added error handling to executable
This commit is contained in:
parent
0f21c99d16
commit
f0ac23ac29
1 changed files with 73 additions and 13 deletions
74
src/main.cpp
74
src/main.cpp
|
@ -1,5 +1,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
@ -20,18 +22,28 @@ LISTING COMMANDS:
|
||||||
|
|
||||||
const string invalidvalue = R"#(Invalid value in config: )#";
|
const string invalidvalue = R"#(Invalid value in config: )#";
|
||||||
const string egc = R"#(Invalid command in config: )#";
|
const string egc = R"#(Invalid command in config: )#";
|
||||||
|
const string invalidconfig = R"#(Not a valid config: )#";
|
||||||
|
|
||||||
|
|
||||||
struct Action {
|
struct Action {
|
||||||
|
string rnames;
|
||||||
|
string rdescription;
|
||||||
|
string rcommand;
|
||||||
string names;
|
string names;
|
||||||
string description;
|
string description;
|
||||||
string command;
|
string command;
|
||||||
|
|
||||||
Action(
|
Action(
|
||||||
|
const string& rnms = "",
|
||||||
|
const string& rdesc = "",
|
||||||
|
const string& rcmd = "",
|
||||||
const string& nms = "",
|
const string& nms = "",
|
||||||
const string& desc = "",
|
const string& desc = "",
|
||||||
const string& cmd = "")
|
const string& cmd = "")
|
||||||
:
|
:
|
||||||
|
rnames(rnms),
|
||||||
|
rdescription(rdesc),
|
||||||
|
rcommand(rcmd),
|
||||||
names(nms),
|
names(nms),
|
||||||
description(desc),
|
description(desc),
|
||||||
command(cmd) {}
|
command(cmd) {}
|
||||||
|
@ -39,6 +51,9 @@ struct Action {
|
||||||
|
|
||||||
void from_toml(const table& t, Action& a) {
|
void from_toml(const table& t, Action& a) {
|
||||||
try {
|
try {
|
||||||
|
a.rnames = *t.get_as<string>("rnames");
|
||||||
|
a.rdescription = *t.get_as<string>("rdescription");
|
||||||
|
a.rcommand = *t.get_as<string>("rcommand");
|
||||||
a.names = *t.get_as<string>("names");
|
a.names = *t.get_as<string>("names");
|
||||||
a.description = *t.get_as<string>("description");
|
a.description = *t.get_as<string>("description");
|
||||||
a.command = *t.get_as<string>("command");
|
a.command = *t.get_as<string>("command");
|
||||||
|
@ -49,10 +64,9 @@ void from_toml(const table& t, Action& a) {
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
const char* configFile = nullptr;
|
const char* configFile = nullptr;
|
||||||
const char* configFile2 = nullptr;
|
|
||||||
|
|
||||||
const char* userHome = getenv("HOME");
|
const char* userHome = getenv("HOME");
|
||||||
string rapidMenuPath = string(userHome) + "/.config/RapidMenu";
|
string rapidMenuPath = string(userHome) + "/.config/RapidMenu";
|
||||||
|
string rapidcommand = "mkdir -p " + rapidMenuPath;
|
||||||
|
|
||||||
if (argc > 1 && strcmp(argv[1], "-c") == 0) {
|
if (argc > 1 && strcmp(argv[1], "-c") == 0) {
|
||||||
if (argc < 3 || argv[2][0] == '-') {
|
if (argc < 3 || argv[2][0] == '-') {
|
||||||
|
@ -61,13 +75,9 @@ int main(int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
configFile = argv[2];
|
configFile = argv[2];
|
||||||
|
|
||||||
if (configFile) {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (filesystem::exists(rapidMenuPath) && filesystem::is_directory(rapidMenuPath)) {
|
if (filesystem::exists(rapidMenuPath) && filesystem::is_directory(rapidMenuPath)) {
|
||||||
} else {
|
} else {
|
||||||
system("mkdir -p /home/$USER/.config/RapidMenu");
|
system(rapidcommand.c_str());
|
||||||
cerr << "Setting up config." << endl;
|
cerr << "Setting up config." << endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -155,15 +165,65 @@ int main(int argc, char* argv[]) {
|
||||||
|
|
||||||
const char* bconfig = nullptr;
|
const char* bconfig = nullptr;
|
||||||
const char* bexe = nullptr;
|
const char* bexe = nullptr;
|
||||||
|
const char* userHome = getenv("HOME");
|
||||||
|
|
||||||
string bexeout;
|
string bexeout;
|
||||||
|
string byn;
|
||||||
|
string bconfigin = argv[2];
|
||||||
|
string bindir = string(userHome) + "/.local/bin";
|
||||||
|
string createbindir = "mkdir -p " + bindir;
|
||||||
|
|
||||||
|
if (filesystem::exists(bindir) && filesystem::is_directory(bindir)) {
|
||||||
|
} else {
|
||||||
|
system(createbindir.c_str());
|
||||||
|
cerr << "Setting up bin dir." << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// config
|
||||||
|
string bconfigfile = bconfigin;
|
||||||
|
|
||||||
|
if (filesystem::exists(bconfigfile) && filesystem::is_regular_file(bconfigfile)) {
|
||||||
bconfig = argv[2];
|
bconfig = argv[2];
|
||||||
|
|
||||||
|
} else {
|
||||||
|
while (!(filesystem::exists(bconfigfile) && filesystem::is_regular_file(bconfigfile))) {
|
||||||
|
cout << "Invalid config file: " << bconfigfile << endl;
|
||||||
|
cout << "Please enter a valid config: ";
|
||||||
|
cin >> bconfigfile;
|
||||||
|
|
||||||
|
// No need to redeclare bconfigfile here; it will update the outer variable
|
||||||
|
bconfig = bconfigfile.c_str();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// executable
|
||||||
cout << "What do you want to call your executable?: ";
|
cout << "What do you want to call your executable?: ";
|
||||||
cin >> bexeout;
|
cin >> bexeout;
|
||||||
|
string bexefile = string(userHome) + "/.local/bin/" + bexeout;
|
||||||
|
|
||||||
|
if (filesystem::exists(bexefile) && filesystem::is_regular_file(bexefile)) {
|
||||||
|
|
||||||
|
while (filesystem::exists(bexefile) && filesystem::is_regular_file(bexefile)) {
|
||||||
|
cout << "do you want to overwrite: " << bexeout << "? (y/n) ";
|
||||||
|
cin >> byn;
|
||||||
|
|
||||||
|
transform(byn.begin(), byn.end(), byn.begin(), ::tolower);
|
||||||
|
|
||||||
|
if (byn == "y") {
|
||||||
bexe = bexeout.c_str();
|
bexe = bexeout.c_str();
|
||||||
|
break;
|
||||||
|
} else if (byn == "n") {
|
||||||
|
cout << "What do you want to call your executable?: ";
|
||||||
|
cin >> bexeout;
|
||||||
|
bexe = bexeout.c_str();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
bexe = bexeout.c_str();
|
||||||
|
}
|
||||||
|
|
||||||
system(("touch /home/$USER/.local/bin/" + string(bexe)).c_str());
|
system(("touch /home/$USER/.local/bin/" + string(bexe)).c_str());
|
||||||
system(("chmod +x /home/$USER/.local/bin/" + string(bexe)).c_str());
|
system(("chmod +x /home/$USER/.local/bin/" + string(bexe)).c_str());
|
||||||
|
|
Loading…
Add table
Reference in a new issue