-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUnpublishCommand.cpp
More file actions
792 lines (671 loc) · 20.2 KB
/
Copy pathUnpublishCommand.cpp
File metadata and controls
792 lines (671 loc) · 20.2 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
/**
*
* @file UnpublishCommand.cpp
* @author Gaspard Kirira
*
* Copyright 2025, 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/UnpublishCommand.hpp>
#include <vix/cli/util/Ui.hpp>
#include <vix/cli/Style.hpp>
#include <vix/utils/Env.hpp>
#include <algorithm>
#include <cctype>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>
#if defined(_WIN32)
#include <windows.h>
#else
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#endif
namespace fs = std::filesystem;
namespace vix::commands
{
using namespace vix::cli::style;
namespace
{
static std::string trim_copy(std::string s)
{
auto isws = [](unsigned char c)
{ return std::isspace(c) != 0; };
while (!s.empty() && isws(static_cast<unsigned char>(s.front())))
s.erase(s.begin());
while (!s.empty() && isws(static_cast<unsigned char>(s.back())))
s.pop_back();
return s;
}
static 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;
}
static std::string iso_utc_now_compact()
{
using namespace std::chrono;
const auto now = system_clock::now();
const std::time_t t = system_clock::to_time_t(now);
std::tm tm{};
#if defined(_WIN32)
gmtime_s(&tm, &t);
#else
gmtime_r(&t, &tm);
#endif
std::ostringstream oss;
oss << std::setw(4) << std::setfill('0') << (tm.tm_year + 1900)
<< std::setw(2) << std::setfill('0') << (tm.tm_mon + 1)
<< std::setw(2) << std::setfill('0') << tm.tm_mday
<< "T"
<< std::setw(2) << std::setfill('0') << tm.tm_hour
<< std::setw(2) << std::setfill('0') << tm.tm_min
<< std::setw(2) << std::setfill('0') << tm.tm_sec
<< "Z";
return oss.str();
}
static std::string home_dir()
{
#ifdef _WIN32
const char *home = vix::utils::vix_getenv("USERPROFILE");
#else
const char *home = vix::utils::vix_getenv("HOME");
#endif
return home ? std::string(home) : std::string();
}
static fs::path vix_root()
{
const std::string h = home_dir();
if (h.empty())
return fs::path(".vix");
return fs::path(h) / ".vix";
}
static fs::path registry_repo_dir()
{
// vix registry sync clones into ~/.vix/registry/index
return vix_root() / "registry" / "index";
}
static fs::path registry_index_dir()
{
// entries: ~/.vix/registry/index/index
return registry_repo_dir() / "index";
}
static bool is_git_repo(const fs::path &dir)
{
std::error_code ec;
return fs::exists(dir / ".git", ec);
}
#if defined(_WIN32)
static std::string join_for_log(
const std::vector<std::string> &args)
{
std::ostringstream out;
for (std::size_t i = 0; i < args.size(); ++i)
{
if (i > 0)
{
out << ' ';
}
const std::string &arg = args[i];
const bool needsQuotes =
arg.find(' ') != std::string::npos ||
arg.find('\t') != std::string::npos ||
arg.find('"') != std::string::npos;
if (!needsQuotes)
{
out << arg;
continue;
}
out << '"';
for (char c : arg)
{
if (c == '"')
{
out << "\\\"";
}
else
{
out << c;
}
}
out << '"';
}
return out.str();
}
#endif
/* =========================
Process runner (no system)
========================= */
struct ProcessResult
{
int exitCode{127};
std::string out;
std::string err;
};
#if defined(_WIN32)
static std::string win_last_error()
{
const DWORD err = GetLastError();
if (!err)
return {};
LPSTR buf = nullptr;
const DWORD n = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&buf,
0,
nullptr);
std::string s = (n && buf) ? std::string(buf, buf + n) : std::string();
if (buf)
LocalFree(buf);
return trim_copy(s);
}
static std::string win_read_all(HANDLE h)
{
std::string out;
char buf[4096];
DWORD read = 0;
while (true)
{
const BOOL ok = ReadFile(h, buf, (DWORD)sizeof(buf), &read, nullptr);
if (!ok || read == 0)
break;
out.append(buf, buf + read);
}
return out;
}
static ProcessResult run_process_capture(const std::vector<std::string> &args, const std::optional<fs::path> &cwd = std::nullopt)
{
ProcessResult r;
if (args.empty())
{
r.exitCode = 127;
r.err = "empty command";
return r;
}
SECURITY_ATTRIBUTES sa{};
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
HANDLE outRead = nullptr, outWrite = nullptr;
HANDLE errRead = nullptr, errWrite = nullptr;
if (!CreatePipe(&outRead, &outWrite, &sa, 0))
{
r.exitCode = 127;
r.err = "CreatePipe(stdout) failed: " + win_last_error();
return r;
}
if (!SetHandleInformation(outRead, HANDLE_FLAG_INHERIT, 0))
{
r.exitCode = 127;
r.err = "SetHandleInformation(stdout) failed: " + win_last_error();
CloseHandle(outRead);
CloseHandle(outWrite);
return r;
}
if (!CreatePipe(&errRead, &errWrite, &sa, 0))
{
r.exitCode = 127;
r.err = "CreatePipe(stderr) failed: " + win_last_error();
CloseHandle(outRead);
CloseHandle(outWrite);
return r;
}
if (!SetHandleInformation(errRead, HANDLE_FLAG_INHERIT, 0))
{
r.exitCode = 127;
r.err = "SetHandleInformation(stderr) failed: " + win_last_error();
CloseHandle(outRead);
CloseHandle(outWrite);
CloseHandle(errRead);
CloseHandle(errWrite);
return r;
}
STARTUPINFOA si{};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdOutput = outWrite;
si.hStdError = errWrite;
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
PROCESS_INFORMATION pi{};
std::string cmd = join_for_log(args);
std::vector<char> cmdBuf(cmd.begin(), cmd.end());
cmdBuf.push_back('\0');
std::string cwdStr;
LPCSTR cwdPtr = nullptr;
if (cwd)
{
cwdStr = cwd->string();
cwdPtr = cwdStr.c_str();
}
const BOOL ok = CreateProcessA(
nullptr,
cmdBuf.data(),
nullptr,
nullptr,
TRUE,
0,
nullptr,
cwdPtr,
&si,
&pi);
CloseHandle(outWrite);
CloseHandle(errWrite);
if (!ok)
{
r.exitCode = 127;
r.err = "CreateProcess failed: " + win_last_error();
CloseHandle(outRead);
CloseHandle(errRead);
return r;
}
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD ec = 127;
GetExitCodeProcess(pi.hProcess, &ec);
r.exitCode = (int)ec;
r.out = win_read_all(outRead);
r.err = win_read_all(errRead);
CloseHandle(outRead);
CloseHandle(errRead);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
r.out = trim_copy(r.out);
r.err = trim_copy(r.err);
return r;
}
#else
static int set_cloexec(int fd)
{
const int flags = fcntl(fd, F_GETFD);
if (flags < 0)
return -1;
return fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
}
static std::string read_fd_all(int fd)
{
std::string out;
char buf[4096];
while (true)
{
const ssize_t n = ::read(fd, buf, sizeof(buf));
if (n > 0)
{
out.append(buf, buf + n);
continue;
}
if (n == 0)
break;
if (errno == EINTR)
continue;
break;
}
return out;
}
static ProcessResult run_process_capture(const std::vector<std::string> &args, const std::optional<fs::path> &cwd = std::nullopt)
{
ProcessResult r;
if (args.empty())
{
r.exitCode = 127;
r.err = "empty command";
return r;
}
int outPipe[2]{-1, -1};
int errPipe[2]{-1, -1};
if (pipe(outPipe) != 0)
{
r.exitCode = 127;
r.err = "pipe(stdout) failed";
return r;
}
if (pipe(errPipe) != 0)
{
r.exitCode = 127;
r.err = "pipe(stderr) failed";
close(outPipe[0]);
close(outPipe[1]);
return r;
}
set_cloexec(outPipe[0]);
set_cloexec(outPipe[1]);
set_cloexec(errPipe[0]);
set_cloexec(errPipe[1]);
pid_t pid = fork();
if (pid < 0)
{
r.exitCode = 127;
r.err = "fork failed";
close(outPipe[0]);
close(outPipe[1]);
close(errPipe[0]);
close(errPipe[1]);
return r;
}
if (pid == 0)
{
if (cwd)
{
if (chdir(cwd->c_str()) != 0)
{
_exit(127);
}
}
dup2(outPipe[1], STDOUT_FILENO);
dup2(errPipe[1], STDERR_FILENO);
close(outPipe[0]);
close(outPipe[1]);
close(errPipe[0]);
close(errPipe[1]);
std::vector<char *> argv;
argv.reserve(args.size() + 1);
for (const auto &a : args)
argv.push_back(const_cast<char *>(a.c_str()));
argv.push_back(nullptr);
execvp(argv[0], argv.data());
_exit(127);
}
close(outPipe[1]);
close(errPipe[1]);
r.out = read_fd_all(outPipe[0]);
r.err = read_fd_all(errPipe[0]);
close(outPipe[0]);
close(errPipe[0]);
int status = 0;
while (waitpid(pid, &status, 0) < 0)
{
if (errno == EINTR)
continue;
break;
}
if (WIFEXITED(status))
r.exitCode = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
r.exitCode = 128 + WTERMSIG(status);
else
r.exitCode = 127;
r.out = trim_copy(r.out);
r.err = trim_copy(r.err);
return r;
}
#endif
static ProcessResult run_process_retry(const std::vector<std::string> &args, const std::optional<fs::path> &cwd = std::nullopt, int attempts = 2)
{
ProcessResult last;
for (int i = 0; i < attempts; ++i)
{
last = run_process_capture(args, cwd);
if (last.exitCode == 0)
return last;
if (i + 1 < attempts)
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
return last;
}
static bool command_exists_on_path(const std::string &exe)
{
const auto r = run_process_capture({exe, "--version"});
return r.exitCode == 0;
}
static bool gh_is_authed()
{
const auto r = run_process_capture({"gh", "auth", "status", "-h", "github.com"});
return r.exitCode == 0;
}
/* =========================
Unpublish logic
========================= */
struct Options
{
std::string id; // namespace/name
bool yes{false}; // skip prompt
};
static std::optional<std::pair<std::string, std::string>> split_pkg_id(const std::string &id)
{
const auto pos = id.find('/');
if (pos == std::string::npos)
return std::nullopt;
std::string ns = lower_copy(trim_copy(id.substr(0, pos)));
std::string name = lower_copy(trim_copy(id.substr(pos + 1)));
if (ns.empty() || name.empty())
return std::nullopt;
return std::make_pair(ns, name);
}
static std::string registry_file_name(const std::string &ns, const std::string &name)
{
return ns + "." + name + ".json";
}
static std::string branch_name(const std::string &ns, const std::string &name)
{
return "unpublish-" + ns + "-" + name + "-" + iso_utc_now_compact();
}
static bool confirm_delete(const std::string &pkgId, bool yes)
{
if (yes)
return true;
vix::cli::util::warn_line(std::cout, "This will remove the package from the Vix registry.");
vix::cli::util::warn_line(std::cout, "Changes are auto-merged after validation.");
vix::cli::util::warn_line(std::cout, "Package: " + pkgId);
vix::cli::util::warn_line(std::cout, "Type DELETE to confirm: ");
std::string line;
std::getline(std::cin, line);
line = trim_copy(line);
return line == "DELETE";
}
static Options parse_args_or_throw(const std::vector<std::string> &args)
{
Options opt;
if (args.empty())
throw std::runtime_error("missing package id. Try: vix unpublish namespace/name");
std::vector<std::string> pos;
for (size_t i = 0; i < args.size(); ++i)
{
const auto &a = args[i];
if (a == "-y" || a == "--yes")
{
opt.yes = true;
continue;
}
if (a == "-h" || a == "--help")
throw std::runtime_error("help");
if (!a.empty() && a[0] == '-')
throw std::runtime_error("unknown flag: " + a);
pos.push_back(a);
}
if (pos.empty())
throw std::runtime_error("missing package id");
opt.id = pos[0];
opt.id = trim_copy(opt.id);
if (opt.id.empty())
throw std::runtime_error("package id cannot be empty");
return opt;
}
static int unpublish_impl(const Options &opt)
{
vix::cli::util::section(std::cout, "Unpublish");
const auto parts = split_pkg_id(opt.id);
if (!parts)
{
vix::cli::util::err_line(std::cerr, "invalid id format, expected namespace/name");
return 1;
}
const std::string ns = parts->first;
const std::string name = parts->second;
const std::string pkgId = ns + "/" + name;
vix::cli::util::kv(std::cout, "id", pkgId);
const fs::path regRepo = registry_repo_dir();
const fs::path regIndex = registry_index_dir();
vix::cli::util::kv(std::cout, "registry", regRepo.string());
if (!fs::exists(regRepo) || !is_git_repo(regRepo) || !fs::exists(regIndex))
{
vix::cli::util::err_line(std::cerr, "registry is not available locally");
vix::cli::util::warn_line(std::cerr, "Run: vix registry sync");
return 1;
}
const fs::path entryPath = regIndex / registry_file_name(ns, name);
vix::cli::util::kv(std::cout, "entry", entryPath.string());
if (!fs::exists(entryPath))
{
vix::cli::util::err_line(std::cerr, "registry entry not found locally");
vix::cli::util::warn_line(std::cerr, "Run: vix registry sync");
return 1;
}
if (!confirm_delete(pkgId, opt.yes))
{
vix::cli::util::warn_line(std::cout, "cancelled");
return 0;
}
const std::string branch = branch_name(ns, name);
vix::cli::util::kv(std::cout, "branch", branch);
{
const auto r = run_process_retry({"git", "-C", regRepo.string(), "pull", "-q", "--ff-only"});
if (r.exitCode != 0)
{
vix::cli::util::err_line(std::cerr, "failed to update local registry repo");
if (!r.err.empty())
vix::cli::util::warn_line(std::cerr, r.err);
return r.exitCode;
}
}
{
const auto r = run_process_retry({"git", "-C", regRepo.string(), "checkout", "-B", branch, "-q"});
if (r.exitCode != 0)
{
vix::cli::util::err_line(std::cerr, "failed to create branch");
if (!r.err.empty())
vix::cli::util::warn_line(std::cerr, r.err);
return r.exitCode;
}
}
{
std::error_code ec;
fs::remove(entryPath, ec);
if (ec)
{
vix::cli::util::err_line(std::cerr, "failed to delete entry file");
return 1;
}
}
{
const auto r = run_process_retry({"git", "-C", regRepo.string(), "add", "-A"});
if (r.exitCode != 0)
{
vix::cli::util::err_line(std::cerr, "failed to stage changes");
if (!r.err.empty())
vix::cli::util::warn_line(std::cerr, r.err);
return r.exitCode;
}
}
{
const std::string msg = "registry: unpublish " + pkgId;
const auto r = run_process_retry({"git", "-C", regRepo.string(), "commit", "-q", "-m", msg});
if (r.exitCode != 0)
{
vix::cli::util::err_line(std::cerr, "failed to commit");
if (!r.err.empty())
vix::cli::util::warn_line(std::cerr, r.err);
return r.exitCode;
}
}
{
const auto r = run_process_retry({"git", "-C", regRepo.string(), "push", "-u", "origin", branch});
if (r.exitCode != 0)
{
vix::cli::util::err_line(std::cerr, "failed to push branch");
if (!r.err.empty())
vix::cli::util::warn_line(std::cerr, r.err);
return r.exitCode;
}
}
// Best effort PR creation
if (!command_exists_on_path("gh"))
{
vix::cli::util::warn_line(std::cout, "gh not found, skipping PR creation");
vix::cli::util::warn_line(std::cout, "Create PR manually: vixcpp/registry <- " + branch);
return 0;
}
if (!gh_is_authed())
{
vix::cli::util::warn_line(std::cout, "gh is installed but not authenticated");
vix::cli::util::warn_line(std::cout, "Run: gh auth login");
vix::cli::util::warn_line(std::cout, "Then create PR: vixcpp/registry <- " + branch);
return 0;
}
{
const std::string title = "registry: unpublish " + pkgId;
const std::string body =
"Removes " + pkgId + " from the Vix registry.\n\n"
"Deleted file:\n"
"- index/" +
registry_file_name(ns, name) + "\n";
const auto r = run_process_retry({"gh", "pr", "create",
"--repo", "vixcpp/registry",
"--base", "main",
"--head", branch,
"--title", title,
"--body", body});
if (r.exitCode == 0)
{
vix::cli::util::ok_line(std::cout, "PR created");
return 0;
}
vix::cli::util::warn_line(std::cout, "gh pr create failed, continuing");
if (!r.err.empty())
vix::cli::util::warn_line(std::cout, r.err);
vix::cli::util::warn_line(std::cout, "Create PR manually: vixcpp/registry <- " + branch);
return 0;
}
}
} // namespace
int UnpublishCommand::run(const std::vector<std::string> &args)
{
try
{
const Options opt = parse_args_or_throw(args);
return unpublish_impl(opt);
}
catch (const std::exception &ex)
{
if (std::string(ex.what()) == "help")
return help();
vix::cli::util::err_line(std::cerr, std::string("unpublish failed: ") + ex.what());
return 1;
}
}
int UnpublishCommand::help()
{
std::cout
<< "Usage:\n"
<< " vix unpublish <namespace/name> [-y|--yes]\n\n"
<< "Description:\n"
<< " Remove a package from the Vix registry (auto-merged after validation).\n\n"
<< "Options:\n"
<< " -y, --yes Skip confirmation prompt\n\n"
<< "Requirements:\n"
<< " - Local registry must be synced (vix registry sync)\n\n"
<< "Confirmation:\n"
<< " - You must type DELETE to confirm\n\n"
<< "Examples:\n"
<< " vix unpublish gaspardkirira/strings\n"
<< " vix unpublish gaspardkirira/strings --yes\n";
return 0;
}
} // namespace vix::commands