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 | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 15 | #if defined(SYSREPO_CLI) |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 16 | #include "sysrepo_access.hpp" |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 17 | #define PROGRAM_NAME "sysrepo-cli" |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 18 | static const auto usage = R"(CLI interface to sysrepo |
| 19 | |
| 20 | Usage: |
| 21 | sysrepo-cli [-d <datastore>] |
| 22 | sysrepo-cli (-h | --help) |
| 23 | sysrepo-cli --version |
| 24 | |
| 25 | Options: |
| 26 | -d <datastore> can be "running" or "startup" [default: running])"; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame^] | 27 | #elif defined(YANG_CLI) |
| 28 | #include <boost/spirit/home/x3.hpp> |
| 29 | #include "yang_access.hpp" |
| 30 | #define PROGRAM_NAME "yang-cli" |
| 31 | static const auto usage = R"(CLI interface for creating local YANG data instances |
| 32 | |
| 33 | Usage: |
| 34 | yang-cli [-s <search_dir>] [-e enable_features]... <schema_file>... |
| 35 | yang-cli (-h | --help) |
| 36 | yang-cli --version |
| 37 | |
| 38 | Options: |
| 39 | -s <search_dir> Set search for schema lookup |
| 40 | -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 | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 41 | #else |
| 42 | #error "Unknown CLI backend" |
| 43 | #endif |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 44 | |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 45 | const auto HISTORY_FILE_NAME = PROGRAM_NAME "_history"; |
| 46 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 47 | int main(int argc, char* argv[]) |
| 48 | { |
| 49 | auto args = docopt::docopt(usage, |
| 50 | {argv + 1, argv + argc}, |
| 51 | true, |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 52 | PROGRAM_NAME " " NETCONF_CLI_VERSION, |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 53 | true); |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 54 | |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 55 | #if defined(SYSREPO_CLI) |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 56 | auto datastoreType = Datastore::Running; |
| 57 | if (const auto& ds = args["-d"]) { |
| 58 | if (ds.asString() == "startup") { |
| 59 | datastoreType = Datastore::Startup; |
| 60 | } else if (ds.asString() == "running") { |
| 61 | datastoreType = Datastore::Running; |
| 62 | } else { |
| 63 | std::cerr << PROGRAM_NAME << ": unknown datastore: " << ds.asString() << "\n"; |
| 64 | return 1; |
| 65 | } |
| 66 | } |
| 67 | SysrepoAccess datastore(PROGRAM_NAME, datastoreType); |
| 68 | 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^] | 69 | #elif defined(YANG_CLI) |
| 70 | YangAccess datastore; |
| 71 | if (const auto& search_dir = args["-s"]) { |
| 72 | datastore.addSchemaDir(search_dir.asString()); |
| 73 | } |
| 74 | for (const auto& schemaFile : args["<schema_file>"].asStringList()) { |
| 75 | datastore.addSchemaFile(schemaFile); |
| 76 | } |
| 77 | if (const auto& enableFeatures = args["-e"]) { |
| 78 | namespace x3 = boost::spirit::x3; |
| 79 | auto grammar = +(x3::char_-":") >> ":" >> +(x3::char_-":"); |
| 80 | for (const auto& enableFeature : enableFeatures.asStringList()) { |
| 81 | std::pair<std::string, std::string> parsed; |
| 82 | auto it = enableFeature.begin(); |
| 83 | auto res = x3::parse(it, enableFeature.cend(), grammar, parsed); |
| 84 | if (!res || it != enableFeature.cend()) { |
| 85 | std::cerr << "Error parsing feature enable flags: " << enableFeature << "\n"; |
| 86 | return 1; |
| 87 | } |
| 88 | try { |
| 89 | datastore.enableFeature(parsed.first, parsed.second); |
| 90 | } catch (std::runtime_error& ex) { |
| 91 | std::cerr << ex.what() << "\n"; |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | } |
| 96 | } |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 97 | #else |
| 98 | #error "Unknown CLI backend" |
| 99 | #endif |
| 100 | |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 101 | auto dataQuery = std::make_shared<DataQuery>(datastore); |
| 102 | Parser parser(datastore.schema(), dataQuery); |
Václav Kubernát | 395d92c | 2020-01-24 12:18:18 +0100 | [diff] [blame] | 103 | |
| 104 | using replxx::Replxx; |
| 105 | |
| 106 | Replxx lineEditor; |
| 107 | |
| 108 | lineEditor.bind_key(Replxx::KEY::meta(Replxx::KEY::BACKSPACE), std::bind(&Replxx::invoke, &lineEditor, Replxx::ACTION::KILL_TO_BEGINING_OF_WORD, std::placeholders::_1)); |
| 109 | lineEditor.bind_key(Replxx::KEY::control('W'), std::bind(&Replxx::invoke, &lineEditor, Replxx::ACTION::KILL_TO_WHITESPACE_ON_LEFT, std::placeholders::_1)); |
| 110 | |
| 111 | lineEditor.set_word_break_characters("\t _[]/:'\"=-%"); |
| 112 | |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 113 | 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] | 114 | std::stringstream stream; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 115 | auto completions = parser.completeCommand(input, stream); |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 116 | |
Jan Kundrát | 8d8efe8 | 2019-10-18 10:21:36 +0200 | [diff] [blame] | 117 | std::vector<replxx::Replxx::Completion> res; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 118 | std::copy(completions.m_completions.begin(), completions.m_completions.end(), std::back_inserter(res)); |
| 119 | context = completions.m_contextLength; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 120 | return res; |
| 121 | }); |
Václav Kubernát | 2b68461 | 2018-08-09 18:55:24 +0200 | [diff] [blame] | 122 | |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 123 | std::optional<std::string> historyFile; |
| 124 | if (auto xdgHome = getenv("XDG_DATA_HOME")) { |
| 125 | historyFile = std::string(xdgHome) + "/" + HISTORY_FILE_NAME; |
| 126 | } else if (auto home = getenv("HOME")) { |
| 127 | historyFile = std::string(home) + "/.local/share/" + HISTORY_FILE_NAME; |
| 128 | } |
| 129 | |
| 130 | if (historyFile) |
| 131 | lineEditor.history_load(historyFile.value()); |
| 132 | |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 133 | while (true) { |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 134 | auto line = lineEditor.input(parser.currentNode() + "> "); |
| 135 | if (!line) { |
Václav Kubernát | 82bf131 | 2019-11-05 11:19:26 +0100 | [diff] [blame] | 136 | // If user pressed CTRL-C to abort the line, errno gets set to EAGAIN. |
| 137 | // If user pressed CTRL-D (for EOF), errno doesn't get set to EAGAIN, so we exit the program. |
| 138 | // I have no idea why replxx uses errno for this. |
| 139 | if (errno == EAGAIN) { |
| 140 | continue; |
| 141 | } else { |
| 142 | break; |
| 143 | } |
Václav Kubernát | 5b80e52 | 2019-01-25 12:17:03 +0100 | [diff] [blame] | 144 | } |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 145 | |
Jan Kundrát | e387702 | 2018-09-05 15:32:09 +0200 | [diff] [blame] | 146 | std::locale C_locale("C"); |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 147 | std::string_view view{line}; |
| 148 | if (std::all_of(view.begin(), view.end(), |
Jan Kundrát | e387702 | 2018-09-05 15:32:09 +0200 | [diff] [blame] | 149 | [C_locale](const auto c) { return std::isspace(c, C_locale);})) { |
| 150 | continue; |
| 151 | } |
| 152 | |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 153 | try { |
Václav Kubernát | 5b80e52 | 2019-01-25 12:17:03 +0100 | [diff] [blame] | 154 | command_ cmd = parser.parseCommand(line, std::cout); |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 155 | boost::apply_visitor(Interpreter(parser, datastore), cmd); |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 156 | } catch (InvalidCommandException& ex) { |
| 157 | std::cerr << ex.what() << std::endl; |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 158 | } catch (DatastoreException& ex) { |
| 159 | std::cerr << ex.what() << std::endl; |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 160 | } |
Václav Kubernát | 5b80e52 | 2019-01-25 12:17:03 +0100 | [diff] [blame] | 161 | |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 162 | lineEditor.history_add(line); |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 165 | if (historyFile) |
| 166 | lineEditor.history_save(historyFile.value()); |
| 167 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 168 | return 0; |
| 169 | } |