blob: eaba76a8606699d86a440344b7e165c9024650e8 [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
9#include <docopt.h>
Václav Kubernát2b684612018-08-09 18:55:24 +020010#include <experimental/filesystem>
Václav Kubernát624a8872018-03-02 17:28:47 +010011#include <iostream>
Václav Kubernát5b80e522019-01-25 12:17:03 +010012#include <linenoise.hpp>
Jan Kundrátdc2b0722018-03-02 14:13:37 +010013#include "NETCONF_CLI_VERSION.h"
Václav Kubernát96344a12018-05-28 16:33:39 +020014#include "interpreter.hpp"
Václav Kubernát6415b822018-08-22 17:40:01 +020015#include "sysrepo_access.hpp"
Václav Kubernát2b684612018-08-09 18:55:24 +020016#include "yang_schema.hpp"
Jan Kundrátdc2b0722018-03-02 14:13:37 +010017
Václav Kubernát624a8872018-03-02 17:28:47 +010018
Jan Kundrátdc2b0722018-03-02 14:13:37 +010019static const char usage[] =
Václav Kubernát624a8872018-03-02 17:28:47 +010020 R"(CLI interface to remote NETCONF hosts
Jan Kundrátdc2b0722018-03-02 14:13:37 +010021
22Usage:
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020023 netconf-cli
Jan Kundrátdc2b0722018-03-02 14:13:37 +010024 netconf-cli (-h | --help)
25 netconf-cli --version
26)";
27
Václav Kubernát624a8872018-03-02 17:28:47 +010028
Jan Kundrátdc2b0722018-03-02 14:13:37 +010029int main(int argc, char* argv[])
30{
31 auto args = docopt::docopt(usage,
32 {argv + 1, argv + argc},
33 true,
34 "netconf-cli " NETCONF_CLI_VERSION,
35 true);
Václav Kubernát624a8872018-03-02 17:28:47 +010036 std::cout << "Welcome to netconf-cli" << std::endl;
Václav Kubernátff2c9f62018-05-16 20:26:31 +020037
Václav Kubernát6415b822018-08-22 17:40:01 +020038 SysrepoAccess datastore("netconf-cli");
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020039 Parser parser(datastore.schema());
Václav Kubernát2b684612018-08-09 18:55:24 +020040
Václav Kubernátff2c9f62018-05-16 20:26:31 +020041 while (true) {
Václav Kubernát5b80e522019-01-25 12:17:03 +010042 linenoise::SetHistoryMaxLen(4);
43 linenoise::SetMultiLine(true);
44 std::string line;
45 auto quit = linenoise::Readline((parser.currentNode() + "> ").c_str(), line);
46 if (quit) {
Václav Kubernátff2c9f62018-05-16 20:26:31 +020047 break;
Václav Kubernát5b80e522019-01-25 12:17:03 +010048 }
Václav Kubernátff2c9f62018-05-16 20:26:31 +020049
Jan Kundráte3877022018-09-05 15:32:09 +020050 std::locale C_locale("C");
Václav Kubernát5b80e522019-01-25 12:17:03 +010051 if (std::all_of(line.begin(), line.end(),
Jan Kundráte3877022018-09-05 15:32:09 +020052 [C_locale](const auto c) { return std::isspace(c, C_locale);})) {
53 continue;
54 }
55
Václav Kubernátff2c9f62018-05-16 20:26:31 +020056 try {
Václav Kubernát5b80e522019-01-25 12:17:03 +010057 command_ cmd = parser.parseCommand(line, std::cout);
Václav Kubernát6415b822018-08-22 17:40:01 +020058 boost::apply_visitor(Interpreter(parser, datastore), cmd);
Václav Kubernátff2c9f62018-05-16 20:26:31 +020059 } catch (InvalidCommandException& ex) {
60 std::cerr << ex.what() << std::endl;
61 }
Václav Kubernát5b80e522019-01-25 12:17:03 +010062
63 linenoise::AddHistory(line.c_str());
Václav Kubernátff2c9f62018-05-16 20:26:31 +020064 }
65
Jan Kundrátdc2b0722018-03-02 14:13:37 +010066 return 0;
67}