Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
Jan Kundrát | a274044 | 2018-03-22 16:56:43 +0100 | [diff] [blame] | 3 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 4 | * |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 5 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 6 | * |
| 7 | */ |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 8 | #include <docopt.h> |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 9 | #include <iostream> |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 10 | #include <optional> |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 11 | #include <replxx.hxx> |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 12 | #include <sstream> |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 13 | #include "NETCONF_CLI_VERSION.h" |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 14 | #include "interpreter.hpp" |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 15 | #include "proxy_datastore.hpp" |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 16 | #if defined(SYSREPO_CLI) |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 17 | #include "sysrepo_access.hpp" |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 18 | #include "yang_schema.hpp" |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 19 | #define PROGRAM_NAME "sysrepo-cli" |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 20 | static const auto usage = R"(CLI interface to sysrepo |
| 21 | |
| 22 | Usage: |
| 23 | sysrepo-cli [-d <datastore>] |
| 24 | sysrepo-cli (-h | --help) |
| 25 | sysrepo-cli --version |
| 26 | |
| 27 | Options: |
| 28 | -d <datastore> can be "running" or "startup" [default: running])"; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 29 | #elif defined(YANG_CLI) |
| 30 | #include <boost/spirit/home/x3.hpp> |
Václav Kubernát | 619e654 | 2020-06-29 14:13:43 +0200 | [diff] [blame] | 31 | #include <filesystem> |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 32 | #include "yang_access.hpp" |
| 33 | #define PROGRAM_NAME "yang-cli" |
| 34 | static const auto usage = R"(CLI interface for creating local YANG data instances |
| 35 | |
Václav Kubernát | 619e654 | 2020-06-29 14:13:43 +0200 | [diff] [blame] | 36 | The <schema_file_or_module_name> argument is treated as a file name if a file |
| 37 | with such a path exists, otherwise it's treated as a module name. Search dirs |
| 38 | will be used to find a schema for that module. |
| 39 | |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 40 | Usage: |
Václav Kubernát | 28cf336 | 2020-06-29 17:52:51 +0200 | [diff] [blame] | 41 | yang-cli [--configonly] [-s <search_dir>] [-e enable_features]... [-i data_file]... <schema_file_or_module_name>... |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 42 | yang-cli (-h | --help) |
| 43 | yang-cli --version |
| 44 | |
| 45 | Options: |
| 46 | -s <search_dir> Set search for schema lookup |
Václav Kubernát | 548cb19 | 2020-06-26 14:00:42 +0200 | [diff] [blame] | 47 | -e <enable_features> Feature to enable after modules are loaded. This option can be supplied more than once. Format: <module_name>:<feature> |
Václav Kubernát | 28cf336 | 2020-06-29 17:52:51 +0200 | [diff] [blame] | 48 | -i <data_file> File to import data from |
| 49 | --configonly Disable editing of operational data)"; |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 50 | #else |
| 51 | #error "Unknown CLI backend" |
| 52 | #endif |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 53 | |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 54 | const auto HISTORY_FILE_NAME = PROGRAM_NAME "_history"; |
| 55 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 56 | int main(int argc, char* argv[]) |
| 57 | { |
| 58 | auto args = docopt::docopt(usage, |
| 59 | {argv + 1, argv + argc}, |
| 60 | true, |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 61 | PROGRAM_NAME " " NETCONF_CLI_VERSION, |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 62 | true); |
Václav Kubernát | 28cf336 | 2020-06-29 17:52:51 +0200 | [diff] [blame] | 63 | WritableOps writableOps = WritableOps::No; |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 64 | |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 65 | #if defined(SYSREPO_CLI) |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 66 | auto datastoreType = Datastore::Running; |
| 67 | if (const auto& ds = args["-d"]) { |
| 68 | if (ds.asString() == "startup") { |
| 69 | datastoreType = Datastore::Startup; |
| 70 | } else if (ds.asString() == "running") { |
| 71 | datastoreType = Datastore::Running; |
| 72 | } else { |
| 73 | std::cerr << PROGRAM_NAME << ": unknown datastore: " << ds.asString() << "\n"; |
| 74 | return 1; |
| 75 | } |
| 76 | } |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 77 | auto datastore = std::make_shared<SysrepoAccess>(datastoreType); |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 78 | std::cout << "Connected to sysrepo [datastore: " << (datastoreType == Datastore::Startup ? "startup" : "running") << "]" << std::endl; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 79 | #elif defined(YANG_CLI) |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 80 | auto datastore = std::make_shared<YangAccess>(); |
Václav Kubernát | 28cf336 | 2020-06-29 17:52:51 +0200 | [diff] [blame] | 81 | if (args["--configonly"].asBool()) { |
| 82 | writableOps = WritableOps::No; |
| 83 | } else { |
| 84 | writableOps = WritableOps::Yes; |
| 85 | std::cout << "ops is writable" << std::endl; |
| 86 | } |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 87 | if (const auto& search_dir = args["-s"]) { |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 88 | datastore->addSchemaDir(search_dir.asString()); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 89 | } |
Václav Kubernát | 619e654 | 2020-06-29 14:13:43 +0200 | [diff] [blame] | 90 | for (const auto& schemaFile : args["<schema_file_or_module_name>"].asStringList()) { |
| 91 | if (std::filesystem::exists(schemaFile)) { |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 92 | datastore->addSchemaFile(schemaFile); |
Václav Kubernát | 619e654 | 2020-06-29 14:13:43 +0200 | [diff] [blame] | 93 | } else if (schemaFile.find('/') == std::string::npos) { // Module names cannot have a slash |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 94 | datastore->loadModule(schemaFile); |
Václav Kubernát | 619e654 | 2020-06-29 14:13:43 +0200 | [diff] [blame] | 95 | } else { |
| 96 | std::cerr << "Cannot load YANG module " << schemaFile << "\n"; |
| 97 | } |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 98 | } |
| 99 | if (const auto& enableFeatures = args["-e"]) { |
| 100 | namespace x3 = boost::spirit::x3; |
| 101 | auto grammar = +(x3::char_-":") >> ":" >> +(x3::char_-":"); |
| 102 | for (const auto& enableFeature : enableFeatures.asStringList()) { |
| 103 | std::pair<std::string, std::string> parsed; |
| 104 | auto it = enableFeature.begin(); |
| 105 | auto res = x3::parse(it, enableFeature.cend(), grammar, parsed); |
| 106 | if (!res || it != enableFeature.cend()) { |
| 107 | std::cerr << "Error parsing feature enable flags: " << enableFeature << "\n"; |
| 108 | return 1; |
| 109 | } |
| 110 | try { |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 111 | datastore->enableFeature(parsed.first, parsed.second); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 112 | } catch (std::runtime_error& ex) { |
| 113 | std::cerr << ex.what() << "\n"; |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | } |
| 118 | } |
Václav Kubernát | 548cb19 | 2020-06-26 14:00:42 +0200 | [diff] [blame] | 119 | if (const auto& dataFiles = args["-i"]) { |
| 120 | for (const auto& dataFile : dataFiles.asStringList()) { |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 121 | datastore->addDataFile(dataFile); |
Václav Kubernát | 548cb19 | 2020-06-26 14:00:42 +0200 | [diff] [blame] | 122 | } |
| 123 | } |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 124 | #else |
| 125 | #error "Unknown CLI backend" |
| 126 | #endif |
| 127 | |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 128 | #if defined(SYSREPO_CLI) |
| 129 | auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>& datastore) { |
| 130 | return std::make_shared<YangAccess>(std::static_pointer_cast<YangSchema>(datastore->schema())); |
| 131 | }; |
| 132 | #elif defined(YANG_CLI) |
| 133 | auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>&) { |
| 134 | return nullptr; |
| 135 | }; |
| 136 | #endif |
| 137 | |
| 138 | ProxyDatastore proxyDatastore(datastore, createTemporaryDatastore); |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 139 | auto dataQuery = std::make_shared<DataQuery>(*datastore); |
| 140 | Parser parser(datastore->schema(), writableOps, dataQuery); |
Václav Kubernát | 395d92c | 2020-01-24 12:18:18 +0100 | [diff] [blame] | 141 | |
| 142 | using replxx::Replxx; |
| 143 | |
| 144 | Replxx lineEditor; |
| 145 | |
Václav Kubernát | 4863729 | 2020-07-08 16:54:13 +0200 | [diff] [blame] | 146 | lineEditor.bind_key(Replxx::KEY::meta(Replxx::KEY::BACKSPACE), [&lineEditor] (const auto& code) { |
| 147 | return lineEditor.invoke(Replxx::ACTION::KILL_TO_BEGINING_OF_WORD, code); |
| 148 | }); |
| 149 | lineEditor.bind_key(Replxx::KEY::control('W'), [&lineEditor] (const auto& code) { |
| 150 | return lineEditor.invoke(Replxx::ACTION::KILL_TO_WHITESPACE_ON_LEFT, code); |
| 151 | }); |
Václav Kubernát | 395d92c | 2020-01-24 12:18:18 +0100 | [diff] [blame] | 152 | |
| 153 | lineEditor.set_word_break_characters("\t _[]/:'\"=-%"); |
| 154 | |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 155 | lineEditor.set_completion_callback([&parser](const std::string& input, int& context) { |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 156 | std::stringstream stream; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 157 | auto completions = parser.completeCommand(input, stream); |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 158 | |
Jan Kundrát | 8d8efe8 | 2019-10-18 10:21:36 +0200 | [diff] [blame] | 159 | std::vector<replxx::Replxx::Completion> res; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 160 | std::copy(completions.m_completions.begin(), completions.m_completions.end(), std::back_inserter(res)); |
| 161 | context = completions.m_contextLength; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 162 | return res; |
| 163 | }); |
Václav Kubernát | 2b68461 | 2018-08-09 18:55:24 +0200 | [diff] [blame] | 164 | |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 165 | std::optional<std::string> historyFile; |
| 166 | if (auto xdgHome = getenv("XDG_DATA_HOME")) { |
| 167 | historyFile = std::string(xdgHome) + "/" + HISTORY_FILE_NAME; |
| 168 | } else if (auto home = getenv("HOME")) { |
| 169 | historyFile = std::string(home) + "/.local/share/" + HISTORY_FILE_NAME; |
| 170 | } |
| 171 | |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 172 | if (historyFile) { |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 173 | lineEditor.history_load(historyFile.value()); |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 174 | } |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 175 | |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 176 | while (true) { |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 177 | auto line = lineEditor.input(parser.currentNode() + "> "); |
| 178 | if (!line) { |
Václav Kubernát | 82bf131 | 2019-11-05 11:19:26 +0100 | [diff] [blame] | 179 | // If user pressed CTRL-C to abort the line, errno gets set to EAGAIN. |
| 180 | // If user pressed CTRL-D (for EOF), errno doesn't get set to EAGAIN, so we exit the program. |
| 181 | // I have no idea why replxx uses errno for this. |
| 182 | if (errno == EAGAIN) { |
| 183 | continue; |
| 184 | } else { |
| 185 | break; |
| 186 | } |
Václav Kubernát | 5b80e52 | 2019-01-25 12:17:03 +0100 | [diff] [blame] | 187 | } |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 188 | |
Jan Kundrát | e387702 | 2018-09-05 15:32:09 +0200 | [diff] [blame] | 189 | std::locale C_locale("C"); |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 190 | std::string_view view{line}; |
| 191 | if (std::all_of(view.begin(), view.end(), |
Jan Kundrát | e387702 | 2018-09-05 15:32:09 +0200 | [diff] [blame] | 192 | [C_locale](const auto c) { return std::isspace(c, C_locale);})) { |
| 193 | continue; |
| 194 | } |
| 195 | |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 196 | try { |
Václav Kubernát | 5b80e52 | 2019-01-25 12:17:03 +0100 | [diff] [blame] | 197 | command_ cmd = parser.parseCommand(line, std::cout); |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 198 | boost::apply_visitor(Interpreter(parser, proxyDatastore), cmd); |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 199 | } catch (InvalidCommandException& ex) { |
| 200 | std::cerr << ex.what() << std::endl; |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 201 | } catch (DatastoreException& ex) { |
| 202 | std::cerr << ex.what() << std::endl; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 203 | } catch (std::runtime_error& ex) { |
| 204 | std::cerr << ex.what() << std::endl; |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 205 | } |
Václav Kubernát | 5b80e52 | 2019-01-25 12:17:03 +0100 | [diff] [blame] | 206 | |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 207 | lineEditor.history_add(line); |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 210 | if (historyFile) { |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 211 | lineEditor.history_save(historyFile.value()); |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 212 | } |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 213 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 214 | return 0; |
| 215 | } |