-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDoctorCommand.cpp
More file actions
1894 lines (1560 loc) · 50.9 KB
/
Copy pathDoctorCommand.cpp
File metadata and controls
1894 lines (1560 loc) · 50.9 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
/**
*
* @file DoctorCommand.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
*/
#include <vix/cli/commands/DoctorCommand.hpp>
#include <vix/cli/util/Ui.hpp>
#include <vix/cli/Style.hpp>
#include <vix/utils/Env.hpp>
#include <nlohmann/json.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <optional>
#include <cctype>
#include <algorithm>
#include <stdexcept>
#include <cstdio>
namespace fs = std::filesystem;
using json = nlohmann::json;
namespace vix::commands
{
using namespace vix::cli::style;
namespace
{
std::string trim_copy(std::string s)
{
while (!s.empty() && (s.back() == '\n' || s.back() == '\r' || std::isspace(static_cast<unsigned char>(s.back()))))
s.pop_back();
size_t i = 0;
while (i < s.size() && std::isspace(static_cast<unsigned char>(s[i])))
++i;
return s.substr(i);
}
bool starts_with(const std::string &s, const std::string &p)
{
return s.size() >= p.size() && s.compare(0, p.size(), p) == 0;
}
fs::path stats_file()
{
#ifdef _WIN32
if (const char *p = vix::utils::vix_getenv("LOCALAPPDATA"))
if (*p)
return fs::path(p) / "Vix" / "install.json";
return fs::current_path() / "install.json";
#else
if (const char *home = vix::utils::vix_getenv("HOME"))
if (*home)
return fs::path(home) / ".local" / "share" / "vix" / "install.json";
return fs::current_path() / "install.json";
#endif
}
fs::path current_exe_path()
{
if (const char *p = vix::utils::vix_getenv("VIX_CLI_PATH"))
if (*p)
return fs::path(p);
#ifdef _WIN32
return fs::path("vix.exe");
#else
return fs::path("vix");
#endif
}
std::string detect_os()
{
#ifdef _WIN32
return "windows";
#elif __APPLE__
return "macos";
#else
return "linux";
#endif
}
std::string detect_arch()
{
#if defined(__x86_64__) || defined(_M_X64)
return "x86_64";
#elif defined(__aarch64__) || defined(_M_ARM64)
return "aarch64";
#else
return "unknown";
#endif
}
std::optional<std::string> extract_version_token(const std::string &text)
{
// Find vX.Y.Z in text
std::string t = text;
t.erase(std::remove(t.begin(), t.end(), '\r'), t.end());
auto looks_like = [](const std::string &x) -> bool
{
if (x.size() < 6 || x[0] != 'v')
return false;
int dots = 0;
for (size_t i = 1; i < x.size(); ++i)
{
const char c = x[i];
if (c == '.')
{
dots++;
continue;
}
if (std::isdigit(static_cast<unsigned char>(c)))
continue;
if (c == '-' || c == '+' || std::isalpha(static_cast<unsigned char>(c)))
continue;
return false;
}
return dots >= 2;
};
std::string cur;
std::vector<std::string> parts;
for (char ch : t)
{
if (std::isspace(static_cast<unsigned char>(ch)))
{
if (!cur.empty())
{
parts.push_back(cur);
cur.clear();
}
continue;
}
cur.push_back(ch);
}
if (!cur.empty())
parts.push_back(cur);
for (int i = static_cast<int>(parts.size()) - 1; i >= 0; --i)
{
if (looks_like(parts[static_cast<size_t>(i)]))
return parts[static_cast<size_t>(i)];
}
return std::nullopt;
}
bool have_cmd(const std::string &name)
{
#ifdef _WIN32
std::string cmd = "where " + name + " >nul 2>&1";
return std::system(cmd.c_str()) == 0;
#else
std::string cmd = "command -v " + name + " >/dev/null 2>&1";
return std::system(cmd.c_str()) == 0;
#endif
}
std::optional<std::string> run_capture(const std::string &cmd)
{
#ifdef _WIN32
FILE *pipe = _popen(cmd.c_str(), "r");
#else
FILE *pipe = popen(cmd.c_str(), "r");
#endif
if (!pipe)
return std::nullopt;
std::string out;
char buf[2048];
while (std::fgets(buf, sizeof(buf), pipe))
out += buf;
#ifdef _WIN32
_pclose(pipe);
#else
pclose(pipe);
#endif
out = trim_copy(out);
if (out.empty())
return std::nullopt;
return out;
}
std::optional<fs::path> which_vix();
std::optional<std::string> vix_version_from_self()
{
#ifdef _WIN32
if (auto w = which_vix())
return extract_version_token(run_capture("\"" + w->string() + "\" --version 2>nul").value_or(""));
return extract_version_token(run_capture("vix --version 2>nul").value_or(""));
#else
return extract_version_token(run_capture("vix --version 2>/dev/null").value_or(""));
#endif
}
bool is_writable_dir(const fs::path &dir)
{
std::error_code ec;
fs::create_directories(dir, ec);
if (ec)
return false;
fs::path probe = dir / ".vix_doctor_write.tmp";
std::ofstream out(probe.string(), std::ios::binary);
if (!out)
return false;
out << "x";
out.close();
fs::remove(probe, ec);
return true;
}
std::vector<std::string> split_path_list(const std::string &pathEnv)
{
std::vector<std::string> out;
std::string cur;
#ifdef _WIN32
const char sep = ';';
#else
const char sep = ':';
#endif
for (char c : pathEnv)
{
if (c == sep)
{
if (!cur.empty())
out.push_back(cur);
cur.clear();
}
else
{
cur.push_back(c);
}
}
if (!cur.empty())
out.push_back(cur);
return out;
}
std::string normalize_dir(std::string s)
{
#ifdef _WIN32
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c)
{ return static_cast<char>(std::tolower(c)); });
while (!s.empty() && (s.back() == '\\' || s.back() == '/'))
s.pop_back();
#else
while (!s.empty() && s.back() == '/')
s.pop_back();
#endif
return s;
}
bool path_contains_dir(const std::string &dir)
{
const char *p = vix::utils::vix_getenv("PATH");
if (!p)
return false;
const std::string want = normalize_dir(dir);
const auto segs = split_path_list(std::string(p));
for (auto s : segs)
{
if (normalize_dir(trim_copy(s)) == want)
return true;
}
return false;
}
json read_json_or_throw(const fs::path &p)
{
std::ifstream in(p);
if (!in)
throw std::runtime_error("cannot open: " + p.string());
json j;
in >> j;
return j;
}
void print_dep_status(const std::string &name, bool ok, const std::string &hintIfMissing)
{
if (ok)
vix::cli::util::ok_line(std::cout, name + ": ok");
else
{
vix::cli::util::err_line(std::cerr, name + ": missing");
if (!hintIfMissing.empty())
vix::cli::util::warn_line(std::cerr, hintIfMissing);
}
}
std::optional<std::string> github_latest_tag(const std::string &repo)
{
// Uses best available tool:
// - Linux/macOS: curl or wget
// - Windows: PowerShell Invoke-RestMethod
#ifdef _WIN32
// Note: keep it single-line friendly for _popen.
const std::string ps =
"powershell -NoProfile -Command \""
"$r=Invoke-RestMethod -Headers @{ 'User-Agent'='vix-doctor' } "
"-Uri 'https://api.github.com/repos/" +
repo +
"/releases/latest'; "
"if($r.tag_name){Write-Output $r.tag_name}\"";
auto out = run_capture(ps + " 2>nul");
if (!out)
return std::nullopt;
auto tag = trim_copy(*out);
if (tag.empty())
return std::nullopt;
return tag;
#else
if (have_cmd("curl"))
{
auto out = run_capture(
"curl -fSsL -H 'User-Agent: vix-doctor' "
"'https://api.github.com/repos/" +
repo +
"/releases/latest' 2>/dev/null");
if (!out)
return std::nullopt;
// Extract "tag_name":"vX.Y.Z" without jq
const std::string &body = *out;
const std::string key = "\"tag_name\"";
const auto pos = body.find(key);
if (pos == std::string::npos)
return std::nullopt;
const auto colon = body.find(':', pos);
if (colon == std::string::npos)
return std::nullopt;
const auto q1 = body.find('"', colon);
if (q1 == std::string::npos)
return std::nullopt;
const auto q2 = body.find('"', q1 + 1);
if (q2 == std::string::npos || q2 <= q1 + 1)
return std::nullopt;
const std::string tag = body.substr(q1 + 1, q2 - (q1 + 1));
if (tag.empty())
return std::nullopt;
return tag;
}
if (have_cmd("wget"))
{
auto out = run_capture(
"wget -qO- "
"'https://api.github.com/repos/" +
repo +
"/releases/latest' 2>/dev/null");
if (!out)
return std::nullopt;
const std::string &body = *out;
const std::string key = "\"tag_name\"";
const auto pos = body.find(key);
if (pos == std::string::npos)
return std::nullopt;
const auto colon = body.find(':', pos);
if (colon == std::string::npos)
return std::nullopt;
const auto q1 = body.find('"', colon);
if (q1 == std::string::npos)
return std::nullopt;
const auto q2 = body.find('"', q1 + 1);
if (q2 == std::string::npos || q2 <= q1 + 1)
return std::nullopt;
const std::string tag = body.substr(q1 + 1, q2 - (q1 + 1));
if (tag.empty())
return std::nullopt;
return tag;
}
return std::nullopt;
#endif
}
struct Options
{
bool jsonOut = false;
bool online = false;
bool production = false;
bool toolchain = false;
std::string repo = "vixcpp/vix";
};
struct ToolchainInfo
{
fs::path cliPath{};
std::string cliVersion{};
std::optional<fs::path> vixDir{};
std::optional<fs::path> cmakePrefixPath{};
};
std::optional<fs::path> env_path(const char *name)
{
if (const char *value = vix::utils::vix_getenv(name))
{
if (*value)
return fs::path(value);
}
return std::nullopt;
}
std::optional<fs::path> first_cmake_prefix_path()
{
if (const char *value = vix::utils::vix_getenv("CMAKE_PREFIX_PATH"))
{
if (!*value)
return std::nullopt;
std::string raw(value);
#ifdef _WIN32
const char sep = ';';
#else
const char sep = ':';
#endif
const auto pos = raw.find(sep);
if (pos != std::string::npos)
raw = raw.substr(0, pos);
raw = trim_copy(raw);
if (!raw.empty())
return fs::path(raw);
}
return std::nullopt;
}
fs::path canonical_or_absolute(const fs::path &path)
{
std::error_code ec;
fs::path resolved = fs::weakly_canonical(path, ec);
if (!ec && !resolved.empty())
return resolved;
resolved = fs::absolute(path, ec);
if (!ec && !resolved.empty())
return resolved;
return path;
}
bool same_installation_root(
const fs::path &a,
const fs::path &b)
{
if (a.empty() || b.empty())
return false;
const fs::path left = canonical_or_absolute(a);
const fs::path right = canonical_or_absolute(b);
if (left == right)
return true;
const std::string ls = left.string();
const std::string rs = right.string();
return ls.find(rs) == 0 || rs.find(ls) == 0;
}
int doctor_toolchain()
{
vix::cli::util::section(std::cout, "Toolchain Consistency");
ToolchainInfo info;
if (auto path = which_vix())
info.cliPath = *path;
else
info.cliPath = current_exe_path();
info.cliVersion = vix_version_from_self().value_or("unknown");
info.vixDir = env_path("Vix_DIR");
info.cmakePrefixPath = first_cmake_prefix_path();
vix::cli::util::section(std::cout, "CLI");
vix::cli::util::kv(std::cout, "Path", info.cliPath.string());
vix::cli::util::kv(std::cout, "Version", info.cliVersion);
vix::cli::util::section(std::cout, "CMake Package");
vix::cli::util::kv(
std::cout,
"Vix_DIR",
info.vixDir ? info.vixDir->string() : "(not set)");
vix::cli::util::kv(
std::cout,
"CMAKE_PREFIX_PATH",
info.cmakePrefixPath ? info.cmakePrefixPath->string() : "(not set)");
bool ok = true;
if (info.vixDir)
{
if (!same_installation_root(info.cliPath.parent_path(), *info.vixDir))
ok = false;
}
if (info.cmakePrefixPath)
{
if (!same_installation_root(info.cliPath.parent_path(), *info.cmakePrefixPath))
ok = false;
}
vix::cli::util::section(std::cout, "Result");
if (ok)
{
vix::cli::util::ok_line(
std::cout,
"Vix CLI and CMake package appear to come from the same installation");
return 0;
}
vix::cli::util::warn_line(
std::cerr,
"Vix CLI and Vix libraries come from different installations");
vix::cli::util::kv(std::cerr, "CLI", info.cliPath.string());
if (info.vixDir)
vix::cli::util::kv(std::cerr, "Vix_DIR", info.vixDir->string());
if (info.cmakePrefixPath)
vix::cli::util::kv(std::cerr, "CMAKE_PREFIX_PATH", info.cmakePrefixPath->string());
vix::cli::util::warn_line(
std::cerr,
"This can cause confusing builds.");
vix::cli::util::warn_line(
std::cerr,
"Fix: use the same Vix installation for the CLI and CMake package.");
vix::cli::util::warn_line(
std::cerr,
"Example:");
vix::cli::util::warn_line(
std::cerr,
" export Vix_DIR=" + info.cliPath.parent_path().string());
vix::cli::util::warn_line(
std::cerr,
" export CMAKE_PREFIX_PATH=" + info.cliPath.parent_path().string());
return 1;
}
Options parse_args(const std::vector<std::string> &args)
{
Options o;
// repo override: --repo owner/name
for (size_t i = 0; i < args.size(); ++i)
{
const auto &a = args[i];
if (a == "production")
{
o.production = true;
continue;
}
if (a == "toolchain")
{
o.toolchain = true;
continue;
}
if (a == "--json")
{
o.jsonOut = true;
continue;
}
if (a == "--online")
{
o.online = true;
continue;
}
if (a == "--repo")
{
if (i + 1 >= args.size())
throw std::runtime_error("--repo requires a value (owner/name)");
o.repo = args[i + 1];
++i;
continue;
}
if (starts_with(a, "--repo="))
{
o.repo = a.substr(std::string("--repo=").size());
if (o.repo.empty())
throw std::runtime_error("--repo=VALUE cannot be empty");
continue;
}
if (a == "-h" || a == "--help")
continue;
throw std::runtime_error("unknown argument: " + a);
}
return o;
}
std::optional<fs::path> which_vix()
{
#ifdef _WIN32
// Take first result from `where vix`
auto out = run_capture("where vix 2>nul");
if (!out)
return std::nullopt;
std::string s = *out;
s.erase(std::remove(s.begin(), s.end(), '\r'), s.end());
// first line
const auto nl = s.find('\n');
const std::string first = trim_copy(nl == std::string::npos ? s : s.substr(0, nl));
if (first.empty())
return std::nullopt;
return fs::path(first);
#else
auto out = run_capture("command -v vix 2>/dev/null");
if (!out)
return std::nullopt;
return fs::path(*out);
#endif
}
std::string shell_quote(const std::string &s)
{
#ifdef _WIN32
return "\"" + s + "\"";
#else
std::string out = "'";
for (char c : s)
{
if (c == '\'')
out += "'\\''";
else
out += c;
}
out += "'";
return out;
#endif
}
std::string lower_copy(std::string s)
{
std::transform(
s.begin(),
s.end(),
s.begin(),
[](unsigned char c)
{
return static_cast<char>(std::tolower(c));
});
return s;
}
std::optional<std::string> read_project_name()
{
const fs::path vixJson = fs::current_path() / "vix.json";
if (fs::exists(vixJson))
{
try
{
const auto j = read_json_or_throw(vixJson);
if (j.is_object() && j.contains("name") && j["name"].is_string())
{
const std::string name = trim_copy(j["name"].get<std::string>());
if (!name.empty())
return name;
}
}
catch (...)
{
}
}
for (const auto &entry : fs::directory_iterator(fs::current_path()))
{
if (!entry.is_regular_file())
continue;
const auto p = entry.path();
if (p.extension() == ".vix")
{
return p.stem().string();
}
}
const auto current = fs::current_path().filename().string();
if (!current.empty())
return current;
return std::nullopt;
}
std::optional<fs::path> detect_build_dir()
{
const std::vector<fs::path> candidates = {
fs::current_path() / "build-ninja",
fs::current_path() / "build-release",
fs::current_path() / "build",
fs::current_path() / "cmake-build-debug",
fs::current_path() / "cmake-build-release"};
for (const auto &candidate : candidates)
{
if (fs::exists(candidate) && fs::is_directory(candidate))
return candidate;
}
return std::nullopt;
}
std::optional<fs::path> detect_binary_path(const std::string &projectName)
{
const auto buildDir = detect_build_dir();
if (!buildDir)
return std::nullopt;
#ifdef _WIN32
const auto exe = *buildDir / (projectName + ".exe");
#else
const auto exe = *buildDir / projectName;
#endif
if (fs::exists(exe))
return exe;
for (const auto &entry : fs::recursive_directory_iterator(*buildDir))
{
if (!entry.is_regular_file())
continue;
#ifdef _WIN32
if (entry.path().filename() == projectName + ".exe")
return entry.path();
#else
if (entry.path().filename() == projectName)
return entry.path();
#endif
}
return exe;
}
#ifndef _WIN32
bool process_running_for_binary(const fs::path &binary)
{
if (binary.empty())
return false;
const std::string cmd =
"pgrep -f " + shell_quote(binary.string()) + " >/dev/null 2>&1";
return std::system(cmd.c_str()) == 0;
}
std::vector<std::string> list_systemd_services()
{
std::vector<std::string> services;
auto out = run_capture(
"systemctl list-unit-files --type=service --no-legend 2>/dev/null | "
"awk '{print $1}'");
if (!out)
return services;
std::string current;
for (char c : *out)
{
if (c == '\n' || c == '\r')
{
current = trim_copy(current);
if (!current.empty())
services.push_back(current);
current.clear();
continue;
}
current.push_back(c);
}
current = trim_copy(current);
if (!current.empty())
services.push_back(current);
return services;
}
std::optional<std::string> systemctl_property(
const std::string &service,
const std::string &property)
{
auto out = run_capture(
"systemctl show " +
shell_quote(service) +
" -p " +
shell_quote(property) +
" --value 2>/dev/null");
if (!out)
return std::nullopt;
const auto value = trim_copy(*out);
if (value.empty())
return std::nullopt;
return value;
}
bool service_points_to_project(
const std::string &service,
const fs::path &projectDir,
const std::optional<fs::path> &binary)
{
const auto workingDir = systemctl_property(service, "WorkingDirectory");
const auto execStart = systemctl_property(service, "ExecStart");
std::error_code ec;
const fs::path canonicalProject =
fs::weakly_canonical(projectDir, ec);
if (workingDir && !workingDir->empty())
{
std::error_code wdEc;
const fs::path canonicalWorkingDir =
fs::weakly_canonical(fs::path(*workingDir), wdEc);
if (!wdEc && !ec && canonicalWorkingDir == canonicalProject)
return true;
if (trim_copy(*workingDir) == projectDir.string())
return true;
}
if (binary && execStart && !execStart->empty())
{
const std::string exec = *execStart;
const std::string bin = binary->string();
if (!bin.empty() && exec.find(bin) != std::string::npos)
return true;
if (binary->has_filename())
{
const std::string filename = binary->filename().string();
if (!filename.empty() && exec.find(filename) != std::string::npos)
{
if (workingDir && trim_copy(*workingDir) == projectDir.string())
return true;
}
}
}
return false;
}
bool systemd_service_exists(const std::string &service)
{
const std::string cmd =
"systemctl status " + shell_quote(service) + " >/dev/null 2>&1";
return std::system(cmd.c_str()) == 0;
}
std::optional<std::string> detect_systemd_service(
const std::string &projectName,
const fs::path &projectDir,
const std::optional<fs::path> &binary)
{
const std::string lower = lower_copy(projectName);
const std::string exactService = lower + ".service";
if (systemd_service_exists(exactService) &&
service_points_to_project(exactService, projectDir, binary))
{
return exactService;
}
const auto services = list_systemd_services();
for (const auto &service : services)
{
if (service_points_to_project(service, projectDir, binary))
return service;
}
return std::nullopt;
}
std::optional<std::string> detect_listening_port_for_binary(const fs::path &binary)
{
if (binary.empty() || !have_cmd("ss"))
return std::nullopt;
auto out = run_capture(
"ss -tulpn 2>/dev/null | grep " +
shell_quote(binary.filename().string()) +
" | head -1");
if (!out)
return std::nullopt;
const std::string line = *out;
const auto colon = line.find(':');
if (colon == std::string::npos)
return std::nullopt;
std::size_t start = colon + 1;
std::size_t end = start;
while (end < line.size() && std::isdigit(static_cast<unsigned char>(line[end])))
++end;
if (end == start)
return std::nullopt;
return line.substr(start, end - start);
}
std::optional<std::string> detect_websocket_port_from_config()
{
const fs::path vixJson = fs::current_path() / "vix.json";
if (!fs::exists(vixJson))
return std::nullopt;
try
{
const json root = read_json_or_throw(vixJson);
if (!root.is_object() ||
!root.contains("production") ||
!root["production"].is_object())
{
return std::nullopt;
}
const auto &production = root["production"];
if (!production.contains("ports") ||
!production["ports"].is_object())
{
return std::nullopt;
}
const auto &ports = production["ports"];
if (!ports.contains("websocket") ||
!ports["websocket"].is_number_integer())