Skip to content

Commit 7f9f798

Browse files
core: Add logic to run psscmd and extract key.
1 parent 23a773c commit 7f9f798

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

zulipterminal/core.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from platform import platform
1313
from types import TracebackType
1414
from typing import Any, Dict, List, Optional, Tuple, Type, Union
15+
import configparser
16+
import subprocess
1517

1618
import pyperclip
1719
import urwid
@@ -98,7 +100,7 @@ def __init__(
98100

99101
self.show_loading()
100102
client_identifier = f"ZulipTerminal/{ZT_VERSION} {platform()}"
101-
self.client = zulip.Client(config_file=config_file, client=client_identifier)
103+
self.client = zulip.Client(config_file=config_file, client=client_identifier,api_key=self.get_api_key(config_file))
102104
self.model = Model(self)
103105
self.view = View(self)
104106
# Start polling for events after view is rendered.
@@ -137,6 +139,25 @@ def raise_exception_in_main_thread(
137139
self._exception_info = exc_info
138140
self._critical_exception = critical
139141
os.write(self._exception_pipe, b"1")
142+
143+
def get_api_key(self,config_file: str) -> Optional[str]:
144+
config_file = os.path.expanduser(config_file)
145+
if config_file is not None and os.path.exists(config_file):
146+
config = configparser.ConfigParser()
147+
with open(config_file) as f:
148+
config.read_file(f, config_file)
149+
if config.has_section("api") and config.has_option("api", "passcmd"):
150+
result = subprocess.run(config["api"]["passcmd"].split(),capture_output=True)
151+
if result.returncode == 0:
152+
return result.stdout.decode().strip()
153+
else :
154+
raise RuntimeError("Error: Unable to retrieve API key.")
155+
else:
156+
raise ValueError("Error: Invalid config file format.")
157+
else:
158+
raise FileNotFoundError(f"Error: Config file '{config_file}' not found.")
159+
160+
140161

141162
def is_in_editor_mode(self) -> bool:
142163
return self._editor is not None

0 commit comments

Comments
 (0)