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 | */ |
| 8 | |
| 9 | #include <docopt.h> |
Václav Kubernát | 2b68461 | 2018-08-09 18:55:24 +0200 | [diff] [blame] | 10 | #include <experimental/filesystem> |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 11 | #include <iostream> |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 12 | #include "NETCONF_CLI_VERSION.h" |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 13 | #include "interpreter.hpp" |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 14 | #include "sysrepo_access.hpp" |
Václav Kubernát | 2b68461 | 2018-08-09 18:55:24 +0200 | [diff] [blame] | 15 | #include "yang_schema.hpp" |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 16 | |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 17 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 18 | static const char usage[] = |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 19 | R"(CLI interface to remote NETCONF hosts |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 20 | |
| 21 | Usage: |
Václav Kubernát | 2b68461 | 2018-08-09 18:55:24 +0200 | [diff] [blame] | 22 | netconf-cli <path-to-yang-schema>... |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 23 | netconf-cli (-h | --help) |
| 24 | netconf-cli --version |
| 25 | )"; |
| 26 | |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 27 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 28 | int main(int argc, char* argv[]) |
| 29 | { |
| 30 | auto args = docopt::docopt(usage, |
| 31 | {argv + 1, argv + argc}, |
| 32 | true, |
| 33 | "netconf-cli " NETCONF_CLI_VERSION, |
| 34 | true); |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 35 | std::cout << "Welcome to netconf-cli" << std::endl; |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 36 | |
Václav Kubernát | 2b68461 | 2018-08-09 18:55:24 +0200 | [diff] [blame] | 37 | auto yangschema = std::make_shared<YangSchema>(); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 38 | Parser parser(yangschema); |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 39 | |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 40 | SysrepoAccess datastore("netconf-cli"); |
| 41 | |
Václav Kubernát | 2b68461 | 2018-08-09 18:55:24 +0200 | [diff] [blame] | 42 | for (auto it : args.at("<path-to-yang-schema>").asStringList()) { |
| 43 | auto dir = std::experimental::filesystem::absolute(it).remove_filename(); |
| 44 | yangschema->addSchemaDirectory(dir.c_str()); |
| 45 | yangschema->addSchemaFile(it.c_str()); |
| 46 | } |
| 47 | |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 48 | while (true) { |
Václav Kubernát | f2e463f | 2018-05-28 15:51:08 +0200 | [diff] [blame] | 49 | std::cout << parser.currentNode() << "> "; |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 50 | std::string input; |
| 51 | std::getline(std::cin, input); |
| 52 | if (std::cin.eof()) |
| 53 | break; |
| 54 | |
Jan Kundrát | e387702 | 2018-09-05 15:32:09 +0200 | [diff] [blame^] | 55 | std::locale C_locale("C"); |
| 56 | if (std::all_of(input.begin(), input.end(), |
| 57 | [C_locale](const auto c) { return std::isspace(c, C_locale);})) { |
| 58 | continue; |
| 59 | } |
| 60 | |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 61 | try { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 62 | command_ cmd = parser.parseCommand(input, std::cout); |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 63 | boost::apply_visitor(Interpreter(parser, datastore), cmd); |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 64 | } catch (InvalidCommandException& ex) { |
| 65 | std::cerr << ex.what() << std::endl; |
| 66 | } |
| 67 | } |
| 68 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 69 | return 0; |
| 70 | } |