Skip to content

Commit

Permalink
run: Add -n argument to login to a realm, and create config.
Browse files Browse the repository at this point in the history
If the realm already exists, will exit with an error saying so.

Tests updated.
  • Loading branch information
Niloth-p committed Jun 11, 2024
1 parent 80f5451 commit ee18654
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions tests/cli/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def test_main_help(capsys: CaptureFixture[str], options: str) -> None:
"-h, --help",
"-d, --debug",
"-o, --list-organizations",
"-n, --new-organization",
"--list-themes",
"--profile",
"--config-file CONFIG_FILE, -c CONFIG_FILE",
Expand Down
33 changes: 25 additions & 8 deletions zulipterminal/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ def parse_args(argv: List[str]) -> argparse.Namespace:
action="store_true",
help="list all the organizations that you have configurations for, and exit",
)
parser.add_argument(
"-n",
"--new-organization",
action="store_true",
help="login to a new organization",
)
parser.add_argument(
"--theme",
"-t",
Expand Down Expand Up @@ -293,15 +299,20 @@ def get_api_key(realm_url: str) -> Optional[Tuple[str, str, str, str]]:
return None


def fetch_zuliprc(zuliprc_path: str) -> str:
def fetch_zuliprc(zuliprc_path: str, new_realm: bool) -> str:
locations_checked = (
zuliprc_path
if zuliprc_path != ""
else f"any of the following locations: "
f"{DOWNLOADED_PATH_ZULIPRC} or {ZULIP_CONFIG_PATH} or {HOME_PATH_ZULIPRC}"
)
missing_zuliprc_text = (
""
if new_realm
else f"{in_color('red', f'zuliprc file was not found at {locations_checked}')}"
)
print(
f"{in_color('red', f'zuliprc file was not found at {locations_checked}')}"
f"{missing_zuliprc_text}"
f"\nPlease enter your credentials to login into your Zulip organization."
f"\n"
f"\nNOTE: The {in_color('blue', 'Zulip server URL')}"
Expand Down Expand Up @@ -387,15 +398,21 @@ def check_for_default_zuliprc() -> str:
return ""


def parse_zuliprc(zuliprc_str: str) -> Tuple[Dict[str, SettingData], str]:
def parse_zuliprc(
zuliprc_str: str, new_realm: bool
) -> Tuple[Dict[str, SettingData], str]:
zuliprc_path = (
check_for_default_zuliprc()
if zuliprc_str == ""
else path.expanduser(zuliprc_str)
(
check_for_default_zuliprc()
if zuliprc_str == ""
else path.expanduser(zuliprc_str)
)
if not new_realm
else ""
)
while zuliprc_path == "" or not path.exists(zuliprc_path):
try:
zuliprc_path = fetch_zuliprc(zuliprc_path)
zuliprc_path = fetch_zuliprc(zuliprc_path, new_realm)
# Invalid user inputs (e.g. pressing arrow keys) may cause ValueError
except (OSError, ValueError):
# Remove zuliprc file if created.
Expand Down Expand Up @@ -552,7 +569,7 @@ def main(options: Optional[List[str]] = None) -> None:
)

try:
zterm, zuliprc_path = parse_zuliprc(zuliprc_path)
zterm, zuliprc_path = parse_zuliprc(zuliprc_path, args.new_organization)

### Validate footlinks settings (not from command line)
if (
Expand Down

0 comments on commit ee18654

Please sign in to comment.