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 | |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 9 | #include <boost/spirit/home/x3.hpp> |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 10 | #include <docopt.h> |
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 <string> |
| 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 | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 15 | #include "parser.hpp" |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 16 | #include "static_schema.hpp" |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 17 | |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 18 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 19 | static const char usage[] = |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 20 | R"(CLI interface to remote NETCONF hosts |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 21 | |
| 22 | Usage: |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 23 | netconf-cli |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 24 | netconf-cli (-h | --help) |
| 25 | netconf-cli --version |
| 26 | )"; |
| 27 | |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 28 | namespace x3 = boost::spirit::x3; |
| 29 | namespace ascii = boost::spirit::x3::ascii; |
| 30 | |
| 31 | using Cmd = std::vector<std::string>; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 32 | using ascii::space; |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 33 | using x3::_attr; |
| 34 | using x3::alpha; |
| 35 | using x3::char_; |
| 36 | using x3::lexeme; |
| 37 | using x3::lit; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 38 | |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 39 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 40 | int main(int argc, char* argv[]) |
| 41 | { |
| 42 | auto args = docopt::docopt(usage, |
| 43 | {argv + 1, argv + argc}, |
| 44 | true, |
| 45 | "netconf-cli " NETCONF_CLI_VERSION, |
| 46 | true); |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 47 | std::cout << "Welcome to netconf-cli" << std::endl; |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 48 | |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 49 | auto schema = std::make_shared<StaticSchema>(); |
Václav Kubernát | a10d8b6 | 2018-06-13 17:42:46 +0200 | [diff] [blame] | 50 | schema->addContainer("", "a", yang::ContainerTraits::Presence); |
| 51 | schema->addContainer("", "b"); |
| 52 | schema->addLeaf("", "leafString", yang::LeafDataTypes::String); |
| 53 | schema->addLeaf("", "leafDecimal", yang::LeafDataTypes::Decimal); |
| 54 | schema->addLeaf("", "leafBool", yang::LeafDataTypes::Bool); |
| 55 | schema->addLeaf("", "leafInt", yang::LeafDataTypes::Int); |
| 56 | schema->addLeaf("", "leafUint", yang::LeafDataTypes::Uint); |
| 57 | schema->addLeafEnum("", "leafEnum", {"lol", "data", "coze"}); |
| 58 | schema->addContainer("a", "a2"); |
| 59 | schema->addLeaf("a", "leafa", yang::LeafDataTypes::String); |
| 60 | schema->addContainer("b", "b2", yang::ContainerTraits::Presence); |
| 61 | schema->addContainer("a/a2", "a3", yang::ContainerTraits::Presence); |
| 62 | schema->addContainer("b/b2", "b3"); |
| 63 | schema->addList("", "list", {"number"}); |
| 64 | schema->addContainer("list", "contInList", yang::ContainerTraits::Presence); |
| 65 | schema->addList("", "twoKeyList", {"number", "name"}); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 66 | Parser parser(schema); |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 67 | |
| 68 | while (true) { |
Václav Kubernát | f2e463f | 2018-05-28 15:51:08 +0200 | [diff] [blame] | 69 | std::cout << parser.currentNode() << "> "; |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 70 | std::string input; |
| 71 | std::getline(std::cin, input); |
| 72 | if (std::cin.eof()) |
| 73 | break; |
| 74 | |
| 75 | try { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 76 | command_ cmd = parser.parseCommand(input, std::cout); |
Václav Kubernát | a10d8b6 | 2018-06-13 17:42:46 +0200 | [diff] [blame] | 77 | boost::apply_visitor(Interpreter(parser, *schema), cmd); |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 78 | } catch (InvalidCommandException& ex) { |
| 79 | std::cerr << ex.what() << std::endl; |
| 80 | } |
| 81 | } |
| 82 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 83 | return 0; |
| 84 | } |