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" |
| 18 | #define PROGRAM_DESCRIPTION R"(CLI interface to sysrepo \ |
| 19 | \ |
| 20 | Usage: \ |
| 21 | sysrepo-cli \ |
| 22 | sysrepo-cli (-h | --help) \ |
| 23 | sysrepo-cli --version \ |
| 24 | )" |
| 25 | #else |
| 26 | #error "Unknown CLI backend" |
| 27 | #endif |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 28 | |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 29 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 30 | |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 31 | |
| 32 | const auto HISTORY_FILE_NAME = PROGRAM_NAME "_history"; |
| 33 | |
| 34 | static const char usage[] = PROGRAM_DESCRIPTION; |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 35 | |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 36 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 37 | int main(int argc, char* argv[]) |
| 38 | { |
| 39 | auto args = docopt::docopt(usage, |
| 40 | {argv + 1, argv + argc}, |
| 41 | true, |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 42 | PROGRAM_NAME " " NETCONF_CLI_VERSION, |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 43 | true); |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 44 | std::cout << "Welcome to " PROGRAM_NAME << std::endl; |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 45 | |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 46 | #if defined(SYSREPO_CLI) |
| 47 | SysrepoAccess datastore(PROGRAM_NAME); |
| 48 | #else |
| 49 | #error "Unknown CLI backend" |
| 50 | #endif |
| 51 | |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame] | 52 | auto dataQuery = std::make_shared<DataQuery>(datastore); |
| 53 | Parser parser(datastore.schema(), dataQuery); |
Václav Kubernát | 395d92c | 2020-01-24 12:18:18 +0100 | [diff] [blame] | 54 | |
| 55 | using replxx::Replxx; |
| 56 | |
| 57 | Replxx lineEditor; |
| 58 | |
| 59 | lineEditor.bind_key(Replxx::KEY::meta(Replxx::KEY::BACKSPACE), std::bind(&Replxx::invoke, &lineEditor, Replxx::ACTION::KILL_TO_BEGINING_OF_WORD, std::placeholders::_1)); |
| 60 | lineEditor.bind_key(Replxx::KEY::control('W'), std::bind(&Replxx::invoke, &lineEditor, Replxx::ACTION::KILL_TO_WHITESPACE_ON_LEFT, std::placeholders::_1)); |
| 61 | |
| 62 | lineEditor.set_word_break_characters("\t _[]/:'\"=-%"); |
| 63 | |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 64 | 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] | 65 | std::stringstream stream; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 66 | auto completions = parser.completeCommand(input, stream); |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 67 | |
Jan Kundrát | 8d8efe8 | 2019-10-18 10:21:36 +0200 | [diff] [blame] | 68 | std::vector<replxx::Replxx::Completion> res; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 69 | std::copy(completions.m_completions.begin(), completions.m_completions.end(), std::back_inserter(res)); |
| 70 | context = completions.m_contextLength; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 71 | return res; |
| 72 | }); |
Václav Kubernát | 2b68461 | 2018-08-09 18:55:24 +0200 | [diff] [blame] | 73 | |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 74 | std::optional<std::string> historyFile; |
| 75 | if (auto xdgHome = getenv("XDG_DATA_HOME")) { |
| 76 | historyFile = std::string(xdgHome) + "/" + HISTORY_FILE_NAME; |
| 77 | } else if (auto home = getenv("HOME")) { |
| 78 | historyFile = std::string(home) + "/.local/share/" + HISTORY_FILE_NAME; |
| 79 | } |
| 80 | |
| 81 | if (historyFile) |
| 82 | lineEditor.history_load(historyFile.value()); |
| 83 | |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 84 | while (true) { |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 85 | auto line = lineEditor.input(parser.currentNode() + "> "); |
| 86 | if (!line) { |
Václav Kubernát | 82bf131 | 2019-11-05 11:19:26 +0100 | [diff] [blame] | 87 | // If user pressed CTRL-C to abort the line, errno gets set to EAGAIN. |
| 88 | // If user pressed CTRL-D (for EOF), errno doesn't get set to EAGAIN, so we exit the program. |
| 89 | // I have no idea why replxx uses errno for this. |
| 90 | if (errno == EAGAIN) { |
| 91 | continue; |
| 92 | } else { |
| 93 | break; |
| 94 | } |
Václav Kubernát | 5b80e52 | 2019-01-25 12:17:03 +0100 | [diff] [blame] | 95 | } |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 96 | |
Jan Kundrát | e387702 | 2018-09-05 15:32:09 +0200 | [diff] [blame] | 97 | std::locale C_locale("C"); |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 98 | std::string_view view{line}; |
| 99 | if (std::all_of(view.begin(), view.end(), |
Jan Kundrát | e387702 | 2018-09-05 15:32:09 +0200 | [diff] [blame] | 100 | [C_locale](const auto c) { return std::isspace(c, C_locale);})) { |
| 101 | continue; |
| 102 | } |
| 103 | |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 104 | try { |
Václav Kubernát | 5b80e52 | 2019-01-25 12:17:03 +0100 | [diff] [blame] | 105 | command_ cmd = parser.parseCommand(line, std::cout); |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 106 | boost::apply_visitor(Interpreter(parser, datastore), cmd); |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 107 | } catch (InvalidCommandException& ex) { |
| 108 | std::cerr << ex.what() << std::endl; |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 109 | } catch (DatastoreException& ex) { |
| 110 | std::cerr << ex.what() << std::endl; |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 111 | } |
Václav Kubernát | 5b80e52 | 2019-01-25 12:17:03 +0100 | [diff] [blame] | 112 | |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 113 | lineEditor.history_add(line); |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 116 | if (historyFile) |
| 117 | lineEditor.history_save(historyFile.value()); |
| 118 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 119 | return 0; |
| 120 | } |