Welcome to little lamb

Code » ssp » commit d15cb32

Add command rename

author Olivier Brunel
2023-04-18 11:58:55 UTC
committer Olivier Brunel
2023-04-19 07:34:19 UTC
parent 3e99c8d6ca9b65ab550873419f5d291b9139b56b

Add command rename

src/ssp/commands.c +2 -0
src/ssp/rename.c +49 -0

diff --git a/src/ssp/commands.c b/src/ssp/commands.c
index e6a2ac8..cd78a49 100644
--- a/src/ssp/commands.c
+++ b/src/ssp/commands.c
@@ -5,11 +5,13 @@
 
 extern struct command command_add;
 extern struct command command_list;
+extern struct command command_rename;
 extern struct command command_show;
 
 struct command *commands[] = {
     &command_add,
     &command_list,
+    &command_rename,
     &command_show,
     0
 };
diff --git a/src/ssp/rename.c b/src/ssp/rename.c
new file mode 100644
index 0000000..4f02089
--- /dev/null
+++ b/src/ssp/rename.c
@@ -0,0 +1,49 @@
+/* This file is part of ssp                             https://lila.oss/ssp
+ * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
+/* SPDX-License-Identifier: GPL-2.0-only */
+#include <errno.h>
+#include <limb/command.h>
+#include <limb/exitcode.h>
+#include <limb/output.h>
+#include "ssp.h"
+
+
+COMMAND(rename, "Rename a site", "<site> <newname>", NULL);
+
+int
+rename_main(int argc, const char *argv[], const char *env[], const char usage[], void *ctx_)
+{
+    struct ssp *ctx = ctx_;
+
+    if (argc != 3)
+        diecmdusage(EX_USAGE, usage, &command_rename);
+
+    const char *site = get_site_name(argv[1], ctx);
+    if (!site)
+        diefusys(exitcode_from_errno(errno), "parse site's name");
+
+    const char *new = get_site_name(argv[2], ctx);
+    if (!new)
+        diefusys(exitcode_from_errno(errno), "parse new name");
+
+    if (!strcmp(site, new))
+        diefu(EX_DATA_ERR, "rename site ", ESC, site, ESC, ": new name is the same");
+
+    int r = open_db(ctx);
+    if (r < 0)
+        diefu(exitcode_from_errno(errno), "open database");
+    if (!r)
+        dief(0, "no database");
+
+    r = db_find(site, ctx);
+    if (r < 0)
+        diefu(EX_DATA_ERR, "find site ", ESC, site, ESC);
+    if (!r) {
+        out("no site ", ESC, site, ESC, " in database");
+    } else {
+        out("Renaming ", ESC, site, ESC, " to ", ESC, new, ESC, "...");
+        rebuild_db(site, new, db_otp(ctx), ctx);
+    }
+
+    return 0;
+}