~nabijaczleweli/klapki.deb

ref: 50de836242adb578bc1bb6c288ddc450c4a4765c klapki.deb/src/ops.hpp -rw-r--r-- 5.0 KiB
50de8362наб Fix upstream homepage link 1 year, 2 months ago
                                                                                
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// The MIT License (MIT)

// Copyright (c) 2020 наб <nabijaczleweli@nabijaczleweli.xyz>

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


#pragma once


#include "state.hpp"
#include "util.hpp"
#include <cstdint>
#include <fmt/format.h>
#include <optional>
#include <string>
#include <variant>
#include <vector>

namespace klapki {
	struct config;
}
namespace klapki::context {
	struct context;
}


namespace klapki::ops {
	struct dump {};
	struct bootpos {
		std::uint16_t position;
	};
	struct addkernel {
		const char * version;
		const char * image;
		std::vector<const char *> initrds;
	};
	struct delkernel {
		const char * version;
	};
	struct addvariant {
		const char * variant;
	};
	struct delvariant {
		const char * variant;
	};

	using op_t = std::variant<dump, bootpos, addkernel, delkernel, addvariant, delvariant>;

	std::optional<std::string> execute(const dump & d, const config & cfg, state::state & state, context::context & context);
	std::optional<std::string> execute(const bootpos & bp, const config & cfg, state::state & state, context::context & context);
	std::optional<std::string> execute(const addkernel & ak, const config & cfg, state::state & state, context::context & context);
	std::optional<std::string> execute(const delkernel & dk, const config & cfg, state::state & state, context::context & context);
	std::optional<std::string> execute(const addvariant & av, const config & cfg, state::state & state, context::context & context);
	std::optional<std::string> execute(const delvariant & dv, const config & cfg, state::state & state, context::context & context);
}


namespace klapki {
	namespace detail {
		struct initrds_fmt {
			const std::vector<const char *> & initrds;
		};
	}

	struct op {
		const ops::op_t & op;

		static std::variant<ops::op_t, std::string> from_cmdline(const char * argv0, const char **& argv);
		static std::optional<std::string> execute(const ops::op_t & op, const config & cfg, state::state & state, context::context & context);
	};
	struct mops {
		const std::vector<ops::op_t> & ops;
	};
}


template <>
struct fmt::formatter<klapki::op> {
	constexpr auto parse(format_parse_context & ctx) { return ctx.begin(); }

	template <typename FormatContext>
	auto format(const klapki::op & op, FormatContext & ctx) {
		return std::visit(klapki::overload{
		                      [&](const klapki::ops::dump &) { return format_to(ctx.out(), "{{dump}}"); },
		                      [&](const klapki::ops::bootpos & bp) { return format_to(ctx.out(), "{{bootpos {}}}", bp.position); },
		                      [&](const klapki::ops::addkernel & ak) {
			                      return format_to(ctx.out(), "{{addkernel {} {} {}}}", ak.version, ak.image, klapki::detail::initrds_fmt{ak.initrds});
		                      },
		                      [&](const klapki::ops::delkernel & dk) { return format_to(ctx.out(), "{{delkernel {}}}", dk.version); },
		                      [&](const klapki::ops::addvariant & av) { return format_to(ctx.out(), "{{addvariant {}}}", av.variant); },
		                      [&](const klapki::ops::delvariant & dv) { return format_to(ctx.out(), "{{delvariant {}}}", dv.variant); },
		                  },
		                  op.op);
	}
};

template <>
struct fmt::formatter<klapki::mops> {
	constexpr auto parse(format_parse_context & ctx) { return ctx.begin(); }

	template <typename FormatContext>
	auto format(const klapki::mops & ops, FormatContext & ctx) {
		auto out = ctx.out();

		bool first = true;
		for(auto && el : ops.ops) {
			if(!first)
				out = format_to(out, ", ");
			else
				first = false;

			out = format_to(out, "{}", klapki::op{el});
		}

		return out;
	}
};

template <>
struct fmt::formatter<klapki::detail::initrds_fmt> {
	constexpr auto parse(format_parse_context & ctx) { return ctx.begin(); }

	template <typename FormatContext>
	auto format(const klapki::detail::initrds_fmt & ints, FormatContext & ctx) {
		auto out = ctx.out();

		for(auto && el : ints.initrds)
			out = format_to(out, "{} ", el);
		out = format_to(out, "\"\"");

		return out;
	}
};