Skip to content

Commit 6da7b0d

Browse files
setup: Add logic to write zuliprc and zulip_key.
1 parent 7f9f798 commit 6da7b0d

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

zulipterminal/cli/run.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,26 @@ def fetch_zuliprc(zuliprc_path: str) -> None:
315315
save_zuliprc_failure = _write_zuliprc(
316316
zuliprc_path,
317317
login_id=login_id,
318-
api_key=api_key,
319318
server_url=preferred_realm_url,
320319
)
321-
if not save_zuliprc_failure:
322-
print(f"Generated API key saved at {zuliprc_path}")
320+
zulip_key_path = os.path.join(os.path.dirname(os.path.abspath(zuliprc_path)), 'zulip_key')
321+
save_zulipkey_failure = _write_zulip_key(
322+
zulip_key_path,
323+
api_key=api_key
324+
)
325+
if not save_zulipkey_failure:
326+
print(f"Generated API key saved at {zulip_key_path}")
327+
else:
328+
exit_with_error(save_zuliprc_failure)
329+
330+
if not save_zuliprc_failure :
331+
print(f"Generated config file saved at {zuliprc_path}")
323332
else:
324333
exit_with_error(save_zuliprc_failure)
325334

326335

327336
def _write_zuliprc(
328-
to_path: str, *, login_id: str, api_key: str, server_url: str
337+
to_path: str, *, login_id: str, server_url: str
329338
) -> str:
330339
"""
331340
Writes a zuliprc file, returning a non-empty error string on failure
@@ -335,12 +344,29 @@ def _write_zuliprc(
335344
with open(
336345
os.open(to_path, os.O_CREAT | os.O_WRONLY | os.O_EXCL, 0o600), "w"
337346
) as f:
338-
f.write(f"[api]\nemail={login_id}\nkey={api_key}\nsite={server_url}")
347+
f.write(f"[api]\nemail={login_id}\n# Fill the passcmd field with a command that outputs the API key.\n# The API key is temporarily stored in the 'zulip_key' file at {os.path.dirname(os.path.abspath(to_path))}.\n# After storing the key in a password manager, replace the cmd.\npasscmd=cat zulip_key\nsite={server_url}")
339348
return ""
340349
except FileExistsError:
341350
return f"zuliprc already exists at {to_path}"
342351
except OSError as ex:
343352
return f"{ex.__class__.__name__}: zuliprc could not be created at {to_path}"
353+
354+
def _write_zulip_key(to_path: str, *, api_key: str) -> str:
355+
"""
356+
Writes a zulip_key file, returning a non-empty error string on failure
357+
Only creates new private files; errors if file already exists
358+
"""
359+
360+
try:
361+
with open(
362+
os.open(to_path, os.O_CREAT | os.O_WRONLY | os.O_EXCL, 0o600), "w"
363+
) as f:
364+
f.write(api_key)
365+
return ""
366+
except FileExistsError:
367+
return f"zulip_key already exists at {to_path}"
368+
except OSError as ex:
369+
return f"{ex.__class__.__name__}: zulip_key could not be created at {to_path}"
344370

345371

346372
def parse_zuliprc(zuliprc_str: str) -> Dict[str, SettingData]:

0 commit comments

Comments
 (0)