Repository navigation
Expand file tree
/
Copy pathui.cpp
More file actions
290 lines (244 loc) · 7.71 KB
/
Copy pathui.cpp
File metadata and controls
290 lines (244 loc) · 7.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "ui.hpp"
#include "error.hpp"
#include "logger.hpp"
#include "settings.hpp"
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
namespace dynamic_file_binder {
namespace {
std::string FormatTime(std::time_t value)
{
std::tm tmValue{};
#if defined(_WIN32)
localtime_s(&tmValue, &value);
#else
localtime_r(&value, &tmValue);
#endif
std::ostringstream stream;
stream << std::put_time(&tmValue, "%Y-%m-%d %H:%M:%S");
return stream.str();
}
void PrintSeparator()
{
std::cout << "------------------------------------------------------------\n";
}
} // namespace
ConsoleUi::ConsoleUi(FileBinder binder)
: binder_(std::move(binder))
{
}
void ConsoleUi::PrintHeader() const
{
PrintSeparator();
std::cout << " DynamicFileBinder - CRUD Console UI\n";
PrintSeparator();
}
void ConsoleUi::PrintMenu() const
{
std::cout << "1) Create binding\n"
<< "2) Read bindings\n"
<< "3) Update binding\n"
<< "4) Delete binding\n"
<< "5) Mark binding as used\n"
<< "6) Optimize storage\n"
<< "7) Revert all bindings\n"
<< "8) Cleanup workspace\n"
<< "0) Exit\n";
}
std::string ConsoleUi::ReadLine(const std::string& prompt)
{
std::cout << prompt;
std::string value;
std::getline(std::cin >> std::ws, value);
return value;
}
bool ConsoleUi::AskYesNo(const std::string& prompt, bool defaultValue)
{
const std::string suffix = defaultValue ? " [Y/n]: " : " [y/N]: ";
const std::string answer = ReadLine(prompt + suffix);
if (answer.empty()) {
return defaultValue;
}
const char first = static_cast<char>(std::tolower(static_cast<unsigned char>(answer.front())));
return first == 'y';
}
std::filesystem::path ConsoleUi::ParsePath(const std::string& value)
{
return std::filesystem::path(value);
}
std::string ConsoleUi::ToString(bool value)
{
return value ? "symbolic" : "physical";
}
void ConsoleUi::ShowLastError() const
{
if (const auto lastError = ErrorManager::Instance().Last(); lastError.has_value()) {
std::cout << ErrorManager::Instance().Format(*lastError) << '\n';
}
}
void ConsoleUi::PrintBindings() const
{
const std::vector<FileInfo> bindings = binder_.ListBindings();
PrintSeparator();
std::cout << "Tracked bindings: " << bindings.size() << '\n';
if (bindings.empty()) {
std::cout << "No bindings found.\n";
return;
}
for (std::size_t index = 0; index < bindings.size(); ++index) {
const FileInfo& binding = bindings[index];
std::cout << '[' << index + 1 << "] "
<< binding.localPath.string() << " -> " << binding.sourcePath.string() << '\n'
<< " type=" << binding.fileType
<< ", mode=" << ToString(binding.isSymbolicLink)
<< ", lastUsed=" << FormatTime(binding.lastUsed)
<< ", size=" << binding.fileSize << " bytes\n";
}
}
void ConsoleUi::CreateBinding()
{
const std::string sourceInput = ReadLine("Source path: ");
if (sourceInput.empty()) {
std::cout << "Source path is required.\n";
return;
}
const std::string targetInput = ReadLine("Target path (leave empty for automatic): ");
const std::string modeInput = ReadLine("Mode [symlink/hardlink/physical] (default: auto): ");
Settings settings;
FileInfo info;
info.sourcePath = ParsePath(sourceInput);
info.localPath = targetInput.empty() ? settings.GetCustomSyncPath(info.sourcePath) : ParsePath(targetInput);
info.fileSize = std::filesystem::is_regular_file(info.sourcePath) ? static_cast<std::size_t>(std::filesystem::file_size(info.sourcePath)) : 0;
info.fileType = info.sourcePath.extension().string();
info.lastUsed = std::time(nullptr);
const bool forcePhysical = modeInput == "physical";
if (forcePhysical) {
info.isSymbolicLink = false;
}
if (!binder_.BindFile(info, forcePhysical)) {
std::cout << "Create failed.\n";
ShowLastError();
return;
}
std::cout << "Binding created successfully.\n";
}
void ConsoleUi::UpdateBinding()
{
PrintBindings();
const std::string currentTarget = ReadLine("Current target path: ");
if (currentTarget.empty()) {
return;
}
const std::string newTarget = ReadLine("New target path: ");
if (newTarget.empty()) {
std::cout << "New target path is required.\n";
return;
}
const std::string modeInput = ReadLine("Mode [symlink/physical] (default: keep auto): ");
const bool forcePhysical = modeInput == "physical";
if (!binder_.UpdateBinding(ParsePath(currentTarget), ParsePath(newTarget), forcePhysical)) {
std::cout << "Update failed.\n";
ShowLastError();
return;
}
std::cout << "Binding updated successfully.\n";
}
void ConsoleUi::DeleteBinding()
{
PrintBindings();
const std::string targetInput = ReadLine("Target path to delete: ");
if (targetInput.empty()) {
return;
}
if (!AskYesNo("Delete this binding?")) {
return;
}
if (!binder_.RemoveBinding(ParsePath(targetInput))) {
std::cout << "Delete failed.\n";
ShowLastError();
return;
}
std::cout << "Binding deleted successfully.\n";
}
void ConsoleUi::MarkBindingAsUsed()
{
PrintBindings();
const std::string targetInput = ReadLine("Target path to mark as used: ");
if (targetInput.empty()) {
return;
}
if (!binder_.MarkFileAsUsed(ParsePath(targetInput))) {
std::cout << "Mark-as-used failed.\n";
ShowLastError();
return;
}
std::cout << "Binding marked as used.\n";
}
void ConsoleUi::HandleOptimizeStorage()
{
Settings settings;
const std::string input = ReadLine("Max local storage in MB (default: " + std::to_string(settings.GetMaxLocalStorageMB()) + "): ");
std::size_t threshold = settings.GetMaxLocalStorageMB();
if (!input.empty()) {
threshold = static_cast<std::size_t>(std::stoull(input));
}
binder_.OptimizeStorage(threshold);
std::cout << "Optimization completed.\n";
}
int ConsoleUi::Run()
{
while (true) {
PrintHeader();
PrintMenu();
const std::string choice = ReadLine("Choice: ");
if (choice == "0") {
return 0;
}
if (choice == "1") {
CreateBinding();
} else if (choice == "2") {
PrintBindings();
} else if (choice == "3") {
UpdateBinding();
} else if (choice == "4") {
DeleteBinding();
} else if (choice == "5") {
MarkBindingAsUsed();
} else if (choice == "6") {
HandleOptimizeStorage();
} else if (choice == "7") {
if (AskYesNo("Revert all bindings and delete .sccache?")) {
if (binder_.RevertConnection()) {
std::cout << "All bindings reverted.\n";
} else {
std::cout << "Revert failed.\n";
ShowLastError();
}
}
} else if (choice == "8") {
if (AskYesNo("Cleanup workspace?")) {
if (binder_.Cleanup()) {
std::cout << "Workspace cleaned up.\n";
} else {
std::cout << "Cleanup failed.\n";
ShowLastError();
}
}
} else {
std::cout << "Unknown choice.\n";
}
ReadLine("Press Enter to continue...");
}
}
#if !defined(DFB_HAS_WXWIDGETS)
int RunPreferredUi(FileBinder binder)
{
ConsoleUi ui(std::move(binder));
return ui.Run();
}
#endif
} // namespace dynamic_file_binder