-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
167 lines (125 loc) · 4.91 KB
/
Copy pathmain.cpp
File metadata and controls
167 lines (125 loc) · 4.91 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include "CLI/CLI.hpp"
#include <vector>
#include <fstream>
#include <cstdlib>
#include <map>
#include <cassert>
#include <stdexcept>
#include "StatusCode.hpp"
#include "nlohmann/json.hpp"
#include <typeinfo>
#include <cstdio>
#include <catch2/catch.hpp>
#include "src/Utils.hpp"
#include "src/Table.hpp"
#define DATABASE_PATH "database/database.json"
#define MAX_ROW_LENGTH 30
using Json = nlohmann::json;
std::string& limitStringLength(std::string&& str){
std::string tmp;
std::stringstream ss(str);
std::vector<std::string> res;
int count = 0;
while( getline(ss,tmp,' ') ){
if( ++count%MAX_ROW_LENGTH == 0){
res.push_back("\n");
}else{
res.push_back( tmp + " " );
}
}
str.clear();
for(auto const& st : res){
str+= st;
}
return str;
}
template <typename T>
void display(StatusCode& s,const T& json){
s.setCode(json["code"]);
s.setShortDesc(json["shortDescr"]);
s.setDesc(json["description"]);
s.setDetails(json["details"]);
s.setCategory(json["category"]);
CliTable::Options opt;
CliTable::TableBody content = {
{ "Code" , s.getCode() },
{ "Category" , s.getCategory() },
{ "Short Description" , s.getShortDesc() },
{ " Description" , limitStringLength( s.getDesc()) },
{ "Details" , limitStringLength( s.getDetails() ) },
};
CliTable::Table table(opt,content);
//Generating the final table
table.generate();
}
void displayCategory(const std::string& category, const Json& json){
auto codes = json[category][0];
for(auto const& codeCategory: codes){
StatusCode statusCode;
display<decltype(codeCategory)>(statusCode,codeCategory);
}
}
int main(int argc,char** argv) {
//auto envPath = getenv(DATABASE_PATH);
const std::map<char,std::string> categoryValues = {
{'1',"informational"},
{'2',"success"},
{'3',"redirection"},
{'4',"clientError"},
{'5',"serverError"}
};
std::ifstream database(DATABASE_PATH);
Json json;
// Reading the database into json
database >> json ;
CLI::App app{"Get http status codes meaning right in your terminal ! "};
app.require_subcommand(1);
bool bInformational,bSuccess,bRedirection,bClient,bServer;
auto code = app.add_subcommand("code","Retrieves the meaning of the specified code");
code->add_flag("-i,--informational",bInformational,"Displays Informational codes and their meanings");
code->add_flag("-s,--success",bSuccess,"Displays Success codes and their meanings");
code->add_flag("-r,--redirection",bRedirection,"Displays Success codes and their meanings");
code->add_flag("-c,--client-error",bClient,"Displays Client error codes and their meanings");
code->add_flag("-e,--server-error",bServer,"Displays Server error codes and their meanings");
std::string sCode;
code->add_option("code",
sCode,
"The code which the meaning should be displayed");
code->callback([&](){
if(sCode.empty()) {
// Either it's an error or we want to display for a specific category
if(bInformational) displayCategory("informational",json);
if(bSuccess) displayCategory("success",json);
if(bRedirection) displayCategory("redirection",json);
if(bClient) displayCategory("clientError",json);
if(bServer) displayCategory("serverError",json);
}else {
// Reading information about the code from the Json
// If the size of the sCode is greater than 3 then bad code
if( sCode.size() > 3){
std::cerr << "The specified code is invalid :(" << std::endl;
return;
}
try{
//Looking for the code's category inside the map
std::string category;
auto it = categoryValues.find(sCode.at(0));
if( it != categoryValues.end() ){
// Now we know the category to which the code belongs to
category = it->second;
auto foundCode = json[category][0][sCode];
StatusCode statusCode;
display<decltype(foundCode)>(statusCode,foundCode);
}else{
std::cerr << "The specified code is invalid "<< std::endl ;
}
}catch(const std::exception& e){
std::cerr << "Cannot open the file" << std::endl;
}
}
});
CLI11_PARSE(app, argc, argv);
std::cout << "\n"<< std::endl;
std::cout<< "Information took from https://httpstatuses.com/" << std::endl;
}