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