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 | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 11 | #include <boost/spirit/home/x3/support/ast/position_tagged.hpp> |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 12 | #include "ast_path.hpp" |
Václav Kubernát | 627f615 | 2018-08-29 13:23:56 +0200 | [diff] [blame] | 13 | #include "ast_values.hpp" |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 14 | |
| 15 | namespace x3 = boost::spirit::x3; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 16 | |
| 17 | using keyValue_ = std::pair<std::string, std::string>; |
| 18 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 19 | enum class LsOption { |
| 20 | Recursive |
| 21 | }; |
| 22 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 23 | struct discard_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 24 | static constexpr auto name = "discard"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 25 | 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át | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 33 | bool operator==(const discard_& b) const; |
| 34 | }; |
| 35 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 36 | struct ls_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 37 | static constexpr auto name = "ls"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 38 | 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át | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 51 | bool operator==(const ls_& b) const; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 52 | std::vector<LsOption> m_options; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 53 | boost::optional<boost::variant<dataPath_, schemaPath_>> m_path; |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 56 | struct cd_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 57 | static constexpr auto name = "cd"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 58 | 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át | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 67 | bool operator==(const cd_& b) const; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 68 | dataPath_ m_path; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | struct create_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 72 | static constexpr auto name = "create"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 73 | static constexpr auto shortHelp = "create - Create a presence container."; |
| 74 | static constexpr auto longHelp = R"( |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 75 | create path |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 76 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 77 | 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] | 78 | |
| 79 | Usage: |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 80 | /> create /module:pContainer |
| 81 | /> create /module:list[key=value][anotherKey=value])"; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 82 | bool operator==(const create_& b) const; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 83 | dataPath_ m_path; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | struct delete_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 87 | static constexpr auto name = "delete"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 88 | static constexpr auto shortHelp = "delete - Delete a presence container."; |
| 89 | static constexpr auto longHelp = R"( |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 90 | delete path |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 91 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 92 | 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] | 93 | |
| 94 | Usage: |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 95 | /> delete /module:pContainer |
| 96 | /> delete /module:list[key=value][anotherKey=value])"; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 97 | bool operator==(const delete_& b) const; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 98 | dataPath_ m_path; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | struct set_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 102 | static constexpr auto name = "set"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 103 | 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át | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 112 | bool operator==(const set_& b) const; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 113 | dataPath_ m_path; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 114 | leaf_data_ m_data; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 115 | }; |
| 116 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 117 | struct commit_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 118 | static constexpr auto name = "commit"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 119 | 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át | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 127 | bool operator==(const set_& b) const; |
| 128 | }; |
| 129 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 130 | struct get_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 131 | static constexpr auto name = "get"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 132 | 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át | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 143 | bool operator==(const get_& b) const; |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 144 | boost::optional<boost::variant<dataPath_, schemaPath_>> m_path; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 145 | }; |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 146 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 147 | struct help_; |
| 148 | using CommandTypes = boost::mpl::vector<discard_, ls_, cd_, create_, delete_, set_, commit_, get_, help_>; |
| 149 | struct 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át | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 176 | // 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át | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 178 | using command_ = boost::make_variant_over<CommandTypes>::type; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 179 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 180 | BOOST_FUSION_ADAPT_STRUCT(ls_, m_options, m_path) |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 181 | BOOST_FUSION_ADAPT_STRUCT(cd_, m_path) |
| 182 | BOOST_FUSION_ADAPT_STRUCT(create_, m_path) |
| 183 | BOOST_FUSION_ADAPT_STRUCT(delete_, m_path) |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 184 | BOOST_FUSION_ADAPT_STRUCT(set_, m_path, m_data) |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 185 | BOOST_FUSION_ADAPT_STRUCT(enum_, m_value) |
| 186 | BOOST_FUSION_ADAPT_STRUCT(binary_, m_value) |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 187 | BOOST_FUSION_ADAPT_STRUCT(identityRef_, m_prefix, m_value) |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 188 | BOOST_FUSION_ADAPT_STRUCT(commit_) |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 189 | BOOST_FUSION_ADAPT_STRUCT(help_, m_cmd) |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 190 | BOOST_FUSION_ADAPT_STRUCT(discard_) |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 191 | BOOST_FUSION_ADAPT_STRUCT(get_, m_path) |