Skip to content

Commit

Permalink
new subcmd for create config file
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Dec 6, 2024
1 parent 4183479 commit 6f0339b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
75 changes: 74 additions & 1 deletion arclet/entari/__main__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from arclet.alconna import Alconna, Args, CommandMeta, Option, command_manager
from importlib.util import find_spec
from pathlib import Path

from arclet.alconna import Alconna, Args, CommandMeta, Option, Subcommand, command_manager
from arclet.alconna.tools.formatter import RichConsoleFormatter

from arclet.entari import Entari

alc = Alconna(
"entari",
Subcommand("new", help_text="新建一个 Entari 配置文件"),
Option("-c|--config", Args["path", str], help_text="指定配置文件路径"),
meta=CommandMeta(
"Entari App Launcher",
Expand All @@ -13,10 +17,79 @@
)


JSON_TEMPLATE = """\
{
"basic": {
"network": [
{
"type": "websocket",
"host": "127.0.0.1",
"port": 5140,
"path": "satori"
}
],
"ignore_self_message": true,
"log_level": "info",
"prefix": ["/"]
},
"plugins": {
"/record_message": true,
"::auto_reload": {
"watch_dirs": ["."]
},
"::echo": true,
"::inspect": true
}
}
"""


YAML_TEMPLATE = """\
basic:
network:
- type: websocket
host: "127.0.0.1"
port: 5140
path: "satori"
ignore_self_message: true
log_level: "info"
prefix: ["/"]
plugins:
/record_message: true
::auto_reload:
watch_dirs: ["."]
::echo: true
::inspect: true
"""


def main():
res = alc()
if not res.matched:
return
command_manager.delete(alc)
if res.find("new"):
if (path := res.query[str]("config.path", None)) is None:
if find_spec("yaml"):
_path = Path.cwd() / "entari.yml"
else:
_path = Path.cwd() / ".entari.json"
else:
_path = Path(path)
if _path.exists():
print(f"{_path} already exists")
return
if _path.suffix.startswith(".json"):
with _path.open("w", encoding="utf-8") as f:
f.write(JSON_TEMPLATE)
print(f"Config file created at {_path}")
return
if _path.suffix in (".yaml", ".yml"):
with _path.open("w", encoding="utf-8") as f:
f.write(YAML_TEMPLATE)
print(f"Config file created at {_path}")
return
print(f"Unsupported file extension: {_path.suffix}")
return
entari = Entari.load(res.query[str]("config.path", None))
entari.run()
2 changes: 2 additions & 0 deletions arclet/entari/command/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def _remove_config_prefix(message: MessageChain):
if message and isinstance(message[0], Text):
text = message[0].text # type: ignore
for prefix in command_prefix:
if not prefix:
return message
if text.startswith(prefix):
message = message.copy()
message[0] = Text(text[len(prefix) :])
Expand Down
7 changes: 6 additions & 1 deletion arclet/entari/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ def reload(self):
@classmethod
def load(cls, path: str | os.PathLike[str] | None = None) -> EntariConfig:
if path is None:
_path = Path.cwd() / ".entari.json"
try:
import yaml

_path = Path.cwd() / "entari.yml"
except ImportError:
_path = Path.cwd() / ".entari.json"
else:
_path = Path(path)
if not _path.exists():
Expand Down

0 comments on commit 6f0339b

Please sign in to comment.