Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mqboard: add username/password support #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mqboard/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def ticks():

# MQTT is the engine for performing commands over ... MQTT
class MQTT:
def __init__(self, server, port, tls, topic, timeout, debug):
def __init__(self, server, port, tls, username, password, topic, timeout, debug):
self._server = server
if not port:
port = 8883 if tls else 1883
Expand All @@ -30,6 +30,8 @@ def __init__(self, server, port, tls, topic, timeout, debug):
# _mqclient.enable_logger(logging.getLogger("paho-mqtt"))
if tls:
self._mqclient.tls_set()
if username:
self._mqclient.username_pw_set(username, password)

def debug(self, msg):
if self._debug:
Expand Down
20 changes: 18 additions & 2 deletions mqboard/mqboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ def global_opts(f):
show_default=True,
show_envvar=True,
)
@click.option(
"--username",
envvar="MQBOARD_USERNAME",
required=False,
help="MQTT username, default to unauthenticated.",
metavar="USERNAME",
show_envvar=True,
)
@click.option(
"--password",
envvar="MQBOARD_PASSWORD",
required=False,
help="MQTT password, default to unauthenticated. Only used if username is also set.",
metavar="PASSWORD",
show_envvar=True,
)
@click.option(
"--timeout",
"-T",
Expand Down Expand Up @@ -105,7 +121,7 @@ def get_topic(prefix, topic):
@click.group()
@global_opts
@click.pass_context
def cli(ctx, server, port, tls, timeout, verbose, prefix, topic):
def cli(ctx, server, port, tls, username, password, timeout, verbose, prefix, topic):
"""mqboard - MQTT MicroPython Tool

Mqboard controls MicroPython boards over MQTT. It can manipulate files on
Expand All @@ -116,7 +132,7 @@ def cli(ctx, server, port, tls, timeout, verbose, prefix, topic):
"""
ctx.ensure_object(dict)
topic = get_topic(prefix, topic)
ctx.obj["engine"] = engine.MQTT(server, port, tls, topic, timeout, verbose)
ctx.obj["engine"] = engine.MQTT(server, port, tls, username, password, topic, timeout, verbose)


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions mqboard/mqsync
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import mqboard, engine, sync
@click.argument("spec", type=click.File("r"), metavar="SPEC_FILE")
@mqboard.global_opts
@click.pass_context
def cli(ctx, server, port, tls, timeout, verbose, prefix, topic, dry_run, spec):
def cli(ctx, server, port, tls, username, password, timeout, verbose, prefix, topic, dry_run, spec):
"""mqsync - MQTT MicroPython File Synchronization Tool

"""
ctx.ensure_object(dict)
topic = mqboard.get_topic(prefix, topic)
eng = engine.MQTT(server, port, tls, topic, timeout, verbose)
eng = engine.MQTT(server, port, tls, username, password, topic, timeout, verbose)
sync.do_sync(eng, spec, dry_run)

if __name__ == "__main__":
Expand Down