diff --git a/getpkg/src/DropshellScriptManager.cpp b/getpkg/src/DropshellScriptManager.cpp index 97a2d1a..897a81d 100644 --- a/getpkg/src/DropshellScriptManager.cpp +++ b/getpkg/src/DropshellScriptManager.cpp @@ -120,4 +120,39 @@ void DropshellScriptManager::addAutocomplete(const std::string& toolName) { std::ofstream outfile(dropshelltool::DROPSHELL_RC_PATH, std::ios::trunc); for (const auto& l : lines) outfile << l << "\n"; outfile.close(); -} \ No newline at end of file +} + +void DropshellScriptManager::removeAutocomplete(const std::string& toolName) { + ensureExists(); + std::ifstream infile(dropshelltool::DROPSHELL_RC_PATH); + std::vector lines; + std::string line; + + std::string blockStart = "# GETPKG-AUTOCOMPLETE-START: " + toolName; + std::string blockEnd = "# GETPKG-AUTOCOMPLETE-END: " + toolName; + bool inBlock = false; + + while (std::getline(infile, line)) { + if (trim(line) == blockStart) { + inBlock = true; + continue; + } + if (trim(line) == blockEnd) { + inBlock = false; + continue; + } + if (inBlock) { + continue; + } + // Also skip the complete line + if (line.find("# autocomplete: " + toolName) != std::string::npos) { + continue; + } + lines.push_back(line); + } + infile.close(); + + std::ofstream outfile(dropshelltool::DROPSHELL_RC_PATH, std::ios::trunc); + for (const auto& l : lines) outfile << l << "\n"; + outfile.close(); +} \ No newline at end of file diff --git a/getpkg/src/DropshellScriptManager.hpp b/getpkg/src/DropshellScriptManager.hpp index fa9ff79..396a95d 100644 --- a/getpkg/src/DropshellScriptManager.hpp +++ b/getpkg/src/DropshellScriptManager.hpp @@ -9,4 +9,5 @@ public: void addToolEntry(const std::string& toolName, const std::string& toolDir); void removeToolEntry(const std::string& toolName); void addAutocomplete(const std::string& toolName); + void removeAutocomplete(const std::string& toolName); }; \ No newline at end of file diff --git a/getpkg/src/main.cpp b/getpkg/src/main.cpp index 16abbf0..3e75cd2 100644 --- a/getpkg/src/main.cpp +++ b/getpkg/src/main.cpp @@ -370,6 +370,51 @@ int create_tool(int argc, char* argv[]) { return 0; } +int uninstall_tool(int argc, char* argv[]) { + if (argc < 3) { + std::cerr << "Usage: getpkg uninstall " << std::endl; + return 1; + } + std::string toolName = argv[2]; + + // Validate tool name + if (!isValidToolName(toolName)) { + std::cerr << "Invalid tool name. Tool names can only contain letters, numbers, dash, underscore, and dot." << std::endl; + return 1; + } + + std::string home = get_home(); + std::filesystem::path configDir = std::filesystem::path(home) / ".config/getpkg"; + std::filesystem::path binDir = std::filesystem::path(home) / ".local/bin/getpkg" / toolName; + std::filesystem::path toolInfoPath = configDir / (toolName + ".json"); + + // Check if tool is installed + if (!std::filesystem::exists(toolInfoPath)) { + std::cerr << "Tool " << toolName << " is not installed." << std::endl; + return 1; + } + + std::cout << "Uninstalling " << toolName << "..." << std::endl; + + // Remove tool entries from bashrc_getpkg + DropshellScriptManager scriptManager; + scriptManager.removeToolEntry(toolName); + scriptManager.removeAutocomplete(toolName); + + // Remove tool directory + if (std::filesystem::exists(binDir)) { + std::filesystem::remove_all(binDir); + } + + // Remove tool info file + if (std::filesystem::exists(toolInfoPath)) { + std::filesystem::remove(toolInfoPath); + } + + std::cout << "Uninstalled " << toolName << " successfully." << std::endl; + return 0; +} + } // end anonymous namespace int main(int argc, char* argv[]) { @@ -380,6 +425,8 @@ int main(int argc, char* argv[]) { std::string command = argv[1]; if (command == "install") { return install_tool(argc, argv); + } else if (command == "uninstall") { + return uninstall_tool(argc, argv); } else if (command == "publish") { return publish_tool(argc, argv); } else if (command == "update") { @@ -387,6 +434,7 @@ int main(int argc, char* argv[]) { } else if (command == "autocomplete") { std::vector args(argv + 2, argv + argc); if (args.empty()) std::cout << R"(install +uninstall publish update version @@ -402,6 +450,7 @@ help std::cout << "Commands:" << std::endl; std::cout << " Install/update the specified tool" << std::endl; std::cout << " install Install/update tool (legacy)" << std::endl; + std::cout << " uninstall Uninstall the specified tool" << std::endl; std::cout << " publish Publish a tool (ARCH optional)" << std::endl; std::cout << " update Update getpkg and all tools" << std::endl; std::cout << " version Show getpkg version" << std::endl; diff --git a/getpkg/test-uninstall/test-uninstall b/getpkg/test-uninstall/test-uninstall new file mode 100755 index 0000000..f48f6f1 --- /dev/null +++ b/getpkg/test-uninstall/test-uninstall @@ -0,0 +1,7 @@ +#\!/bin/bash +if [ "$1" = "version" ]; then + echo "1.0.0" +elif [ "$1" = "autocomplete" ]; then + echo "help" + echo "version" +fi diff --git a/getpkg/test-uninstall/test-uninstall-dir/test-uninstall b/getpkg/test-uninstall/test-uninstall-dir/test-uninstall new file mode 100755 index 0000000..f48f6f1 --- /dev/null +++ b/getpkg/test-uninstall/test-uninstall-dir/test-uninstall @@ -0,0 +1,7 @@ +#\!/bin/bash +if [ "$1" = "version" ]; then + echo "1.0.0" +elif [ "$1" = "autocomplete" ]; then + echo "help" + echo "version" +fi