blob: 3a66ee4f1ccccc40a9dc1b4a3a7314c3ca627fb5 [file] [log] [blame]
Jan Kundrátdc2b0722018-03-02 14:13:37 +01001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
Jan Kundráta2740442018-03-22 16:56:43 +01003 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
Jan Kundrátdc2b0722018-03-02 14:13:37 +01004 *
Václav Kubernát624a8872018-03-02 17:28:47 +01005 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
Jan Kundrátdc2b0722018-03-02 14:13:37 +01006 *
7*/
8
Václav Kubernát624a8872018-03-02 17:28:47 +01009#include <boost/spirit/home/x3.hpp>
Jan Kundrátdc2b0722018-03-02 14:13:37 +010010#include <docopt.h>
Václav Kubernát624a8872018-03-02 17:28:47 +010011#include <iostream>
Jan Kundrátdc2b0722018-03-02 14:13:37 +010012#include <string>
13#include "NETCONF_CLI_VERSION.h"
Václav Kubernát48fc3832018-05-28 14:21:22 +020014#include "parser.hpp"
15#include "schema.hpp"
Jan Kundrátdc2b0722018-03-02 14:13:37 +010016
Václav Kubernát624a8872018-03-02 17:28:47 +010017
Jan Kundrátdc2b0722018-03-02 14:13:37 +010018static const char usage[] =
Václav Kubernát624a8872018-03-02 17:28:47 +010019 R"(CLI interface to remote NETCONF hosts
Jan Kundrátdc2b0722018-03-02 14:13:37 +010020
21Usage:
Václav Kubernát624a8872018-03-02 17:28:47 +010022 netconf-cli
Jan Kundrátdc2b0722018-03-02 14:13:37 +010023 netconf-cli (-h | --help)
24 netconf-cli --version
25)";
26
Václav Kubernát624a8872018-03-02 17:28:47 +010027namespace x3 = boost::spirit::x3;
28namespace ascii = boost::spirit::x3::ascii;
29
30using Cmd = std::vector<std::string>;
Václav Kubernát624a8872018-03-02 17:28:47 +010031using ascii::space;
Václav Kubernát48fc3832018-05-28 14:21:22 +020032using x3::_attr;
33using x3::alpha;
34using x3::char_;
35using x3::lexeme;
36using x3::lit;
Václav Kubernát624a8872018-03-02 17:28:47 +010037
Václav Kubernát48fc3832018-05-28 14:21:22 +020038void interpret(cd_ command, Parser& parser)
Václav Kubernát624a8872018-03-02 17:28:47 +010039{
Václav Kubernát48fc3832018-05-28 14:21:22 +020040 parser.changeNode(command.m_path);
Václav Kubernát624a8872018-03-02 17:28:47 +010041}
42
Jan Kundrátdc2b0722018-03-02 14:13:37 +010043int 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át624a8872018-03-02 17:28:47 +010050 std::cout << "Welcome to netconf-cli" << std::endl;
Václav Kubernátff2c9f62018-05-16 20:26:31 +020051
Václav Kubernát48fc3832018-05-28 14:21:22 +020052 Schema schema;
53 schema.addContainer("", "a");
54 schema.addContainer("", "b");
55 schema.addContainer("a", "a2");
56 schema.addContainer("b", "b2");
57 schema.addContainer("a/a2", "a3");
58 schema.addContainer("b/b2", "b3");
59 schema.addList("", "list", {"number"});
60 schema.addContainer("list", "contInList");
61 schema.addList("", "twoKeyList", {"number", "name"});
62 Parser parser(schema);
Václav Kubernátff2c9f62018-05-16 20:26:31 +020063
64 while (true) {
Václav Kubernátf2e463f2018-05-28 15:51:08 +020065 std::cout << parser.currentNode() << "> ";
Václav Kubernátff2c9f62018-05-16 20:26:31 +020066 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);
Václav Kubernát2a9e1792018-05-28 12:53:48 +020073 interpret(command, parser);
Václav Kubernátff2c9f62018-05-16 20:26:31 +020074 } catch (InvalidCommandException& ex) {
75 std::cerr << ex.what() << std::endl;
76 }
77 }
78
Jan Kundrátdc2b0722018-03-02 14:13:37 +010079 return 0;
80}