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