laravel-tools/source/lib/command.py

145 lines
2.8 KiB
Python
Raw Normal View History

2026-09-17 17:01:20 +02:00
import lib.string as _string
2026-03-06 08:31:05 +01:00
2026-09-17 17:01:20 +02:00
def generic(
2026-03-06 08:31:05 +01:00
head,
args
):
def arg_process(arg):
if (arg.get("name", None) is None):
if (arg.get("value", None) is None):
template = None
else:
template = "{{value}}"
else:
if (arg.get("value", None) is None):
template = "--{{name}}"
else:
template = "--{{name}}={{value}}"
2026-09-17 17:01:20 +02:00
return _string.coin(
2026-03-06 08:31:05 +01:00
template,
{
"name": (arg.get("name", None) or ""),
2026-09-17 17:01:20 +02:00
"value": _string.wrap(
2026-03-06 08:31:05 +01:00
(arg.get("value", None) or ""),
"'",
"'",
{
"mode": arg.get("quote", False),
}
),
}
)
return " ".join(
[head]
+
list(
map(
arg_process,
args
)
)
)
2026-09-17 17:01:20 +02:00
def directory_create(
2026-03-06 08:31:05 +01:00
path
):
2026-09-17 17:01:20 +02:00
return generic(
2026-03-06 08:31:05 +01:00
"mkdir",
[
{"value": path},
{"name": "parents"},
]
)
2026-09-17 17:01:20 +02:00
def remove(
2026-03-06 08:31:05 +01:00
path
):
2026-09-17 17:01:20 +02:00
return generic(
2026-03-06 08:31:05 +01:00
"rm",
[
{"value": path},
{"name": "recursive"},
{"name": "force"},
]
)
2026-09-17 17:01:20 +02:00
def copy(
2026-03-06 08:31:05 +01:00
path_from,
path_to,
options = None
):
options = (
{
"update": False,
"recursive": True,
}
|
(options or {})
)
2026-09-17 17:01:20 +02:00
return generic(
2026-03-06 08:31:05 +01:00
"cp",
[
{"value": path_from},
{"value": path_to},
]
+
(
[{"name": "update"}]
if options["update"] else
[]
)
+
(
[{"name": "recursive"}]
if options["recursive"] else
[]
)
)
2026-09-17 17:01:20 +02:00
def rsync(
2026-03-06 08:31:05 +01:00
path_from,
path_to,
options = None
):
options = (
{
"verbose": False,
"excludes": [],
}
|
(options or {})
)
2026-09-17 17:01:20 +02:00
return generic(
2026-03-06 08:31:05 +01:00
"rsync",
(
[
{"name": "update"},
{"name": "recursive"},
]
+
(
[{"name": "verbose"}]
if options["verbose"] else
[]
)
+
list(
map(
2026-09-17 17:01:20 +02:00
lambda exclude: {"name": "exclude", "value": exclude, "quote": True},
2026-03-06 08:31:05 +01:00
options["excludes"]
)
)
+
[
2026-09-17 17:01:20 +02:00
{"value": _string.coin("{{path}}/.", {"path": path_from})},
{"value": _string.coin("{{path}}", {"path": path_to})},
2026-03-06 08:31:05 +01:00
]
)
)