From ba0c29f4a8ca57f7e6986b5ada1404e5e409034f Mon Sep 17 00:00:00 2001 From: COW_ILLUMINATI Date: Tue, 28 May 2024 11:40:44 -0400 Subject: [PATCH] First version --- .gitignore | 1 + Makefile | 11 ++++ README.md | 7 +-- src/cowtool.cpp | 142 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 src/cowtool.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2e04bce --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +cowtool diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2a747c9 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +all: + g++ src/cowtool.cpp -o ./cowtool + +install: + mv ./cowtool /sbin/cowtool + cp -r ./structure/* /etc/ + mkdir /sources/ + mkdir /sources/cowtool/ + mkdir /sources/cowtool/buildspace/ + + diff --git a/README.md b/README.md index 56216cc..dfbf094 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,8 @@ When installing a package, you are prompted to create a bash script from a templ Commands: - - cowtool lick < name > : Create/edit a package script + - cowtool lick < name > < editor > : Create/edit a package script - cowtool milk < name > : Install package - cowtool herd : List package scripts - - cowtool graze < name / empty for all > : Update packages, if empty do only autoupdate - - cowtool steak < name > : Uninstalls a package - - cowtool butcher < name > : Removes package script + - cowtool graze : Update packages, if empty do only autoupdate + - cowtool butcher < name > : Uninstalls a package diff --git a/src/cowtool.cpp b/src/cowtool.cpp new file mode 100644 index 0000000..90db16f --- /dev/null +++ b/src/cowtool.cpp @@ -0,0 +1,142 @@ +#include +#include +#include +#include +#include +#include +#include + +/* + * File structure: + * /etc/cowtool/ + * |- enabled + * |- disabled + * \- install_template.sh + * + * + * /sources/cowtool/buildspace/ + * + * lick will load a template via a text editor of choice. + * milk will run a script from Disabled, and if autoupdate, copy it to Enabled + * herd will list both Enabled and Disabled + * graze will run all the scripts in Enabled + * Butcher will run in uninstall version of the script and remove it from Enabled +*/ + +bool ask(std::string prompt) { + std::cout << prompt << " (Y/n) "; + std::string response; + std::cin >> response; + + if (response == "y" || response == "yes") { + return true; + } else if (response == "n" || response == "no") { + return false; + } else { + // default to yes + return true; + } +} + + + +int main(int argc, char* argv[]) { + + if( !getuid() ) { + std::cout << "Must be root!" << std::endl; + return 1; + } + + if ( argc == 1 ) { + std::cout << " Cowtool 0.0 :" << std::endl; + std::cout << " - cowtool lick < name > < editor > : Create/edit a package script" << std::endl; + std::cout << " - cowtool milk < name > : Install package" << std::endl; + std::cout << " - cowtool herd : List package scripts" << std::endl; + std::cout << " - cowtool graze < name / empty for all > : Update packages, if empty do only autoupdate" << std::endl; + std::cout << " - cowtool butcher < name > : Uninstalls a package" << std::endl; + return 0; + } + + std::string firstArg = argv[1]; + + if ( firstArg == "lick") { + + if (argc < 3) { + std::cout << "Package name required!" << std::endl; + return 1; + } + + std::string name = argv[2]; + std::string editor; + if (argc < 4) { + editor = "vim"; + } else { + editor = argv[3]; + } + + + if (!std::filesystem::exists("/etc/cowtool/disabled/install_"+name+".sh")) { + system(("cp -v /etc/cowtool/install_template.sh /etc/cowtool/disabled/install_"+name+".sh").c_str()); + system(("cp -v /etc/cowtool/disabled/install_"+name+".sh /etc/cowtool/disabled/remove_"+name+".sh").c_str()); + system(("echo #### ADD UNINSTALLATION #### >> /etc/cowtool/disabled/remove_"+name+".sh").c_str()); + } + + system((editor+"/etc/cowtool/disabled/install_"+name+".sh").c_str()); + system((editor+"/etc/cowtool/disabled/remove_"+name+".sh").c_str()); + std::cout << "Remember to `milk` to install/update `"+ name << "`!" << std::endl; + + } else if (firstArg == "milk") { + + if (argc < 3) { + std::cout << "Package name required!" << std::endl; + return 1; + } + + std::string name = argv[2]; + + if (ask("Should this package be updated when `graze` is run?")) { + system(("cp -v /etc/cowtool/disabled/install_" + name + ".sh /etc/cowtool/enabled/install_" + name + ".sh").c_str()); + } + + system(("bash /etc/cowtool/disabled/install_" + name + ".sh").c_str()); + + } else if (firstArg == "herd") { + + std::cout << "List of package scripts:" << std::endl << std::endl; + system("ls /etc/cowtool/disabled/install_"); + + std::cout << std::endl << "List of package scripts marked as autoupdate:" << std::endl << std::endl; + system("ls /etc/cowtool/enabled/install_"); + + } else if (firstArg == "graze") { + + // Loads directory + std::filesystem::path dir("/etc/cowtool/enabled"); + for (const auto& entry : std::filesystem::directory_iterator(dir)) { + if (entry.is_regular_file()) { + system(("bash /etc/cowtool/enabled/" + entry.path().filename().string()).c_str()); + } + } + + } else if (firstArg == "butcher") { + + if (argc < 3) { + std::cout << "Package name required!" << std::endl; + return 1; + } + + std::string name = argv[2]; + + system(("rm -f /etc/cowtool/enabled/install_"+name+".sh").c_str()); + system(("bash /etc/cowtool/disabled/remove_"+name+".sh").c_str()); + + } else { + + std::cout << "Unknown argument!" << std::endl; + std::cout << "Run without arguments for help menu." << std::endl; + return 1; + } + + return 0; + +}