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