This repository was archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcfgedit.cpp
More file actions
148 lines (121 loc) · 3.51 KB
/
cfgedit.cpp
File metadata and controls
148 lines (121 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
// libircbot irc::bot::
#include "ircbot/bot.h"
using namespace irc::bot;
static
void wait_anykey()
{
std::cout << "Press any key to continue or ctrl-c to quit..." << std::endl;
std::cin.get();
}
int main(int argc, char **argv) try
{
Opts opts;
const int nargs(opts.parse({argv+1,argv+argc}));
if(argc - nargs < 2)
{
std::cerr << "Usage: " << argv[0] << " [--dbdir=db --db=ircbot] <dbkey | *> [key] [= [val...]]\n"
<< "\t- dbkey is the channel or account name, i.e \"#SPQF\" or foobar.\n"
<< "\t- key is the fully qualified JSON key, i.e config.vote.duration\n"
<< "\t- Omitting value after = is a deletion of this key.\n"
<< "\t- Omitting key prints the whole document.\n"
<< "\t- Using \"*\" as dbkey prints the whole database.\n"
<< "\t- Multiple vals become a JSON array.\n"
<< "\t- Multiple vals in quotes become a single string.\n";
return -1;
}
std::cout << "Using configuration: " << std::endl << opts << std::endl << std::endl;
const std::string db { opts.count("db")? opts["db"] : "ircbot" };
const std::string dockey { tolower(argv[nargs+1]) };
const std::string key { argc > nargs+2? argv[nargs+2] : "" };
const std::string eq { argc > nargs+3? argv[nargs+3] : "" };
const std::vector<std::string> vals
{
argv + (argc > nargs + 4? nargs + 4 : 0),
argv + (argc > nargs + 4? nargs + argc : 0)
};
std::cout << "db [" << db << "]" << std::endl;
std::cout << "doc [" << dockey << "]" << std::endl;
std::cout << "key [" << key << "]" << std::endl;
for(const auto &val : vals)
std::cout << "val [" << val << "]" << std::endl;
if(!eq.empty() && eq != "=")
throw Exception("Invalid syntax (missing equal sign)");
// Open database
Adb adb(opts["dbdir"] + "/" + db);
irc::bot::adb = &adb;
// Read Iteration
if(dockey == "*")
{
std::for_each(adb.begin(),adb.end(),[]
(const auto &kv)
{
std::cout << "[" << kv.first << "] => " << std::endl;
std::cout << kv.second << std::endl << std::endl;
});
return 0;
}
// Creation warning
if(!adb.exists(dockey))
{
std::cout << "Document not found in database." << std::endl;
if(key.empty())
return -1;
std::cout << "It will be created." << std::endl;
wait_anykey();
}
// Deletion warning
if(!eq.empty() && vals.empty())
{
std::cout << "DELETING THE KEY [" << key << "] INSIDE [" << dockey << "]" << std::endl;
wait_anykey();
}
Acct acct(&dockey);
// Read whole document
if(key.empty() && eq.empty() && vals.empty())
{
const Adoc doc(acct.get());
std::cout << doc << std::endl;
return 0;
}
// Read document by key
if(eq.empty() && vals.empty())
{
const Adoc doc(acct.get(key));
std::cout << doc << std::endl;
return 0;
}
// Delete document
if(vals.empty())
{
Adoc doc(acct.get());
if(!doc.remove(key))
throw Exception("Failed to remove key: not found");
acct.set(doc);
std::cout << "Removed." << std::endl;
return 0;
}
// Overwrite whole document (disabled)
if(key.empty())
throw Exception("Specify a key");
// Set key to value
if(vals.size() == 1)
{
acct.set_val(key,vals.at(0));
std::cout << "Done." << std::endl;
return 0;
}
// Set key to array
acct.set_val(key,vals.begin(),vals.end());
return 0;
}
catch(const Exception &e)
{
std::cerr << "Error: " << e << std::endl;
return -1;
}