Passed
Push — main ( 175dfc...6e2127 )
by Jace
45:23 queued 02:23
created

slackoff.cli.main()   B

Complexity

Conditions 7

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 31
rs 7.9279
c 0
b 0
f 0
cc 7
nop 4
1
import sys
2
import time
3
from contextlib import suppress
4
5
import click
6
import log
7
import pync
8
9
from . import __version__, browser, slack
10
from .config import settings
11
12
13
def clean_channel(_ctx, _param, value: str | None) -> str:
14
    if value is None:
15
        return ""
16
    if value := value.strip("# "):
17
        return value
18
    raise click.BadParameter("Invalid channel name.")
19
20
21
@click.command(help="Automatically sign out/in of a Slack workspace.")
22
@click.argument("workspace", nargs=-1)
23
@click.option(
24
    "-i", "--signin", is_flag=True, default=False, help="Only attempt to sign in."
25
)
26
@click.option(
27
    "-o", "--signout", is_flag=True, default=False, help="Only attempt to sign out."
28
)
29
@click.option(
30
    "-m", "--mute", callback=clean_channel, help="Mute the specified channel."
31
)
32
@click.option(
33
    "-u", "--unmute", callback=clean_channel, help="Unmute the specified channel."
34
)
35
@click.option(
36
    "--debug", is_flag=True, default=False, help="Show verbose logging output."
37
)
38
@click.version_option(__version__)
39
@click.help_option("-h", "--help")
40
def main(
41
    workspace: str, signin: bool, signout: bool, mute: str, unmute: str, debug: bool
42
):
43
    log.init(debug=debug, format="%(levelname)s: %(message)s")
44
45
    explicit = bool(workspace)
46
    workspace = get_workspace(workspace)
47
48
    if not (signin or signout) and not slack.activate():
49
        sys.exit(1)
50
51
    if signin:
52
        code = 0 if attempt_signin(workspace) else 2
53
        sys.exit(code)
54
55
    if signout:
56
        attempt_signout(workspace)
57
        sys.exit(0)
58
59
    if mute:
60
        code = 0 if attempt_mute(workspace, explicit, mute) else 2
61
        sys.exit(code)
62
63
    if unmute:
64
        code = 0 if attempt_unmute(workspace, explicit, unmute) else 2
65
        sys.exit(code)
66
67
    if not attempt_signout(workspace):
68
        click.echo(f"Signing in to {workspace}")
69
        attempt_signin(workspace)
70
71
72
def get_workspace(workspace: str) -> str:
73
    workspace = " ".join(workspace)
74
75
    if not workspace:
76
        if settings.workspaces:
77
            workspace = settings.workspaces[0].name
78
        else:
79
            workspace = click.prompt("Slack workspace")
80
81
    log.debug(f"Modifying workspace: {workspace}")
82
    return workspace
83
84
85
def attempt_signin(workspace) -> bool:
86
    if not slack.signin(workspace):
87
        message = f"Click 'Open' to sign in to {workspace}"
88
89
        if not slack.ready(workspace):
90
            pync.notify(message, title="Slackoff")
91
            click.echo(message + "...")
92
93
        with suppress(KeyboardInterrupt):
94
            while not slack.ready(workspace):
95
                log.debug(f"Waiting for workspace: {workspace}")
96
                time.sleep(1)
97
            click.echo(f"Signed in to of {workspace}")
98
99
        browser.close()
100
101
    settings.activate(workspace)
102
    return True
103
104
105
def attempt_signout(workspace) -> bool:
106
    if slack.signout(workspace):
107
        click.echo(f"Signed out of {workspace}")
108
        settings.deactivate(workspace)
109
        log.debug("Waiting for extra browser tabs")
110
        time.sleep(10)
111
        browser.close()
112
        return True
113
114
    click.echo(f"Currently signed out of {workspace}")
115
    return False
116
117
118
def attempt_mute(workspace: str, explicit: bool, channel: str) -> bool:
119
    ready = slack.ready(workspace)
120
    if explicit:
121
        if not ready:
122
            ready = attempt_signin(workspace)
123
        if not ready:
124
            click.echo(f"Workspace not available: {workspace}")
125
            return False
126
    elif not ready:
127
        workspace = "current workspace"
128
129
    click.echo(f"Muting #{channel} in {workspace}")
130
    return slack.mute(workspace, channel)
131
132
133
def attempt_unmute(workspace: str, explicit: bool, channel: str) -> bool:
134
    ready = slack.ready(workspace)
135
    if explicit:
136
        if not ready:
137
            ready = attempt_signin(workspace)
138
        if not ready:
139
            click.echo(f"Workspace not available: {workspace}")
140
            return False
141
    elif not ready:
142
        workspace = "current workspace"
143
144
    click.echo(f"Unmuting #{channel} in {workspace}")
145
    return slack.unmute(workspace, channel)
146
147
148
if __name__ == "__main__":  # pragma: no cover
149
    main()  # pylint: disable=no-value-for-parameter
150