Skip to content

Commit

Permalink
Move matrix-raw options into new cli._common module
Browse files Browse the repository at this point in the history
- Create a new module synadm.cli._module holding command options and
  option groups supposed to be shared between commands.
- Move some of groups and options of matrix-raw command into the new
  module since.
  • Loading branch information
JOJ0 committed Oct 2, 2023
1 parent 86bd02e commit d87098b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 22 deletions.
54 changes: 54 additions & 0 deletions synadm/cli/_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
# synadm
# Copyright (C) 2020-2023 Johannes Tiefenbacher
#
# synadm is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# synadm is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"Common CLI options, option groups, helpers and utilities."

import click

from click_option_group import MutuallyExclusiveOptionGroup


def common_opts_raw_command(function):
return click.argument(
"endpoint", type=str
)(
click.option(
"--method", "-m", type=click.Choice(["get", "post", "put", "delete"]),
help="""The HTTP method used for the request.""",
default="get", show_default=True
)(function)
)

data_group_raw_command = MutuallyExclusiveOptionGroup(
"Data",
help=""
)

def data_opts_raw_command(function):
return data_group_raw_command.option(
"--data", "-d", type=str, default='{}', show_default=True,
help="""The JSON string sent in the body of post, put and delete
requests - provided as a string. Make sure to escape it from shell
interpretation by using single quotes. E.g '{"key1": "value1",
"key2": 123}'"""
)( data_group_raw_command.option(
"--data-file", "-f", type=click.File("rt"),
show_default=True,
help="""Read JSON data from file. To read from stdin use "-" as the
filename argument."""
)(function)
)

25 changes: 3 additions & 22 deletions synadm/cli/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import click

from synadm import cli
from synadm.cli._common import common_opts_raw_command, data_opts_raw_command
from click_option_group import optgroup, MutuallyExclusiveOptionGroup


Expand Down Expand Up @@ -72,28 +73,8 @@ def login_cmd(helper, user_id, password):


@matrix.command(name="raw")
@click.argument(
"endpoint", type=str)
@click.option(
"--method", "-m", type=click.Choice(["get", "post", "put", "delete"]),
help="""The HTTP method used for the request.""",
default="get", show_default=True)
@optgroup.group(
"Data input",
cls=MutuallyExclusiveOptionGroup,
help="")
@optgroup.option(
"--data", "-d", type=str, default='{}', show_default=True,
help="""The JSON string sent in the body of post, put and delete requests -
provided as a string. Make sure to escape it from shell interpretation by
using single quotes. E.g '{"key1": "value1", "key2": 123}'
""")
@optgroup.option(
"--data-file", "-f", type=click.File("rt"),
show_default=True,
help="""Read JSON data from file. To read from stdin use "-" as the
filename argument.
""")
@common_opts_raw_command
@data_opts_raw_command
@optgroup.group(
"Matrix token",
cls=MutuallyExclusiveOptionGroup,
Expand Down

0 comments on commit d87098b

Please sign in to comment.