From e2325fc74cf77bd7abaf533d8dfc1741a16bc096 Mon Sep 17 00:00:00 2001
From: atoav <dh@atoav.com>
Date: Tue, 28 Apr 2020 20:40:56 +0200
Subject: [PATCH] Restructure CLI, add config command

---
 bbbmon/bbbmon.py        | 43 +++++++++++++++++++++++++++++++++++------
 bbbmon/configuration.py |  9 +++++++++
 pyproject.toml          |  2 +-
 3 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/bbbmon/bbbmon.py b/bbbmon/bbbmon.py
index 29f3fd5..34ac6c9 100755
--- a/bbbmon/bbbmon.py
+++ b/bbbmon/bbbmon.py
@@ -11,7 +11,7 @@ from typing import NewType, Optional, Tuple, Iterable, List
 
 # Local module imports
 from bbbmon.xmldict import XmlListConfig, XmlDictConfig
-from bbbmon.configuration import Config, Endpoint, SERVER_PROPERTIES_FILE, Url, Secret
+from bbbmon.configuration import Config, Endpoint, SERVER_PROPERTIES_FILE, Url, Secret, get_user_config_path
 
 
 
@@ -35,6 +35,8 @@ bigbluebutton.web.serverURL=https://bbb.example.com/
 ; bigbluebutton.web.serverURL=https://bbb.test.com/
 """
 
+CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
+
 
 def generate_checksum(call_name: str, query_string: str, secret: Secret) -> str:
     """
@@ -55,7 +57,7 @@ def request_meetings(secret: Secret, bbb_url: Url, user_config_path: str) -> Xml
     call_name = "getMeetings"
     checksum = generate_checksum(call_name, "", secret)
     url = "{}/api/{}?checksum={}".format(bbb_url, call_name, checksum)
-    
+
     try:
         r = requests.get(url)
     except:
@@ -270,8 +272,7 @@ def init_config() -> Optional[Config]:
     the user config path. Display a message if neither of these files exist.
     """
     # Get OS dependend properties file
-    user_config_path = click.get_app_dir("bbbmon")
-    user_config_path = "{}.properties".format(user_config_path)
+    user_config_path = get_user_config_path()
 
     # Check if we are on the server and try to read that properties file first
     if os.path.isfile(SERVER_PROPERTIES_FILE):
@@ -309,7 +310,13 @@ def new_config(user_config_path: str):
 
 
 
-@click.command()
+
+@click.group()
+def main():
+    pass 
+
+@main.command(context_settings=CONTEXT_SETTINGS)
+@click.pass_context
 @click.option('--endpoint', '-e', multiple=True, help="Filter by one or more endpoints as named in the user configuration (e.g. [servername]). Order is respected.")
 @click.option('--watch', '-w', help="Run repeatedly with the given interval in seconds", type=click.IntRange(2, 2147483647, clamp=True))
 @click.option('--leaderboards/--no-leaderboards', default=True, show_default=True, help="Hide or show the meeting leaderboards")
@@ -318,7 +325,8 @@ def new_config(user_config_path: str):
 @click.option('--presenter/--no-presenter', default=True, show_default=True, help="Hide or show the presenters")
 @click.option('--presenter-id/--no-presenter-id', default=True, show_default=True, help="Hide or show the presenter IDs")
 @click.option('--fancy/--no-fancy', default=True, show_default=True, help="Use fancy headers")
-def main(leaderboards, participants, presenter, watch, presenter_id, meetings, endpoint, fancy):
+def meetings(ctx, leaderboards, participants, presenter, watch, presenter_id, meetings, endpoint, fancy):
+    """View currently active meetings"""
     config = init_config()
     config.filter_endpoints(endpoint)
     if watch is not None:
@@ -329,5 +337,28 @@ def main(leaderboards, participants, presenter, watch, presenter_id, meetings, e
         print_overview(config, leaderboards, participants, presenter, presenter_id, meetings, watch, fancy)
 
 
+
+@main.command(context_settings=CONTEXT_SETTINGS)
+@click.pass_context
+@click.option('--edit', is_flag=True, help="Open the config in the default editor")
+@click.option('--print', 'print_', is_flag=True, help="Print the config to stdout")
+@click.option('--path', is_flag=True, help="Print the path to the config")
+def config(ctx, edit, path, print_):
+    """Print, show or edit the config"""
+    user_config_path = get_user_config_path()
+
+    if edit:
+        click.edit(filename=user_config_path)
+    elif path:
+        print(get_user_config_path())
+    elif print_:
+        with open(get_user_config_path(), "r") as f:
+            print(f.read())
+    else:
+        ctx = click.get_current_context()
+        click.echo(ctx.get_help())
+
+
+
 if __name__ == "__main__":
     main()
\ No newline at end of file
diff --git a/bbbmon/configuration.py b/bbbmon/configuration.py
index 9f1516f..f92559c 100644
--- a/bbbmon/configuration.py
+++ b/bbbmon/configuration.py
@@ -13,6 +13,15 @@ Secret = NewType('Secret', str)
 Url    = NewType('Url', str)
 
 
+def get_user_config_path() -> str:
+    """
+    Return the user config path
+    """
+    user_config_path = click.get_app_dir("bbbmon")
+    user_config_path = "{}.properties".format(user_config_path)
+    return user_config_path
+
+
 class Config():
     """
     Holds the Server Configurations for multiple endpoints
diff --git a/pyproject.toml b/pyproject.toml
index cbd0ca4..1ea6c17 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [tool.poetry]
 name = "bbbmon"
-version = "0.1.10"
+version = "0.1.11"
 description = "A small CLI utility to monitor bbb usage"
 authors = ["David Huss <david.huss@hfbk-hamburg.de>"]
 maintainers = ["David Huss <david.huss@hfbk-hamburg.de>"]
-- 
GitLab