Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 1 | /* |
| 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át | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 10 | #include <boost/mpl/vector.hpp> |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 11 | #include "ast_path.hpp" |
Václav Kubernát | 627f615 | 2018-08-29 13:23:56 +0200 | [diff] [blame] | 12 | #include "ast_values.hpp" |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 13 | |
| 14 | namespace x3 = boost::spirit::x3; |
| 15 | namespace ascii = boost::spirit::x3::ascii; |
| 16 | |
| 17 | using ascii::space; |
| 18 | using x3::_attr; |
| 19 | using x3::alnum; |
| 20 | using x3::alpha; |
| 21 | using x3::char_; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 22 | using x3::double_; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 23 | using x3::expect; |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 24 | using x3::int8; |
| 25 | using x3::int16; |
| 26 | using x3::int32; |
| 27 | using x3::int64; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 28 | using x3::lexeme; |
| 29 | using x3::lit; |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 30 | using x3::uint8; |
| 31 | using x3::uint16; |
| 32 | using x3::uint32; |
| 33 | using x3::uint64; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 34 | |
| 35 | struct parser_context_tag; |
| 36 | |
| 37 | using keyValue_ = std::pair<std::string, std::string>; |
| 38 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 39 | enum class LsOption { |
| 40 | Recursive |
| 41 | }; |
| 42 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 43 | struct discard_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 44 | static constexpr auto name = "discard"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 45 | static constexpr auto shortHelp = "discard - Discard current changes."; |
| 46 | static constexpr auto longHelp = R"( |
| 47 | discard |
| 48 | |
| 49 | Discards current changes. Accepts no arguments. |
| 50 | |
| 51 | Usage: |
| 52 | /> discard)"; |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 53 | bool operator==(const discard_& b) const; |
| 54 | }; |
| 55 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 56 | struct ls_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 57 | static constexpr auto name = "ls"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 58 | static constexpr auto shortHelp = "ls - List available nodes."; |
| 59 | static constexpr auto longHelp = R"( |
| 60 | ls [--recursive] [path] |
| 61 | |
| 62 | Lists available nodes in the current directory. Optionally |
| 63 | accepts a path argument. Accepts both schema paths and data |
| 64 | paths. Path starting with a forward slash means an absolute |
| 65 | path. |
| 66 | |
| 67 | Usage: |
| 68 | /> ls |
| 69 | /> ls --recursive module:node |
| 70 | /> ls /module:node)"; |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 71 | bool operator==(const ls_& b) const; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 72 | std::vector<LsOption> m_options; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 73 | boost::optional<boost::variant<dataPath_, schemaPath_>> m_path; |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 74 | }; |
| 75 | |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 76 | struct cd_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 77 | static constexpr auto name = "cd"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 78 | static constexpr auto shortHelp = "cd - Enter a different node."; |
| 79 | static constexpr auto longHelp = R"( |
| 80 | cd path |
| 81 | |
| 82 | Enters a node specified by path. Only accepts data paths. |
| 83 | |
| 84 | Usage: |
| 85 | /> cd /module:node/node2 |
| 86 | /> cd ..)"; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 87 | bool operator==(const cd_& b) const; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 88 | dataPath_ m_path; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | struct create_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 92 | static constexpr auto name = "create"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 93 | static constexpr auto shortHelp = "create - Create a presence container."; |
| 94 | static constexpr auto longHelp = R"( |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 95 | create path |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 96 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 97 | Creates a presence container or a list instance specified by path. |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 98 | |
| 99 | Usage: |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 100 | /> create /module:pContainer |
| 101 | /> create /module:list[key=value][anotherKey=value])"; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 102 | bool operator==(const create_& b) const; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 103 | dataPath_ m_path; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | struct delete_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 107 | static constexpr auto name = "delete"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 108 | static constexpr auto shortHelp = "delete - Delete a presence container."; |
| 109 | static constexpr auto longHelp = R"( |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 110 | delete path |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 111 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 112 | Deletes a presence container or a list instance specified by path. |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 113 | |
| 114 | Usage: |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 115 | /> delete /module:pContainer |
| 116 | /> delete /module:list[key=value][anotherKey=value])"; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 117 | bool operator==(const delete_& b) const; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 118 | dataPath_ m_path; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | struct set_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 122 | static constexpr auto name = "set"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 123 | static constexpr auto shortHelp = "set - Change value of a leaf."; |
| 124 | static constexpr auto longHelp = R"( |
| 125 | set path_to_leaf value |
| 126 | |
| 127 | Changes the leaf specified by path to value. |
| 128 | |
| 129 | Usage: |
| 130 | /> set /module:leaf 123 |
| 131 | /> set /module:leaf abc)"; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 132 | bool operator==(const set_& b) const; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 133 | dataPath_ m_path; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 134 | leaf_data_ m_data; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 135 | }; |
| 136 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 137 | struct commit_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 138 | static constexpr auto name = "commit"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 139 | static constexpr auto shortHelp = "commit - Commit current changes."; |
| 140 | static constexpr auto longHelp = R"( |
| 141 | commit |
| 142 | |
| 143 | Commits the current changes. Accepts no arguments. |
| 144 | |
| 145 | Usage: |
| 146 | /> commit)"; |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 147 | bool operator==(const set_& b) const; |
| 148 | }; |
| 149 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 150 | struct get_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 151 | static constexpr auto name = "get"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 152 | static constexpr auto shortHelp = "get - Retrieve configuration from the server."; |
| 153 | static constexpr auto longHelp = R"( |
| 154 | get [path] |
| 155 | |
| 156 | Retrieves configuration of the current node. Works recursively. |
| 157 | Optionally takes an argument specifying a path, the output will, |
| 158 | as if the user was in that node. |
| 159 | |
| 160 | Usage: |
| 161 | /> get |
| 162 | /> get /module:path)"; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 163 | bool operator==(const get_& b) const; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 164 | boost::optional<boost::variant<dataPath_, schemaPath_>> m_path; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 165 | }; |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 166 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 167 | struct help_; |
| 168 | using CommandTypes = boost::mpl::vector<discard_, ls_, cd_, create_, delete_, set_, commit_, get_, help_>; |
| 169 | struct help_ : x3::position_tagged { |
| 170 | static constexpr auto name = "help"; |
| 171 | static constexpr auto shortHelp = "help - Print help for commands."; |
| 172 | static constexpr auto longHelp = R"( |
| 173 | help [command_name] |
| 174 | |
| 175 | Print help for command_name. If used without an argument, |
| 176 | print short help for all commands. |
| 177 | |
| 178 | Usage: |
| 179 | /> help |
| 180 | /> help cd |
| 181 | /> help help)"; |
| 182 | bool operator==(const help_& b) const; |
| 183 | |
| 184 | // The help command has got one optional argument – a command name (type). |
| 185 | // All commands are saved in CommandTypes, so we could just use that, but |
| 186 | // that way, Spirit would be default constructing the command structs, |
| 187 | // which is undesirable, so firstly we use mpl::transform to wrap |
| 188 | // CommandTypes with boost::type: |
| 189 | using WrappedCommandTypes = boost::mpl::transform<CommandTypes, boost::type<boost::mpl::_>>::type; |
| 190 | // Next, we create a variant over the wrapped types: |
| 191 | using CommandTypesVariant = boost::make_variant_over<WrappedCommandTypes>::type; |
| 192 | // Finally, we wrap the variant with boost::optional: |
| 193 | boost::optional<CommandTypesVariant> m_cmd; |
| 194 | }; |
| 195 | |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 196 | // TODO: The usage of MPL won't be necessary after std::variant support is added to Spirit |
| 197 | // https://github.com/boostorg/spirit/issues/270 |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 198 | using command_ = boost::make_variant_over<CommandTypes>::type; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 199 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 200 | BOOST_FUSION_ADAPT_STRUCT(ls_, m_options, m_path) |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 201 | BOOST_FUSION_ADAPT_STRUCT(cd_, m_path) |
| 202 | BOOST_FUSION_ADAPT_STRUCT(create_, m_path) |
| 203 | BOOST_FUSION_ADAPT_STRUCT(delete_, m_path) |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 204 | BOOST_FUSION_ADAPT_STRUCT(set_, m_path, m_data) |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 205 | BOOST_FUSION_ADAPT_STRUCT(enum_, m_value) |
| 206 | BOOST_FUSION_ADAPT_STRUCT(binary_, m_value) |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 207 | BOOST_FUSION_ADAPT_STRUCT(identityRef_, m_prefix, m_value) |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 208 | BOOST_FUSION_ADAPT_STRUCT(commit_) |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 209 | BOOST_FUSION_ADAPT_STRUCT(help_, m_cmd) |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 210 | BOOST_FUSION_ADAPT_STRUCT(discard_) |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 211 | BOOST_FUSION_ADAPT_STRUCT(get_, m_path) |