-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1312 lines (1158 loc) · 48.8 KB
/
main.cpp
File metadata and controls
1312 lines (1158 loc) · 48.8 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <array>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <thread>
#include <utility>
#include <vector>
#include <ftxui/component/component.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/dom/elements.hpp>
class DockerManager {
public:
struct ContainerInfo {
std::string id;
std::string name;
std::string status;
std::string ports;
bool running = false;
};
std::vector<ContainerInfo> getContainerList() const;
std::vector<std::pair<std::string, std::string>> getImageList() const;
bool pullImage(const std::string& image, std::string& message) const;
bool runContainerInteractive(const std::string& image,
const std::vector<std::string>& ports,
std::string& message) const;
bool startInteractive(const std::string& containerId, std::string& message) const;
bool startDetached(const std::string& containerId, std::string& message) const;
bool deleteImage(const std::string& imageId, std::string& message) const;
bool stopContainer(const std::string& containerId, std::string& message) const;
bool removeContainer(const std::string& containerId, std::string& message) const;
bool execShell(const std::string& containerId, std::string& message) const;
bool execDetachedCommand(const std::string& containerId,
const std::string& command,
std::string& message) const;
bool spinUpMySQL(const std::string& port,
const std::string& password,
const std::string& version,
std::string& message) const;
bool showContainerIP(const std::string& containerId, std::string& message) const;
bool createDockerfile(const std::string& baseImage,
const std::string& bashScriptPath,
std::string outputFile,
const std::string& imageName,
std::string& message) const;
private:
static bool runCommand(const std::string& cmd, bool quiet = true);
static std::string shellEscape(const std::string& value);
};
bool DockerManager::runCommand(const std::string& cmd, bool quiet) {
std::string command = cmd;
if (quiet) {
command += " > /dev/null 2>&1";
}
const int code = std::system(command.c_str());
return code == 0;
}
std::string DockerManager::shellEscape(const std::string& value) {
std::string escaped = "'";
for (char c : value) {
if (c == '\'') {
escaped += "'\\''";
} else {
escaped += c;
}
}
escaped += "'";
return escaped;
}
std::vector<std::pair<std::string, std::string>> DockerManager::getImageList() const {
std::vector<std::pair<std::string, std::string>> images;
std::array<char, 256> buffer{};
std::string result;
FILE* pipe = popen("docker images --format '{{.ID}} {{.Repository}}:{{.Tag}}'", "r");
if (!pipe) {
return images;
}
while (fgets(buffer.data(), static_cast<int>(buffer.size()), pipe) != nullptr) {
result = buffer.data();
std::stringstream ss(result);
std::string id;
std::string repoTag;
ss >> id >> repoTag;
if (!id.empty() && !repoTag.empty()) {
images.emplace_back(id, repoTag);
}
}
pclose(pipe);
return images;
}
std::vector<DockerManager::ContainerInfo> DockerManager::getContainerList() const {
std::vector<ContainerInfo> containers;
std::array<char, 256> buffer{};
std::string result;
FILE* pipe = popen("docker ps -a --format '{{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}'", "r");
if (!pipe) {
return containers;
}
while (fgets(buffer.data(), static_cast<int>(buffer.size()), pipe) != nullptr) {
result = buffer.data();
while (!result.empty() && (result.back() == '\n' || result.back() == '\r')) {
result.pop_back();
}
std::stringstream ss(result);
ContainerInfo info;
std::getline(ss, info.id, '\t');
std::getline(ss, info.name, '\t');
std::getline(ss, info.status, '\t');
std::getline(ss, info.ports);
if (!info.id.empty() && !info.name.empty()) {
info.running = info.status.rfind("Up", 0) == 0;
containers.push_back(info);
}
}
pclose(pipe);
return containers;
}
bool DockerManager::pullImage(const std::string& image, std::string& message) const {
if (image.empty()) {
message = "Please provide an image name.";
return false;
}
const bool ok = runCommand("docker pull " + shellEscape(image));
message = ok ? "Image pulled successfully." : "Could not pull that image.";
return ok;
}
bool DockerManager::runContainerInteractive(const std::string& image,
const std::vector<std::string>& ports,
std::string& message) const {
if (image.empty()) {
message = "Please choose an image first.";
return false;
}
std::string cmd = "docker run -it ";
for (const auto& port : ports) {
cmd += "-p " + shellEscape(port) + " ";
}
cmd += shellEscape(image) + " /bin/sh";
const bool ok = runCommand(cmd, false);
message = ok ? "Interactive container session finished." : "Could not start that container session.";
return ok;
}
bool DockerManager::startInteractive(const std::string& containerId, std::string& message) const {
const bool ok = runCommand("docker start -ai " + shellEscape(containerId), false);
message = ok ? "Interactive container session finished." : "Could not start that container interactively.";
return ok;
}
bool DockerManager::startDetached(const std::string& containerId, std::string& message) const {
const bool ok = runCommand("docker start " + shellEscape(containerId));
message = ok ? "Container started in detached mode." : "Could not start that container.";
return ok;
}
bool DockerManager::deleteImage(const std::string& imageId, std::string& message) const {
const bool ok = runCommand("docker rmi " + shellEscape(imageId));
message = ok ? "Image deleted." : "Could not delete that image. It may still be in use.";
return ok;
}
bool DockerManager::stopContainer(const std::string& containerId, std::string& message) const {
const bool ok = runCommand("docker stop " + shellEscape(containerId));
message = ok ? "Container stopped." : "Could not stop that container.";
return ok;
}
bool DockerManager::removeContainer(const std::string& containerId, std::string& message) const {
const bool ok = runCommand("docker rm " + shellEscape(containerId));
message = ok ? "Container removed." : "Could not remove that container.";
return ok;
}
bool DockerManager::execShell(const std::string& containerId, std::string& message) const {
const bool ok = runCommand("docker exec -it " + shellEscape(containerId) + " /bin/sh", false);
message = ok ? "Shell session finished." : "Could not open a shell in that container.";
return ok;
}
bool DockerManager::execDetachedCommand(const std::string& containerId,
const std::string& command,
std::string& message) const {
if (command.empty()) {
message = "Please provide a command to run.";
return false;
}
const std::string cmd = "docker exec -d " + shellEscape(containerId) + " /bin/sh -c " +
shellEscape(command);
const bool ok = runCommand(cmd);
message = ok ? "Command dispatched in detached mode." : "Could not run that command.";
return ok;
}
bool DockerManager::spinUpMySQL(const std::string& port,
const std::string& password,
const std::string& version,
std::string& message) const {
const std::string cmd = "docker run -p " + shellEscape(port) +
" --name mysql-container -e MYSQL_ROOT_PASSWORD=" +
shellEscape(password) + " -d " + shellEscape("mysql:" + version);
const bool ok = runCommand(cmd);
message = ok ? "MySQL container launched." : "Could not launch MySQL. Check the version and port mapping.";
return ok;
}
bool DockerManager::showContainerIP(const std::string& containerId, std::string& message) const {
const std::string command = "docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' " +
shellEscape(containerId);
std::array<char, 128> buffer{};
std::string ip;
FILE* pipe = popen(command.c_str(), "r");
if (!pipe) {
message = "Could not inspect that container.";
return false;
}
if (fgets(buffer.data(), static_cast<int>(buffer.size()), pipe) != nullptr) {
ip = buffer.data();
}
pclose(pipe);
while (!ip.empty() && (ip.back() == '\n' || ip.back() == '\r' || ip.back() == ' ' || ip.back() == '\t')) {
ip.pop_back();
}
if (ip.empty()) {
message = "No IP address found. The container may be stopped.";
return false;
}
message = "Container IP address: " + ip;
return true;
}
bool DockerManager::createDockerfile(const std::string& baseImage,
const std::string& bashScriptPath,
std::string outputFile,
const std::string& imageName,
std::string& message) const {
if (baseImage.empty() || bashScriptPath.empty()) {
message = "Base image and script path are required.";
return false;
}
if (!std::filesystem::exists(bashScriptPath)) {
message = "Could not find that script file.";
return false;
}
if (outputFile.empty()) {
outputFile = "Dockerfile";
}
std::ifstream scriptFile(bashScriptPath);
std::ofstream dockerfile(outputFile);
if (!scriptFile.is_open() || !dockerfile.is_open()) {
message = "Could not open one of the files.";
return false;
}
dockerfile << "FROM " << baseImage << "\n";
dockerfile << "WORKDIR /app\n\n";
dockerfile << "# Auto-generated by Tux-Dock\n";
std::string line;
while (std::getline(scriptFile, line)) {
if (line.empty()) {
continue;
}
if (line.rfind("#", 0) == 0 || line.rfind("#!", 0) == 0) {
continue;
}
dockerfile << "RUN " << line << "\n";
}
dockerfile << "\nCMD [\"/bin/bash\"]\n";
dockerfile.close();
scriptFile.close();
if (imageName.empty()) {
message = "Dockerfile created. Build skipped because no image name was provided.";
return true;
}
const bool ok = runCommand("docker build -t " + shellEscape(imageName) + " -f " +
shellEscape(outputFile) + " .");
message = ok ? "Dockerfile created and image build completed." : "Dockerfile created, but the image build failed.";
return ok;
}
class TuxDockApp {
public:
void Run();
private:
enum class ModalMode { None, Input, Confirm, Select, Message };
struct RunContainerContext {
std::string image;
int port_count = 0;
std::vector<std::string> ports;
};
struct MySqlContext {
std::string port;
std::string password;
std::string version;
};
struct DockerfileContext {
std::string base_image;
std::string script_path;
std::string output_file;
std::string image_name;
};
DockerManager docker_;
std::vector<std::string> menu_entries_ = {
"Pull Docker Image",
"Run/Create Interactive Container",
"List All Containers",
"List All Images",
"Start Container Interactively (boot new session)",
"Start Detached Container Session",
"Delete Docker Image",
"Stop Container",
"Remove Container",
"Attach Shell to Running Container",
"Run Detached Command in Container",
"Spin Up MySQL Container",
"Get Container IP Address",
"Create Dockerfile & Build Image from Bash Script",
"About Tux-Dock",
"Exit",
};
int menu_selected_ = 0;
std::string status_ = "Ready. Select an action and press Enter.";
ModalMode modal_mode_ = ModalMode::None;
std::string modal_title_;
std::string modal_text_;
std::string modal_input_;
std::vector<std::string> modal_select_entries_;
int modal_select_index_ = 0;
ftxui::Component menu_component_ = ftxui::Menu(&menu_entries_, &menu_selected_);
ftxui::Component input_component_ = ftxui::Input(&modal_input_, "Type here");
ftxui::Component select_component_ = ftxui::Menu(&modal_select_entries_, &modal_select_index_);
std::function<void(bool, const std::string&)> input_callback_;
std::function<void(bool)> confirm_callback_;
std::function<void(bool, int)> select_callback_;
ftxui::ScreenInteractive* screen_ = nullptr;
static bool IsDigits(const std::string& value);
static std::string ShortId(const std::string& id);
static bool IsValidPortMapping(const std::string& mapping);
std::string FormatContainerList(const std::vector<DockerManager::ContainerInfo>& containers) const;
std::string FormatImageList(const std::vector<std::pair<std::string, std::string>>& images) const;
void SetStatus(const std::string& message);
void OpenInput(const std::string& title,
const std::string& text,
std::function<void(bool, const std::string&)> callback,
bool secret = false);
void OpenConfirm(const std::string& title, const std::string& text, std::function<void(bool)> callback);
void OpenSelect(const std::string& title,
const std::string& text,
std::vector<std::string> options,
std::function<void(bool, int)> callback);
void OpenMessage(const std::string& title, const std::string& text);
void ResolveInput(bool confirmed);
void ResolveConfirm(bool confirmed);
void ResolveSelect(bool confirmed);
void CloseMessage();
void ExecuteSelectedAction();
void ActionPullImage();
void ActionRunContainer();
void ActionListContainers();
void ActionListImages();
void ActionStartInteractive();
void ActionStartDetached();
void ActionDeleteImage();
void ActionStopContainer();
void ActionRemoveContainer();
void ActionExecShell();
void ActionExecDetachedCommand();
void ActionSpinUpMySQL();
void ActionShowContainerIP();
void ActionCreateDockerfile();
void ActionAbout();
void PromptPortCountAndRun(const std::shared_ptr<RunContainerContext>& context);
void PromptNextPort(const std::shared_ptr<RunContainerContext>& context, int index);
void PromptContainerSelection(const std::string& title,
std::function<void(const std::string& id, const std::string& name)> callback);
void PromptImageSelection(const std::string& title,
std::function<void(const std::string& id, const std::string& tag)> callback);
void RunDeferredStatusAction(const std::string& wait_message,
std::function<std::string()> action);
static void ClearTerminal();
void RunWithRestoredIO(const std::function<void()>& action,
bool clear_before = false,
bool clear_after = false);
bool OnEvent(ftxui::Event event);
ftxui::Element Render() const;
ftxui::Element RenderModal() const;
};
bool TuxDockApp::IsDigits(const std::string& value) {
if (value.empty()) {
return false;
}
for (unsigned char c : value) {
if (!std::isdigit(c)) {
return false;
}
}
return true;
}
std::string TuxDockApp::ShortId(const std::string& id) {
if (id.size() <= 12) {
return id;
}
return id.substr(0, 12);
}
bool TuxDockApp::IsValidPortMapping(const std::string& mapping) {
const std::size_t separator = mapping.find(':');
if (separator == std::string::npos) {
return false;
}
const std::string host = mapping.substr(0, separator);
const std::string container = mapping.substr(separator + 1);
return IsDigits(host) && IsDigits(container);
}
std::string TuxDockApp::FormatContainerList(
const std::vector<DockerManager::ContainerInfo>& containers) const {
std::stringstream ss;
for (std::size_t i = 0; i < containers.size(); ++i) {
const auto& container = containers[i];
const std::string state = container.running ? "running" : "stopped";
const std::string ports = container.ports.empty() ? "none" : container.ports;
ss << i + 1 << ". " << container.name << " (" << ShortId(container.id) << ")"
<< " [" << state << "]"
<< " ports: " << ports;
if (i + 1 < containers.size()) {
ss << "\n";
}
}
return ss.str();
}
std::string TuxDockApp::FormatImageList(const std::vector<std::pair<std::string, std::string>>& images) const {
std::stringstream ss;
for (std::size_t i = 0; i < images.size(); ++i) {
ss << i + 1 << ". " << images[i].second << " (" << ShortId(images[i].first) << ")";
if (i + 1 < images.size()) {
ss << "\n";
}
}
return ss.str();
}
void TuxDockApp::SetStatus(const std::string& message) {
status_ = message;
}
void TuxDockApp::OpenInput(const std::string& title,
const std::string& text,
std::function<void(bool, const std::string&)> callback,
bool secret) {
modal_mode_ = ModalMode::Input;
modal_title_ = title;
modal_text_ = text;
modal_input_.clear();
ftxui::InputOption input_option;
input_option.password = secret;
input_component_ = ftxui::Input(&modal_input_, "Type here", input_option);
input_callback_ = std::move(callback);
}
void TuxDockApp::OpenConfirm(const std::string& title,
const std::string& text,
std::function<void(bool)> callback) {
modal_mode_ = ModalMode::Confirm;
modal_title_ = title;
modal_text_ = text;
confirm_callback_ = std::move(callback);
}
void TuxDockApp::OpenSelect(const std::string& title,
const std::string& text,
std::vector<std::string> options,
std::function<void(bool, int)> callback) {
modal_mode_ = ModalMode::Select;
modal_title_ = title;
modal_text_ = text;
modal_select_entries_ = std::move(options);
modal_select_index_ = 0;
select_component_ = ftxui::Menu(&modal_select_entries_, &modal_select_index_);
select_callback_ = std::move(callback);
}
void TuxDockApp::OpenMessage(const std::string& title, const std::string& text) {
modal_mode_ = ModalMode::Message;
modal_title_ = title;
modal_text_ = text;
}
void TuxDockApp::ResolveInput(bool confirmed) {
const std::string value = modal_input_;
auto callback = std::move(input_callback_);
modal_mode_ = ModalMode::None;
modal_title_.clear();
modal_text_.clear();
modal_input_.clear();
input_component_ = ftxui::Input(&modal_input_, "Type here");
input_callback_ = {};
if (callback) {
callback(confirmed, value);
}
}
void TuxDockApp::ResolveConfirm(bool confirmed) {
auto callback = std::move(confirm_callback_);
modal_mode_ = ModalMode::None;
modal_title_.clear();
modal_text_.clear();
confirm_callback_ = {};
if (callback) {
callback(confirmed);
}
}
void TuxDockApp::ResolveSelect(bool confirmed) {
const int selected = modal_select_index_;
auto callback = std::move(select_callback_);
modal_mode_ = ModalMode::None;
modal_title_.clear();
modal_text_.clear();
modal_select_entries_.clear();
modal_select_index_ = 0;
select_component_ = ftxui::Menu(&modal_select_entries_, &modal_select_index_);
select_callback_ = {};
if (callback) {
callback(confirmed, selected);
}
}
void TuxDockApp::CloseMessage() {
modal_mode_ = ModalMode::None;
modal_title_.clear();
modal_text_.clear();
}
void TuxDockApp::PromptContainerSelection(
const std::string& title,
std::function<void(const std::string& id, const std::string& name)> callback) {
auto containers = docker_.getContainerList();
if (containers.empty()) {
SetStatus("No containers available.");
return;
}
std::vector<std::string> options;
options.reserve(containers.size());
for (const auto& container : containers) {
const std::string state = container.running ? "running" : "stopped";
const std::string ports = container.ports.empty() ? "none" : container.ports;
options.push_back(container.name + " (" + ShortId(container.id) + ") [" + state + "] ports: " + ports);
}
OpenSelect(title,
"Select a container with arrows and press Enter.",
std::move(options),
[this, containers, callback = std::move(callback)](bool confirmed, int selected) mutable {
if (!confirmed) {
SetStatus("Action cancelled.");
return;
}
if (selected < 0 || selected >= static_cast<int>(containers.size())) {
SetStatus("Please choose a valid container.");
return;
}
const auto& chosen = containers[static_cast<std::size_t>(selected)];
callback(chosen.id, chosen.name);
});
}
void TuxDockApp::PromptImageSelection(const std::string& title,
std::function<void(const std::string& id, const std::string& tag)> callback) {
auto images = docker_.getImageList();
if (images.empty()) {
SetStatus("No images found.");
return;
}
std::vector<std::string> options;
options.reserve(images.size());
for (const auto& image : images) {
options.push_back(image.second + " (" + ShortId(image.first) + ")");
}
OpenSelect(title,
"Select an image with arrows and press Enter.",
std::move(options),
[this, images, callback = std::move(callback)](bool confirmed, int selected) mutable {
if (!confirmed) {
SetStatus("Action cancelled.");
return;
}
if (selected < 0 || selected >= static_cast<int>(images.size())) {
SetStatus("Please choose a valid image.");
return;
}
const auto& chosen = images[static_cast<std::size_t>(selected)];
callback(chosen.first, chosen.second);
});
}
void TuxDockApp::ClearTerminal() {
std::cout << "\x1b[2J\x1b[H" << std::flush;
}
void TuxDockApp::RunWithRestoredIO(const std::function<void()>& action,
bool clear_before,
bool clear_after) {
if (screen_ != nullptr) {
screen_->WithRestoredIO([&] {
if (clear_before) {
ClearTerminal();
}
action();
if (clear_after) {
ClearTerminal();
}
})();
return;
}
if (clear_before) {
ClearTerminal();
}
action();
if (clear_after) {
ClearTerminal();
}
}
void TuxDockApp::RunDeferredStatusAction(const std::string& wait_message,
std::function<std::string()> action) {
SetStatus(wait_message);
if (screen_ == nullptr) {
SetStatus(action());
return;
}
ftxui::ScreenInteractive* active_screen = screen_;
active_screen->PostEvent(ftxui::Event::Custom);
std::thread([this, active_screen, action = std::move(action)]() mutable {
const std::string final_message = action();
if (ftxui::ScreenInteractive::Active() != active_screen) {
return;
}
active_screen->Post([this, final_message] { SetStatus(final_message); });
active_screen->PostEvent(ftxui::Event::Custom);
}).detach();
}
void TuxDockApp::ActionPullImage() {
const std::vector<std::string> quick_images = {
"debian:stable",
"ubuntu:noble",
"rockylinux:9.3",
"alpine:latest",
};
std::vector<std::string> options = quick_images;
options.push_back("Custom image...");
OpenSelect("Pull Docker Image",
"Select an image with arrows, then press Enter.",
std::move(options),
[this, quick_images](bool confirmed, int selected) {
if (!confirmed) {
SetStatus("Image pull cancelled.");
return;
}
if (selected < 0 || selected > static_cast<int>(quick_images.size())) {
SetStatus("Please select a valid image option.");
return;
}
if (selected < static_cast<int>(quick_images.size())) {
const std::string image = quick_images[static_cast<std::size_t>(selected)];
RunDeferredStatusAction("Please wait, pulling image...", [this, image] {
std::string message;
docker_.pullImage(image, message);
return message;
});
return;
}
OpenInput("Pull Docker Image",
"Enter custom Docker image name:",
[this](bool custom_confirmed, const std::string& image) {
if (!custom_confirmed) {
SetStatus("Image pull cancelled.");
return;
}
if (image.empty()) {
SetStatus("Image name cannot be empty.");
ActionPullImage();
return;
}
RunDeferredStatusAction("Please wait, pulling image...", [this, image] {
std::string message;
docker_.pullImage(image, message);
return message;
});
});
});
}
void TuxDockApp::ActionRunContainer() {
auto images = docker_.getImageList();
std::vector<std::string> options;
options.reserve(images.size() + 1);
for (const auto& image : images) {
options.push_back(image.second + " (" + ShortId(image.first) + ")");
}
options.push_back("Custom image...");
OpenSelect("Run Interactive Container",
"Select an image with arrows, then press Enter.",
std::move(options),
[this, images](bool confirmed, int selected) {
if (!confirmed) {
SetStatus("Container run cancelled.");
return;
}
if (selected < 0 || selected > static_cast<int>(images.size())) {
SetStatus("Please select a valid image option.");
return;
}
auto context = std::make_shared<RunContainerContext>();
if (selected == static_cast<int>(images.size())) {
OpenInput("Run Interactive Container",
"Enter custom image name:",
[this, context](bool custom_confirmed, const std::string& image_name) {
if (!custom_confirmed) {
SetStatus("Container run cancelled.");
return;
}
if (image_name.empty()) {
SetStatus("Image name cannot be empty.");
ActionRunContainer();
return;
}
context->image = image_name;
PromptPortCountAndRun(context);
});
return;
}
context->image = images[static_cast<std::size_t>(selected)].second;
PromptPortCountAndRun(context);
});
}
void TuxDockApp::PromptPortCountAndRun(const std::shared_ptr<RunContainerContext>& context) {
OpenInput("Port Mappings",
"How many port mappings? (0 for none)",
[this, context](bool confirmed, const std::string& count_value) {
if (!confirmed) {
SetStatus("Container run cancelled.");
return;
}
if (!IsDigits(count_value)) {
SetStatus("Please enter a valid number.");
PromptPortCountAndRun(context);
return;
}
context->port_count = std::stoi(count_value);
context->ports.clear();
if (context->port_count < 0) {
SetStatus("Port mappings cannot be negative.");
PromptPortCountAndRun(context);
return;
}
PromptNextPort(context, 0);
});
}
void TuxDockApp::PromptNextPort(const std::shared_ptr<RunContainerContext>& context, int index) {
if (index >= context->port_count) {
std::string message;
SetStatus("Starting interactive container...");
RunWithRestoredIO([this, context, &message] {
docker_.runContainerInteractive(context->image, context->ports, message);
}, true, true);
SetStatus(message);
return;
}
OpenInput("Port Mapping",
"Enter mapping #" + std::to_string(index + 1) + " (example: 8080:80)",
[this, context, index](bool confirmed, const std::string& mapping) {
if (!confirmed) {
SetStatus("Container run cancelled.");
return;
}
if (!IsValidPortMapping(mapping)) {
SetStatus("Use host:container format, for example 8080:80.");
PromptNextPort(context, index);
return;
}
context->ports.push_back(mapping);
PromptNextPort(context, index + 1);
});
}
void TuxDockApp::ActionListContainers() {
auto containers = docker_.getContainerList();
if (containers.empty()) {
SetStatus("No containers available.");
return;
}
OpenMessage("Containers", FormatContainerList(containers));
SetStatus("Container list opened.");
}
void TuxDockApp::ActionListImages() {
auto images = docker_.getImageList();
if (images.empty()) {
SetStatus("No images found.");
return;
}
OpenMessage("Images", FormatImageList(images));
SetStatus("Image list opened.");
}
void TuxDockApp::ActionStartInteractive() {
PromptContainerSelection("Start Interactively", [this](const std::string& id, const std::string&) {
std::string message;
SetStatus("Starting interactive session...");
RunWithRestoredIO([this, &id, &message] { docker_.startInteractive(id, message); }, true, true);
SetStatus(message);
});
}
void TuxDockApp::ActionStartDetached() {
PromptContainerSelection("Start Detached", [this](const std::string& id, const std::string&) {
std::string message;
SetStatus("Starting container in detached mode...");
RunWithRestoredIO([this, &id, &message] { docker_.startDetached(id, message); });
SetStatus(message);
});
}
void TuxDockApp::ActionDeleteImage() {
PromptImageSelection("Delete Image", [this](const std::string& id, const std::string& tag) {
OpenConfirm("Delete Image",
"Delete image " + tag + "?",
[this, id](bool confirmed) {
if (!confirmed) {
SetStatus("Image deletion cancelled.");
return;
}
RunDeferredStatusAction("Please wait, deleting image...", [this, id] {
std::string message;
docker_.deleteImage(id, message);
return message;
});
});
});
}
void TuxDockApp::ActionStopContainer() {
PromptContainerSelection("Stop Container", [this](const std::string& id, const std::string&) {
RunDeferredStatusAction("Please wait, stopping container...", [this, id] {
std::string message;
docker_.stopContainer(id, message);
return message;
});
});
}
void TuxDockApp::ActionRemoveContainer() {
PromptContainerSelection("Remove Container", [this](const std::string& id, const std::string& name) {
OpenConfirm("Remove Container",
"Remove container " + name + "?",
[this, id](bool confirmed) {
if (!confirmed) {
SetStatus("Container removal cancelled.");
return;
}
RunDeferredStatusAction("Please wait, removing container...", [this, id] {
std::string message;
docker_.removeContainer(id, message);
return message;
});
});
});
}
void TuxDockApp::ActionExecShell() {
PromptContainerSelection("Open Shell", [this](const std::string& id, const std::string&) {
std::string message;
SetStatus("Opening shell session...");
RunWithRestoredIO([this, &id, &message] { docker_.execShell(id, message); }, true, true);
SetStatus(message);
});
}
void TuxDockApp::ActionExecDetachedCommand() {
PromptContainerSelection("Run Detached Command",
[this](const std::string& id, const std::string& name) {
OpenInput("Detached Command",
"Enter command to run in " + name + ":",
[this, id](bool confirmed, const std::string& command) {
if (!confirmed) {
SetStatus("Detached command cancelled.");
return;
}
std::string message;
SetStatus("Running detached command...");
RunWithRestoredIO([this, &id, &command, &message] {
docker_.execDetachedCommand(id, command, message);
});
SetStatus(message);
});
});
}
void TuxDockApp::ActionSpinUpMySQL() {
auto context = std::make_shared<MySqlContext>();
OpenInput("MySQL Setup",
"Enter port mapping (example: 3306:3306)",
[this, context](bool confirmed, const std::string& port) {
if (!confirmed) {
SetStatus("MySQL setup cancelled.");
return;
}
if (!IsValidPortMapping(port)) {
SetStatus("Use host:container format, for example 3306:3306.");
return;
}
context->port = port;