-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
171 lines (139 loc) · 4.71 KB
/
main.cpp
File metadata and controls
171 lines (139 loc) · 4.71 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
168
169
170
171
#include <iostream>
#include <string>
#include <fstream>
#include "models/Deeplink.h"
#include "models/Configuration.h"
#include "libs/json.hpp"
namespace fs = std::filesystem;
using json = nlohmann::json;
const std::string VERSION = "0.0.1";
const std::string COMMAND_CLEAN = "clean";
const std::string COMMAND_GENERATE = "generate";
const std::string COMMAND_VERSION = "version";
const std::string PARAM_URL = "{{URL}}";
Configuration readConfiguration() {
std::ifstream file("config.json");
Configuration config;
if (!file.is_open()) {
std::cerr << "Failed to open the JSON file." << std::endl;
return config;
}
json jsonData;
file >> jsonData;
config.outputFolder = jsonData["output_folder"];
for (const auto& deeplinkData : jsonData["deeplinks"]) {
Deeplink deeplink;
deeplink.templateName = deeplinkData["template"];
deeplink.deeplink = deeplinkData["deeplink"];
deeplink.url = deeplinkData["url"];
config.deeplinks.push_back(deeplink);
}
return config;
}
std::string getTemplateContents(const Deeplink& deeplink) {
try {
std::string templateFilePath = "templates/" + deeplink.templateName + ".template";
std::ifstream templateFile(templateFilePath);
if (!templateFile.is_open()) {
std::cerr << "Error: Unable to open template file: " << templateFilePath << std::endl;
return "";
}
std::stringstream buffer;
buffer << templateFile.rdbuf();
std::string templateContents = buffer.str();
templateFile.close();
size_t urlPos = templateContents.find(PARAM_URL);
while (urlPos != std::string::npos) {
templateContents.replace(urlPos, PARAM_URL.length(), deeplink.url);
urlPos = templateContents.find(PARAM_URL, urlPos + 1);
}
return templateContents;
} catch (const std::exception& e) {
return "";
}
}
bool clean(const std::string& path) {
try {
if (!fs::exists(path)) {
return false;
}
if (!fs::is_directory(path)) {
return false;
}
fs::remove_all(path);
return true;
} catch (const std::exception& e) {
return false;
}
}
bool createDirectory(const std::string& path) {
try {
if (fs::exists(path)) {
return false;
}
fs::create_directory(path);
return true;
} catch (const std::exception& e) {
return false;
}
}
bool generate(const Configuration& config) {
try {
createDirectory(config.outputFolder);
for (const auto& deeplink : config.deeplinks) {
std::string deeplinkPath = config.outputFolder + "/" + deeplink.deeplink;
createDirectory(deeplinkPath);
std::string indexPath = deeplinkPath + "/index.html";
std::string templateContent = getTemplateContents(deeplink);
std::ofstream outputFile(indexPath);
if (outputFile.is_open()) {
outputFile << templateContent;
outputFile.close();
}
}
return true;
} catch (const std::exception& e) {
return false;
}
}
void printUsage(char* argv[]) {
std::cerr << "Usage: " << argv[0] << " command [--verbose] [--help]" << std::endl;
std::cerr << std::endl;
std::cerr << "Commands: " << std::endl;
std::cerr << " clean Cleans previously generated static redirect sitemap files." << std::endl;
std::cerr << " generate Generates a deeplink redirect sitemap files based on config." << std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
printUsage(argv);
return 1;
}
std::string command = argv[1];
if (command == COMMAND_VERSION) {
std::cout << "GitHub Pages Deeplink generator v" << VERSION << std::endl;
return 0;
}
if (command == COMMAND_CLEAN) {
Configuration config = readConfiguration();
if (clean(config.outputFolder)) {
return 0;
}
std::cerr << "Error cleaning ouptut folder: " << config.outputFolder << std::endl;
return 1;
}
if (command == COMMAND_GENERATE) {
Configuration config = readConfiguration();
if (fs::exists(config.outputFolder)) {
std::cerr << "Seems like output folder already exists: " << config.outputFolder << std::endl;
std::cerr << "Try to run: gdeeplink clean" << std::endl;
return 1;
}
if (generate(config)) {
return 0;
}
std::cerr << "Error generating deeplinks: " << std::endl;
return 1;
}
std::cerr << "Unknown command: " << command << std::endl;
return 1;
}