From ae0048ef3d03c1511f2e12e0fdb5ae8d28061dab Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sun, 2 Apr 2023 12:46:44 +0200 Subject: [PATCH] Add command to clone the source repository --- build.ninja | 4 ++-- src/command.cpp | 33 ++++++++++++++++++++++++++++++++- src/command.h | 25 +++++++++++++++++++++++++ src/config.h | 1 + 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 src/config.h diff --git a/build.ninja b/build.ninja index 0d2507b..81e0472 100644 --- a/build.ninja +++ b/build.ninja @@ -3,11 +3,11 @@ rule cxx description = CXX $out rule link - command = g++ -o $out $in + command = g++ -o $out $in -lboost_filesystem -lboost_system description = LINK $out build build/package.o: cxx src/package.cpp | src/package.h -build build/command.o: cxx src/command.cpp | src/command.h build/package.o +build build/command.o: cxx src/command.cpp | src/command.h build/package.o src/config.h build build/main.o: cxx src/main.cpp diff --git a/src/command.cpp b/src/command.cpp index 4066c23..0fa6bf1 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -1,5 +1,6 @@ #include "command.h" #include +#include "config.h" namespace katja { @@ -17,7 +18,33 @@ namespace katja void help::execute() const { std::cout << "Usage:\n" - "\tkatja {info|help} [OPTIONS]\n\n"; + "\tkatja {list|update|help} [OPTIONS]\n\n"; + } + + update::update() + : git_binary(boost::process::search_path("git")) + { + } + + void update::execute() const + { + std::filesystem::path workdir{ WORKDIR }; + std::filesystem::path repository = workdir / "sbo/repository"; + std::filesystem::file_status repository_status = std::filesystem::status(repository); + + if (std::filesystem::exists(repository_status) + && !std::filesystem::is_directory(repository_status)) + { + throw std::runtime_error("The working directory path \"" + + repository.string() + "\" exists, but it isn't a directory."); + } + else if (!std::filesystem::exists(repository_status)) + { + git("clone", std::filesystem::path(), + "git://git.slackbuilds.org/slackbuilds.git", repository.native()); + } + git("remote", repository.native(), "update", "--prune"); + git("reset", repository.native(), "--hard", "origin/master"); } command_exception::command_exception(const command_exception_t exception_type, @@ -65,6 +92,10 @@ namespace katja { return std::make_unique(); } + else if (strcmp(argv[1], "update") == 0) + { + return std::make_unique(); + } else if (strcmp(argv[1], "help") == 0) { return std::make_unique(); diff --git a/src/command.h b/src/command.h index 8d0679d..f03281f 100644 --- a/src/command.h +++ b/src/command.h @@ -3,6 +3,8 @@ #include #include #include "package.h" +#include +#include namespace katja { @@ -24,6 +26,29 @@ namespace katja void execute() const override; }; + class update final : public command + { + boost::filesystem::path git_binary; + + template + void git(const std::string& command, const std::filesystem::path& cwd, const Args&... args) const + { + if (cwd.empty()) + { + boost::process::system(git_binary, command, args...); + } + else + { + boost::process::system(git_binary, "-C", cwd.native(), command, args...); + } + } + + public: + explicit update(); + + void execute() const override; + }; + enum class command_exception_t { no_command, diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..c156081 --- /dev/null +++ b/src/config.h @@ -0,0 +1 @@ +#define WORKDIR "./var"