diff libervia/cli/cmd_forums.py @ 4191:5d056d524298

core, doc, cli (forums): new `forums set` commands + doc: - document the fact that if an empty `uri` is used, the forum node is created automatically - new `forums/set` CLI commands and its documentation
author Goffi <goffi@goffi.org>
date Mon, 11 Dec 2023 18:10:27 +0100
parents 47401850dec6
children
line wrap: on
line diff
--- a/libervia/cli/cmd_forums.py	Mon Dec 11 18:07:58 2023 +0100
+++ b/libervia/cli/cmd_forums.py	Mon Dec 11 18:10:27 2023 +0100
@@ -18,11 +18,13 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
+import sys
 from . import base
 from libervia.backend.core.i18n import _
 from libervia.cli.constants import Const as C
 from libervia.cli import common
 from libervia.backend.tools.common.ansi import ANSI as A
+from libervia.frontends.bridge.bridge_frontend import BridgeException
 import codecs
 import json
 
@@ -82,7 +84,7 @@
                 self.args.key,
                 self.profile,
             )
-        except Exception as e:
+        except BridgeException as e:
             if e.classname == "NotFound":
                 forums_json = ""
             else:
@@ -169,11 +171,51 @@
                 self.host.quit(1)
             forums = json.loads(forums_raw)
             await self.output(forums)
+
+
+class Set(base.CommandBase):
+
+    def __init__(self, host):
+        base.CommandBase.__init__(
+            self,
+            host,
+            "set",
+            use_pubsub=True,
+            help=_("set forums"),
+        )
+
+    def add_parser_options(self):
+        self.parser.add_argument(
+            "-k",
+            "--key",
+            default="",
+            help=_("forum key (DEFAULT: default forums)"),
+        )
+
+    async def start(self):
+        forums_raw = sys.stdin.read()
+        try:
+            json.loads(forums_raw)
+        except Exception as e:
+            self.parser.error(f"Invalid JSON, a valid JSON must be used as input: {e}")
+        try:
+            await self.host.bridge.forums_set(
+                forums_raw,
+                self.args.service,
+                self.args.node,
+                self.args.key,
+                self.profile,
+            )
+        except Exception as e:
+            self.disp(f"can't set forums: {e}", error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+        else:
+            self.disp(_("forums have been set"))
             self.host.quit()
 
 
 class Forums(base.CommandBase):
-    subcommands = (Get, Edit)
+    subcommands = (Get, Set, Edit)
 
     def __init__(self, host):
         super(Forums, self).__init__(