LCOV - code coverage report
Current view: top level - src/tools/kdb - set.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 36 39 92.3 %
Date: 2019-09-12 12:28:41 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include <set.hpp>
      10             : 
      11             : #include <cmdline.hpp>
      12             : #include <kdb.hpp>
      13             : #include <kdbio.hpp>
      14             : 
      15             : #include <iostream>
      16             : 
      17             : using namespace std;
      18             : using namespace kdb;
      19             : 
      20         430 : SetCommand::SetCommand ()
      21             : {
      22         430 : }
      23             : 
      24         352 : int SetCommand::execute (Cmdline const & cl)
      25             : {
      26         704 :         int argc = cl.arguments.size ();
      27         352 :         if (argc != 1 && argc != 2)
      28             :         {
      29           0 :                 throw invalid_argument ("1 or 2 arguments needed");
      30             :         }
      31             : 
      32             :         bool nullValue;
      33         704 :         std::string value;
      34             : 
      35         352 :         if (argc == 2)
      36             :         {
      37         334 :                 nullValue = false;
      38         668 :                 value = cl.arguments[1];
      39             :         }
      40             :         else
      41             :         {
      42             :                 nullValue = true;
      43             :         }
      44             : 
      45         704 :         KeySet conf;
      46         704 :         Key k = cl.createKey (0);
      47         704 :         std::string name = k.getName ();
      48             : 
      49             :         // do not resume on any get errors
      50             :         // otherwise the user might break
      51             :         // the config
      52         352 :         kdb.get (conf, k);
      53             : 
      54         352 :         if (name[0] == '/')
      55             :         {
      56             :                 // fix name for lookup
      57         108 :                 name = cl.ns + name;
      58         162 :                 if (!cl.quiet) std::cout << "Using name " << name << std::endl;
      59             : 
      60             :                 // fix k for kdb.set later
      61          54 :                 k.setName (name);
      62             :         }
      63             : 
      64         704 :         Key key = conf.lookup (name);
      65             : 
      66         704 :         std::ostringstream toprint;
      67         352 :         if (!key)
      68             :         {
      69         584 :                 toprint << "Create a new key " << name;
      70        1168 :                 key = Key (name, KEY_END);
      71         292 :                 if (!nullValue)
      72             :                 {
      73        1096 :                         toprint << " with string \"" << value << '"' << endl;
      74         822 :                         key.setString (value);
      75             :                 }
      76             :                 else
      77             :                 {
      78          36 :                         toprint << " with null value" << endl;
      79             :                         key.setBinary (nullptr, 0);
      80             :                 }
      81         292 :                 if (!key.isValid ())
      82             :                 {
      83           0 :                         cerr << "no valid name supplied" << endl;
      84             :                         return 1;
      85             :                 }
      86             :                 conf.append (key);
      87             :         }
      88             :         else
      89             :         {
      90          60 :                 if (!nullValue)
      91             :                 {
      92         240 :                         toprint << "Set string to \"" << value << '"' << endl;
      93         180 :                         key.setString (value);
      94             :                 }
      95             :                 else
      96             :                 {
      97           0 :                         toprint << "Set null value" << endl;
      98             :                         key.setBinary (nullptr, 0);
      99             :                 }
     100             :         }
     101         352 :         kdb.set (conf, k);
     102         326 :         printWarnings (cerr, k, cl.verbose, cl.debug);
     103         326 :         printError (cerr, k, cl.verbose, cl.debug);
     104             : 
     105        1304 :         if (!cl.quiet) cout << toprint.str ();
     106             : 
     107             :         return 0;
     108             : }
     109             : 
     110         860 : SetCommand::~SetCommand ()
     111             : {
     112        7594 : }

Generated by: LCOV version 1.13