Passed
Pull Request — main (#14)
by Jace
57s
created

slackoff.cli.attempt_signout()   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nop 1
1
import sys
2
3
import click
4
import log
5
import pync
6
7
from . import __version__, slack
8
from .config import settings
9
10
11
@click.command(help="Automatically sign out/in of a Slack workspace.")
12
@click.argument("workspace", nargs=-1)
13
@click.option(
14
    "-i", "--signin", is_flag=True, default=False, help="Only attempt to sign in."
15
)
16
@click.option(
17
    "-o", "--signout", is_flag=True, default=False, help="Only attempt to sign out."
18
)
19
@click.option(
20
    "--debug", is_flag=True, default=False, help="Show verbose logging output."
21
)
22
@click.version_option(__version__)
23
def main(workspace: str, signin: bool, signout: bool, debug: bool):
24
    log.init(debug=debug, format="%(levelname)s: %(message)s")
25
26
    workspace = get_workspace(workspace)
27
28
    if not (signin or signout) and not slack.activate():
29
        sys.exit(1)
30
31
    if signin:
32
        attempt_signin(workspace)
33
        sys.exit(0)
34
35
    if signout:
36
        attempt_signout(workspace)
37
        sys.exit(0)
38
39
    if not attempt_signout(workspace):
40
        click.echo(f"Signing in to {workspace}")
41
        attempt_signin(workspace)
42
43
44
def get_workspace(workspace: str) -> str:
45
    workspace = " ".join(workspace)
46
47
    if not workspace:
48
        if settings.workspaces:
49
            workspace = settings.workspaces[0].name
50
        else:
51
            workspace = click.prompt("Slack workspace")
52
53
    log.debug(f"Modifying workspace: {workspace}")
54
    return workspace
55
56
57
def attempt_signin(workspace) -> bool:
58
    if not slack.signin(workspace):
59
        message = f"Click 'Open' to sign in to {workspace}"
60
        pync.notify(message, title="Slackoff")
61
        click.echo(message)
62
        click.pause()
63
        slack.close()  # TODO: Move this to browser utils
64
65
    settings.activate(workspace)
66
    return True
67
68
69
def attempt_signout(workspace) -> bool:
70
    if slack.signout(workspace):
71
        click.echo(f"Signed out of {workspace}")
72
        settings.deactivate(workspace)
73
        return True
74
75
    click.echo(f"Currently signed out of {workspace}")
76
    return False
77
78
79
if __name__ == "__main__":  # pragma: no cover
80
    main()  # pylint: disable=no-value-for-parameter
81