summaryrefslogtreecommitdiff
path: root/src/command.h
blob: f03281f0835004f5a559b276b7479f2744350a42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cstring>
#include <memory>
#include <vector>
#include "package.h"
#include <boost/process.hpp>
#include <filesystem>

namespace katja
{
    class command
    {
    public:
        virtual void execute() const = 0;
    };

    class list final : public command
    {
    public:
        void execute() const override;
    };

    class help final : public command
    {
    public:
        void execute() const override;
    };

    class update final : public command
    {
        boost::filesystem::path git_binary;

        template<typename... Args>
        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,
        too_many_arguments,
        unknown_command,
    };

    class command_exception final : public std::exception
    {
        command_exception_t m_exception_type;
        std::vector<std::string> m_failed_arguments;

    public:
        explicit command_exception(const command_exception_t exception_type,
            std::vector<std::string> failed_arguments = {}) noexcept;

        const char *what() const noexcept override;
        const std::vector<std::string>& failed_arguments() const noexcept;
    };

    std::unique_ptr<command> parse_command_line(int argc, char **argv);
}