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"( |
| 89 | create path_to_presence_container |
| 90 | |
| 91 | Creates a presence container specified by a path. |
| 92 | |
| 93 | Usage: |
| 94 | /> create /module:pContainer)"; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 95 | bool operator==(const create_& b) const; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 96 | dataPath_ m_path; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | struct delete_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 100 | static constexpr auto name = "delete"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 101 | static constexpr auto shortHelp = "delete - Delete a presence container."; |
| 102 | static constexpr auto longHelp = R"( |
| 103 | delete path_to_presence_container |
| 104 | |
| 105 | Delete a presence container specified by a path. |
| 106 | |
| 107 | Usage: |
| 108 | /> delete /module:pContainer)"; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 109 | bool operator==(const delete_& b) const; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 110 | dataPath_ m_path; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | struct set_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 114 | static constexpr auto name = "set"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 115 | static constexpr auto shortHelp = "set - Change value of a leaf."; |
| 116 | static constexpr auto longHelp = R"( |
| 117 | set path_to_leaf value |
| 118 | |
| 119 | Changes the leaf specified by path to value. |
| 120 | |
| 121 | Usage: |
| 122 | /> set /module:leaf 123 |
| 123 | /> set /module:leaf abc)"; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 124 | bool operator==(const set_& b) const; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 125 | dataPath_ m_path; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 126 | leaf_data_ m_data; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 127 | }; |
| 128 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 129 | struct commit_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 130 | static constexpr auto name = "commit"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 131 | static constexpr auto shortHelp = "commit - Commit current changes."; |
| 132 | static constexpr auto longHelp = R"( |
| 133 | commit |
| 134 | |
| 135 | Commits the current changes. Accepts no arguments. |
| 136 | |
| 137 | Usage: |
| 138 | /> commit)"; |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 139 | bool operator==(const set_& b) const; |
| 140 | }; |
| 141 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 142 | struct get_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 143 | static constexpr auto name = "get"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 144 | static constexpr auto shortHelp = "get - Retrieve configuration from the server."; |
| 145 | static constexpr auto longHelp = R"( |
| 146 | get [path] |
| 147 | |
| 148 | Retrieves configuration of the current node. Works recursively. |
| 149 | Optionally takes an argument specifying a path, the output will, |
| 150 | as if the user was in that node. |
| 151 | |
| 152 | Usage: |
| 153 | /> get |
| 154 | /> get /module:path)"; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 155 | bool operator==(const get_& b) const; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 156 | boost::optional<boost::variant<dataPath_, schemaPath_>> m_path; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 157 | }; |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 158 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 159 | struct help_; |
| 160 | using CommandTypes = boost::mpl::vector<discard_, ls_, cd_, create_, delete_, set_, commit_, get_, help_>; |
| 161 | struct help_ : x3::position_tagged { |
| 162 | static constexpr auto name = "help"; |
| 163 | static constexpr auto shortHelp = "help - Print help for commands."; |
| 164 | static constexpr auto longHelp = R"( |
| 165 | help [command_name] |
| 166 | |
| 167 | Print help for command_name. If used without an argument, |
| 168 | print short help for all commands. |
| 169 | |
| 170 | Usage: |
| 171 | /> help |
| 172 | /> help cd |
| 173 | /> help help)"; |
| 174 | bool operator==(const help_& b) const; |
| 175 | |
| 176 | // The help command has got one optional argument – a command name (type). |
| 177 | // All commands are saved in CommandTypes, so we could just use that, but |
| 178 | // that way, Spirit would be default constructing the command structs, |
| 179 | // which is undesirable, so firstly we use mpl::transform to wrap |
| 180 | // CommandTypes with boost::type: |
| 181 | using WrappedCommandTypes = boost::mpl::transform<CommandTypes, boost::type<boost::mpl::_>>::type; |
| 182 | // Next, we create a variant over the wrapped types: |
| 183 | using CommandTypesVariant = boost::make_variant_over<WrappedCommandTypes>::type; |
| 184 | // Finally, we wrap the variant with boost::optional: |
| 185 | boost::optional<CommandTypesVariant> m_cmd; |
| 186 | }; |
| 187 | |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 188 | // TODO: The usage of MPL won't be necessary after std::variant support is added to Spirit |
| 189 | // https://github.com/boostorg/spirit/issues/270 |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 190 | using command_ = boost::make_variant_over<CommandTypes>::type; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 191 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 192 | BOOST_FUSION_ADAPT_STRUCT(ls_, m_options, m_path) |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 193 | BOOST_FUSION_ADAPT_STRUCT(cd_, m_path) |
| 194 | BOOST_FUSION_ADAPT_STRUCT(create_, m_path) |
| 195 | BOOST_FUSION_ADAPT_STRUCT(delete_, m_path) |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 196 | BOOST_FUSION_ADAPT_STRUCT(enum_, m_value) |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 197 | BOOST_FUSION_ADAPT_STRUCT(set_, m_path, m_data) |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 198 | BOOST_FUSION_ADAPT_STRUCT(commit_) |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 199 | BOOST_FUSION_ADAPT_STRUCT(help_, m_cmd) |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 200 | BOOST_FUSION_ADAPT_STRUCT(discard_) |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 201 | BOOST_FUSION_ADAPT_STRUCT(get_, m_path) |