blob: 5f8f0526f8e68006cdf200bc3e4173fde6152fd9 [file] [log] [blame]
Václav Kubernát24df80e2018-06-06 15:18:03 +02001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
3 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
4 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
8#pragma once
9
Václav Kubernát57272422019-02-08 12:48:24 +010010#include <boost/mpl/vector.hpp>
Václav Kubernát509ce652019-05-29 19:46:44 +020011#include <boost/spirit/home/x3/support/ast/position_tagged.hpp>
Václav Kubernát24df80e2018-06-06 15:18:03 +020012#include "ast_path.hpp"
Václav Kubernát627f6152018-08-29 13:23:56 +020013#include "ast_values.hpp"
Václav Kubernát24df80e2018-06-06 15:18:03 +020014
15namespace x3 = boost::spirit::x3;
Václav Kubernát24df80e2018-06-06 15:18:03 +020016
17using keyValue_ = std::pair<std::string, std::string>;
18
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020019enum class LsOption {
20 Recursive
21};
22
Václav Kubernát6d791432018-10-25 16:00:35 +020023struct discard_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010024 static constexpr auto name = "discard";
Václav Kubernát054cc992019-02-21 14:23:52 +010025 static constexpr auto shortHelp = "discard - Discard current changes.";
26 static constexpr auto longHelp = R"(
27 discard
28
29 Discards current changes. Accepts no arguments.
30
31 Usage:
32 /> discard)";
Václav Kubernát6d791432018-10-25 16:00:35 +020033 bool operator==(const discard_& b) const;
34};
35
Václav Kubernát11afac72018-07-18 14:59:53 +020036struct ls_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010037 static constexpr auto name = "ls";
Václav Kubernát054cc992019-02-21 14:23:52 +010038 static constexpr auto shortHelp = "ls - List available nodes.";
39 static constexpr auto longHelp = R"(
40 ls [--recursive] [path]
41
42 Lists available nodes in the current directory. Optionally
43 accepts a path argument. Accepts both schema paths and data
44 paths. Path starting with a forward slash means an absolute
45 path.
46
47 Usage:
48 /> ls
49 /> ls --recursive module:node
50 /> ls /module:node)";
Václav Kubernát11afac72018-07-18 14:59:53 +020051 bool operator==(const ls_& b) const;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020052 std::vector<LsOption> m_options;
Václav Kubernát5c75b252018-10-10 18:33:47 +020053 boost::optional<boost::variant<dataPath_, schemaPath_>> m_path;
Václav Kubernát11afac72018-07-18 14:59:53 +020054};
55
Václav Kubernát24df80e2018-06-06 15:18:03 +020056struct cd_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010057 static constexpr auto name = "cd";
Václav Kubernát054cc992019-02-21 14:23:52 +010058 static constexpr auto shortHelp = "cd - Enter a different node.";
59 static constexpr auto longHelp = R"(
60 cd path
61
62 Enters a node specified by path. Only accepts data paths.
63
64 Usage:
65 /> cd /module:node/node2
66 /> cd ..)";
Václav Kubernát24df80e2018-06-06 15:18:03 +020067 bool operator==(const cd_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020068 dataPath_ m_path;
Václav Kubernát24df80e2018-06-06 15:18:03 +020069};
70
71struct create_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010072 static constexpr auto name = "create";
Václav Kubernát054cc992019-02-21 14:23:52 +010073 static constexpr auto shortHelp = "create - Create a presence container.";
74 static constexpr auto longHelp = R"(
Václav Kubernátf5f64f02019-03-19 17:15:47 +010075 create path
Václav Kubernát054cc992019-02-21 14:23:52 +010076
Václav Kubernátf5f64f02019-03-19 17:15:47 +010077 Creates a presence container or a list instance specified by path.
Václav Kubernát054cc992019-02-21 14:23:52 +010078
79 Usage:
Václav Kubernátf5f64f02019-03-19 17:15:47 +010080 /> create /module:pContainer
81 /> create /module:list[key=value][anotherKey=value])";
Václav Kubernát24df80e2018-06-06 15:18:03 +020082 bool operator==(const create_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020083 dataPath_ m_path;
Václav Kubernát24df80e2018-06-06 15:18:03 +020084};
85
86struct delete_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010087 static constexpr auto name = "delete";
Václav Kubernát054cc992019-02-21 14:23:52 +010088 static constexpr auto shortHelp = "delete - Delete a presence container.";
89 static constexpr auto longHelp = R"(
Václav Kubernátf5f64f02019-03-19 17:15:47 +010090 delete path
Václav Kubernát054cc992019-02-21 14:23:52 +010091
Václav Kubernátf5f64f02019-03-19 17:15:47 +010092 Deletes a presence container or a list instance specified by path.
Václav Kubernát054cc992019-02-21 14:23:52 +010093
94 Usage:
Václav Kubernátf5f64f02019-03-19 17:15:47 +010095 /> delete /module:pContainer
96 /> delete /module:list[key=value][anotherKey=value])";
Václav Kubernát24df80e2018-06-06 15:18:03 +020097 bool operator==(const delete_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020098 dataPath_ m_path;
Václav Kubernát24df80e2018-06-06 15:18:03 +020099};
100
101struct set_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +0100102 static constexpr auto name = "set";
Václav Kubernát054cc992019-02-21 14:23:52 +0100103 static constexpr auto shortHelp = "set - Change value of a leaf.";
104 static constexpr auto longHelp = R"(
105 set path_to_leaf value
106
107 Changes the leaf specified by path to value.
108
109 Usage:
110 /> set /module:leaf 123
111 /> set /module:leaf abc)";
Václav Kubernát24df80e2018-06-06 15:18:03 +0200112 bool operator==(const set_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200113 dataPath_ m_path;
Václav Kubernátebca2552018-06-08 19:06:02 +0200114 leaf_data_ m_data;
Václav Kubernát24df80e2018-06-06 15:18:03 +0200115};
116
Václav Kubernát812ee282018-08-30 17:10:03 +0200117struct commit_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +0100118 static constexpr auto name = "commit";
Václav Kubernát054cc992019-02-21 14:23:52 +0100119 static constexpr auto shortHelp = "commit - Commit current changes.";
120 static constexpr auto longHelp = R"(
121 commit
122
123 Commits the current changes. Accepts no arguments.
124
125 Usage:
126 /> commit)";
Václav Kubernát812ee282018-08-30 17:10:03 +0200127 bool operator==(const set_& b) const;
128};
129
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200130struct get_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +0100131 static constexpr auto name = "get";
Václav Kubernát054cc992019-02-21 14:23:52 +0100132 static constexpr auto shortHelp = "get - Retrieve configuration from the server.";
133 static constexpr auto longHelp = R"(
134 get [path]
135
136 Retrieves configuration of the current node. Works recursively.
137 Optionally takes an argument specifying a path, the output will,
138 as if the user was in that node.
139
140 Usage:
141 /> get
142 /> get /module:path)";
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200143 bool operator==(const get_& b) const;
Václav Kubernát5c75b252018-10-10 18:33:47 +0200144 boost::optional<boost::variant<dataPath_, schemaPath_>> m_path;
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200145};
Václav Kubernát812ee282018-08-30 17:10:03 +0200146
Václav Kubernát054cc992019-02-21 14:23:52 +0100147struct help_;
148using CommandTypes = boost::mpl::vector<discard_, ls_, cd_, create_, delete_, set_, commit_, get_, help_>;
149struct help_ : x3::position_tagged {
150 static constexpr auto name = "help";
151 static constexpr auto shortHelp = "help - Print help for commands.";
152 static constexpr auto longHelp = R"(
153 help [command_name]
154
155 Print help for command_name. If used without an argument,
156 print short help for all commands.
157
158 Usage:
159 /> help
160 /> help cd
161 /> help help)";
162 bool operator==(const help_& b) const;
163
164 // The help command has got one optional argument – a command name (type).
165 // All commands are saved in CommandTypes, so we could just use that, but
166 // that way, Spirit would be default constructing the command structs,
167 // which is undesirable, so firstly we use mpl::transform to wrap
168 // CommandTypes with boost::type:
169 using WrappedCommandTypes = boost::mpl::transform<CommandTypes, boost::type<boost::mpl::_>>::type;
170 // Next, we create a variant over the wrapped types:
171 using CommandTypesVariant = boost::make_variant_over<WrappedCommandTypes>::type;
172 // Finally, we wrap the variant with boost::optional:
173 boost::optional<CommandTypesVariant> m_cmd;
174};
175
Václav Kubernát57272422019-02-08 12:48:24 +0100176// TODO: The usage of MPL won't be necessary after std::variant support is added to Spirit
177// https://github.com/boostorg/spirit/issues/270
Václav Kubernát57272422019-02-08 12:48:24 +0100178using command_ = boost::make_variant_over<CommandTypes>::type;
Václav Kubernát24df80e2018-06-06 15:18:03 +0200179
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200180BOOST_FUSION_ADAPT_STRUCT(ls_, m_options, m_path)
Václav Kubernát24df80e2018-06-06 15:18:03 +0200181BOOST_FUSION_ADAPT_STRUCT(cd_, m_path)
182BOOST_FUSION_ADAPT_STRUCT(create_, m_path)
183BOOST_FUSION_ADAPT_STRUCT(delete_, m_path)
Václav Kubernát24df80e2018-06-06 15:18:03 +0200184BOOST_FUSION_ADAPT_STRUCT(set_, m_path, m_data)
Václav Kubernátab538992019-03-06 15:30:50 +0100185BOOST_FUSION_ADAPT_STRUCT(enum_, m_value)
186BOOST_FUSION_ADAPT_STRUCT(binary_, m_value)
Václav Kubernáteeb38842019-03-20 19:46:05 +0100187BOOST_FUSION_ADAPT_STRUCT(identityRef_, m_prefix, m_value)
Václav Kubernát812ee282018-08-30 17:10:03 +0200188BOOST_FUSION_ADAPT_STRUCT(commit_)
Václav Kubernát054cc992019-02-21 14:23:52 +0100189BOOST_FUSION_ADAPT_STRUCT(help_, m_cmd)
Václav Kubernát6d791432018-10-25 16:00:35 +0200190BOOST_FUSION_ADAPT_STRUCT(discard_)
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200191BOOST_FUSION_ADAPT_STRUCT(get_, m_path)