Repository: JMCFTW/outline-cli (language: python)

Issue:
Support multiple outline servers
If get multiple outline server configs in app.ini, then asking user which outline server they want to use.


Discussion / hints:

Fix patch:
diff --git a/example-app.ini b/example-app.ini
index f5340df..8bc0277 100644
--- a/example-app.ini
+++ b/example-app.ini
@@ -1,6 +1,11 @@
 [Gmail]
 EMAIL = your_email
 APP_PASSWORD = your_app_password
-[OutlineVPN]
-certSha256 = your_certSha256
-apiUrl = your_apiUrl
+
+; Support multiple outline servers
+[OutlineVPN-internal]
+certSha256 = internal_certSha256
+apiUrl = internal_apiUrl
+[OutlineVPN-external]
+certSha256 = external_certSha256
+apiUrl = external_apiUrl
diff --git a/src/outline_cli/__init__.py b/src/outline_cli/__init__.py
index d1bbca1..4971585 100644
--- a/src/outline_cli/__init__.py
+++ b/src/outline_cli/__init__.py
@@ -6,18 +6,25 @@
     get_method_arguments,
     get_method_user_want_to_call,
     get_public_methods,
+    get_outline_servers_config,
+    get_outline_server_user_want_to_use,
 )
 
 
-def init_outline():
+def init_outline(section):
     return OutlineVPN(
-        certSha256=get_config_from_app_ini("OutlineVPN", "certSha256"),
-        apiUrl=get_config_from_app_ini("OutlineVPN", "apiUrl"),
+        certSha256=get_config_from_app_ini(section, "certSha256"),
+        apiUrl=get_config_from_app_ini(section, "apiUrl"),
     )
 
 
 def init_cli():
-    outline_client = init_outline()
+    outline_servers = get_outline_servers_config()
+    outline_client = init_outline(
+        section=outline_servers[0]
+        if len(outline_servers) == 1
+        else get_outline_server_user_want_to_use(outline_servers)
+    )
     return outline_client, get_public_methods(outline_client)
 
 
diff --git a/src/outline_cli/helper.py b/src/outline_cli/helper.py
index 7daf864..d33b667 100644
--- a/src/outline_cli/helper.py
+++ b/src/outline_cli/helper.py
@@ -10,6 +10,10 @@
 config.read("app.ini", encoding="utf-8")
 
 
+def get_outline_servers_config():
+    return [c for c in config.sections() if "OutlineVPN" in c]
+
+
 def get_config_from_app_ini(section_name, key):
     return config.get(section_name, key)
 
@@ -22,6 +26,19 @@ def get_public_methods(instance):
     ]
 
 
+def get_outline_server_user_want_to_use(outline_servers):
+    return prompt(
+        [
+            {
+                "type": "list",
+                "name": "outline_server",
+                "message": "Please choose which outline server you want to use? ",
+                "choices": outline_servers,
+            }
+        ]
+    )["outline_server"]
+
+
 def get_method_user_want_to_call(methods):
     return prompt(
         [


Test patch:
diff --git a/src/tests.py b/src/tests.py
index 7d0a5b5..c997de5 100644
--- a/src/tests.py
+++ b/src/tests.py
@@ -182,9 +182,14 @@ class TestMain(unittest.TestCase):
     """Test main"""
 
     def test_init_outline(self):
-        self.assertIsInstance(outline_cli.init_outline(), OutlineVPN)
+        self.assertIsInstance(
+            outline_cli.init_outline(section="OutlineVPN-internal"), OutlineVPN
+        )
+
+    @patch("outline_cli.get_outline_server_user_want_to_use")
+    def test_init_cli(self, mock):
+        mock.return_value = "OutlineVPN-internal"
 
-    def test_init_cli(self):
         client, public_methods = outline_cli.init_cli()
         self.assertIsInstance(client, OutlineVPN)
         self.assertEqual(
@@ -210,8 +215,10 @@ def test_init_cli(self):
 
     @classmethod
     @patch("outline_cli.helper.prompt")
-    def test_start_cli_with_no_args_method(cls, mock):
-        mock.return_value = {"method": "get_server_info"}
+    @patch("outline_cli.get_outline_server_user_want_to_use")
+    def test_start_cli_with_no_args_method(cls, mock, mock2):
+        mock2.return_value = {"method": "get_server_info"}
+        mock.return_value = "OutlineVPN-internal"
         try:
             outline_cli.start_cli()
         except MissingSchema:
@@ -219,8 +226,10 @@ def test_start_cli_with_no_args_method(cls, mock):
 
     @classmethod
     @patch("outline_cli.helper.prompt")
-    def test_start_cli_with_args_method(cls, mock):
-        mock.return_value = {"method": "batch_create_user_by_email_list"}
+    @patch("outline_cli.get_outline_server_user_want_to_use")
+    def test_start_cli_with_args_method(cls, mock, mock2):
+        mock2.return_value = {"method": "batch_create_user_by_email_list"}
+        mock.return_value = "OutlineVPN-external"
         try:
             outline_cli.start_cli()
         except (MissingSchema, TypeError):