summaryrefslogtreecommitdiff
path: root/src/command.cpp
blob: 9443372cd7d916f37c3d326809c6939b03287ad1 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "command.h"
#include <cassert>
#include "config.h"
#include "sbo.h"
#include <ftxui/dom/elements.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include "component.h"

namespace katja
{
    void list::execute() const
    {
        sbo sbo_repository;
        std::unordered_map<std::string, package> packages = katja::read_package_database();
        std::vector<std::vector<std::string>> table_data;
        std::set<std::string> installed_packages;
        std::unordered_map<std::string, package> package_database = read_package_database();

        for (const auto& package : package_database)
        {
            std::string tag = package.second.tag();
            if (tag.find("_SBo") == std::string::npos)
            {
                continue;
            }
            installed_packages.insert(package.first);
        }
        for (const auto& package : sbo_repository.list(installed_packages))
        {
            if (package_database.find(package.first)->second.version() == package.second.version())
            {
                continue;
            }
            table_data.push_back({
                package.second.name(),
                package.second.version(),
                package.second.tag(),
                package.second.architecture(),
                "SBo"
            });
        }

        auto screen = ftxui::ScreenInteractive::Fullscreen();
        PackageList list_component{
            { "Package name", "Version", "Tag", "Architecture", "Repository" },
            table_data
        };

        screen.Loop(std::make_shared<PackageList>(list_component));
    }

    void help::execute() const
    {
        std::cout << "Usage:\n"
            "\tkatja {list|update|help} [OPTIONS]\n\n";
    }

    void update::execute() const
    {
        sbo().refresh();
    }

    command_exception::command_exception(const command_exception_t exception_type,
        std::vector<std::string> failed_arguments) noexcept
        : m_exception_type(exception_type), m_failed_arguments(failed_arguments)
    {
    }

    const char *command_exception::what() const noexcept
    {
        switch (m_exception_type)
        {
            case command_exception_t::no_command:
                return "No command specified.";
            case command_exception_t::too_many_arguments:
                return "Too many arguments given.";
            case command_exception_t::unknown_command:
                return "Unknown command.";
        }
        assert(false);
    }

    const std::vector<std::string>& command_exception::failed_arguments() const noexcept
    {
        return m_failed_arguments;
    }

    std::unique_ptr<command> parse_command_line(int argc, char **argv)
    {
        if (argc > 2)
        {
            std::vector<std::string> failed_arguments;

            for (std::size_t i = 2; i < argc; ++i)
            {
                failed_arguments.push_back(argv[i]);
            }
            throw command_exception(command_exception_t::unknown_command);
        }
        else if (argc < 2)
        {
            throw command_exception(command_exception_t::no_command);
        }
        if (strcmp(argv[1], "list") == 0)
        {
            return std::make_unique<list>();
        }
        else if (strcmp(argv[1], "update") == 0)
        {
            return std::make_unique<update>();
        }
        else if (strcmp(argv[1], "help") == 0)
        {
            return std::make_unique<help>();
        }
        throw command_exception(command_exception_t::unknown_command, { argv[1] });
    }
}