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> |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 13 | #include "CParser.hpp" |
| 14 | #include "CTree.hpp" |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 15 | #include "NETCONF_CLI_VERSION.h" |
| 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 | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 22 | netconf-cli |
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 | namespace x3 = boost::spirit::x3; |
| 28 | namespace ascii = boost::spirit::x3::ascii; |
| 29 | |
| 30 | using Cmd = std::vector<std::string>; |
| 31 | using x3::alpha; |
| 32 | using x3::lit; |
| 33 | using x3::char_; |
| 34 | using x3::_attr; |
| 35 | using x3::lexeme; |
| 36 | using ascii::space; |
| 37 | |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 38 | void interpret(cd_ command, CTree& tree) |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 39 | { |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 40 | tree.changeNode(command.m_path); |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 43 | int main(int argc, char* argv[]) |
| 44 | { |
| 45 | auto args = docopt::docopt(usage, |
| 46 | {argv + 1, argv + argc}, |
| 47 | true, |
| 48 | "netconf-cli " NETCONF_CLI_VERSION, |
| 49 | true); |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 50 | std::cout << "Welcome to netconf-cli" << std::endl; |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 51 | |
| 52 | CTree tree; |
| 53 | tree.addContainer("", "a"); |
| 54 | tree.addContainer("", "b"); |
| 55 | tree.addContainer("a", "a2"); |
| 56 | tree.addContainer("b", "b2"); |
| 57 | tree.addContainer("a/a2", "a3"); |
| 58 | tree.addContainer("b/b2", "b3"); |
| 59 | tree.addList("", "list", {"number"}); |
| 60 | tree.addContainer("list", "contInList"); |
| 61 | tree.addList("", "twoKeyList", {"number", "name"}); |
| 62 | CParser parser(tree); |
| 63 | |
| 64 | while (true) { |
| 65 | std::cout << tree.currentNode() << ">"; |
| 66 | std::string input; |
| 67 | std::getline(std::cin, input); |
| 68 | if (std::cin.eof()) |
| 69 | break; |
| 70 | |
| 71 | try { |
| 72 | cd_ command = parser.parseCommand(input, std::cout); |
| 73 | interpret(command, tree); |
| 74 | } catch (InvalidCommandException& ex) { |
| 75 | std::cerr << ex.what() << std::endl; |
| 76 | } |
| 77 | } |
| 78 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 79 | return 0; |
| 80 | } |