forked from vbatts/maubot
Implement uploading plugins
This commit is contained in:
parent
c334afd38b
commit
cb3993d79f
6 changed files with 88 additions and 21 deletions
|
@ -13,12 +13,53 @@
|
|||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
from urllib.request import urlopen, Request
|
||||
from urllib.error import HTTPError
|
||||
from typing import IO, Tuple
|
||||
import json
|
||||
|
||||
from colorama import Fore
|
||||
import click
|
||||
|
||||
from ..base import app
|
||||
from ..config import config
|
||||
|
||||
|
||||
class UploadError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@app.command(help="Upload a maubot plugin")
|
||||
@click.argument("path")
|
||||
@click.option("-s", "--server", help="The maubot instance to upload the plugin to")
|
||||
def upload(server: str) -> None:
|
||||
pass
|
||||
def upload(path: str, server: str) -> None:
|
||||
if not server:
|
||||
try:
|
||||
server = config["default_server"]
|
||||
except KeyError:
|
||||
print(Fore.RED + "Default server not configured" + Fore.RESET)
|
||||
return
|
||||
try:
|
||||
token = config["servers"][server]
|
||||
except KeyError:
|
||||
print(Fore.RED + "Server not found" + Fore.RESET)
|
||||
return
|
||||
with open(path, "rb") as file:
|
||||
upload_file(file, server, token)
|
||||
|
||||
|
||||
def upload_file(file: IO, server: str, token: str) -> None:
|
||||
req = Request(f"{server}/_matrix/maubot/v1/plugins/upload?allow_override=true", data=file,
|
||||
headers={"Authorization": f"Bearer {token}", "Content-Type": "application/zip"})
|
||||
try:
|
||||
with urlopen(req) as resp_data:
|
||||
resp = json.load(resp_data)
|
||||
print(f"{Fore.GREEN}Plugin {Fore.CYAN}{resp['id']} v{resp['version']}{Fore.GREEN} "
|
||||
f"uploaded to {Fore.CYAN}{server}{Fore.GREEN} successfully.{Fore.RESET}")
|
||||
except HTTPError as e:
|
||||
try:
|
||||
err = json.load(e)
|
||||
except json.JSONDecodeError:
|
||||
err = {}
|
||||
print(err.get("stacktrace", ""))
|
||||
print(Fore.RED + "Failed to upload plugin: " + err.get("error", str(e)) + Fore.RESET)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue