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 | 2b68461 | 2018-08-09 18:55:24 +0200 | [diff] [blame] | 9 | #include <experimental/filesystem> |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 10 | #include <iostream> |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 11 | #include <optional> |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 12 | #include <replxx.hxx> |
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 | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 15 | #include "sysrepo_access.hpp" |
Václav Kubernát | 2b68461 | 2018-08-09 18:55:24 +0200 | [diff] [blame] | 16 | #include "yang_schema.hpp" |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 17 | |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 18 | const auto HISTORY_FILE_NAME = "netconf-cli_history"; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 19 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 20 | static const char usage[] = |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 21 | R"(CLI interface to remote NETCONF hosts |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 22 | |
| 23 | Usage: |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 24 | netconf-cli |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 25 | netconf-cli (-h | --help) |
| 26 | netconf-cli --version |
| 27 | )"; |
| 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 | int main(int argc, char* argv[]) |
| 31 | { |
| 32 | auto args = docopt::docopt(usage, |
| 33 | {argv + 1, argv + argc}, |
| 34 | true, |
| 35 | "netconf-cli " NETCONF_CLI_VERSION, |
| 36 | true); |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 37 | std::cout << "Welcome to netconf-cli" << std::endl; |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 38 | |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 39 | SysrepoAccess datastore("netconf-cli"); |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 40 | Parser parser(datastore.schema()); |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 41 | replxx::Replxx lineEditor; |
| 42 | lineEditor.set_completion_callback([&parser](const std::string& input, int&) { |
| 43 | std::stringstream stream; |
| 44 | auto completionsSet = parser.completeCommand(input, stream); |
| 45 | |
| 46 | std::vector<std::string> res; |
| 47 | std::transform(completionsSet.begin(), completionsSet.end(), std::back_inserter(res), |
| 48 | [input](auto it) { return it; }); |
| 49 | return res; |
| 50 | }); |
| 51 | lineEditor.set_word_break_characters(" '/["); |
Václav Kubernát | 2b68461 | 2018-08-09 18:55:24 +0200 | [diff] [blame] | 52 | |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 53 | std::optional<std::string> historyFile; |
| 54 | if (auto xdgHome = getenv("XDG_DATA_HOME")) { |
| 55 | historyFile = std::string(xdgHome) + "/" + HISTORY_FILE_NAME; |
| 56 | } else if (auto home = getenv("HOME")) { |
| 57 | historyFile = std::string(home) + "/.local/share/" + HISTORY_FILE_NAME; |
| 58 | } |
| 59 | |
| 60 | if (historyFile) |
| 61 | lineEditor.history_load(historyFile.value()); |
| 62 | |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 63 | while (true) { |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 64 | auto line = lineEditor.input(parser.currentNode() + "> "); |
| 65 | if (!line) { |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 66 | break; |
Václav Kubernát | 5b80e52 | 2019-01-25 12:17:03 +0100 | [diff] [blame] | 67 | } |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 68 | |
Jan Kundrát | e387702 | 2018-09-05 15:32:09 +0200 | [diff] [blame] | 69 | std::locale C_locale("C"); |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 70 | std::string_view view{line}; |
| 71 | if (std::all_of(view.begin(), view.end(), |
Jan Kundrát | e387702 | 2018-09-05 15:32:09 +0200 | [diff] [blame] | 72 | [C_locale](const auto c) { return std::isspace(c, C_locale);})) { |
| 73 | continue; |
| 74 | } |
| 75 | |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 76 | try { |
Václav Kubernát | 5b80e52 | 2019-01-25 12:17:03 +0100 | [diff] [blame] | 77 | command_ cmd = parser.parseCommand(line, std::cout); |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 78 | boost::apply_visitor(Interpreter(parser, datastore), cmd); |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 79 | } catch (InvalidCommandException& ex) { |
| 80 | std::cerr << ex.what() << std::endl; |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 81 | } catch (DatastoreException& ex) { |
| 82 | std::cerr << ex.what() << std::endl; |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 83 | } |
Václav Kubernát | 5b80e52 | 2019-01-25 12:17:03 +0100 | [diff] [blame] | 84 | |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 85 | lineEditor.history_add(line); |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 88 | if (historyFile) |
| 89 | lineEditor.history_save(historyFile.value()); |
| 90 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 91 | return 0; |
| 92 | } |