systemctl: add "edit --stdin"

This is a fancy wrapper around "cat <<EOF", but:
- the user doesn't need to figure out the file name,
- parent directories are created automatically,
- daemon-reload is implied,
so it's a convenient way to create units or drop-ins.

Closes https://github.com/systemd/systemd/issues/21862.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2023-12-05 18:47:19 +01:00
parent 232f017b1a
commit 329050c5e2
6 changed files with 95 additions and 39 deletions
+6 -1
View File
@@ -317,12 +317,13 @@ int verb_edit(int argc, char *argv[], void *userdata) {
.marker_end = DROPIN_MARKER_END,
.remove_parent = !arg_full,
.overwrite_with_origin = true,
.stdin = arg_stdin,
};
_cleanup_strv_free_ char **names = NULL;
sd_bus *bus;
int r;
if (!on_tty())
if (!on_tty() && !arg_stdin)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units if not on a tty.");
if (arg_transport != BUS_TRANSPORT_LOCAL)
@@ -342,6 +343,10 @@ int verb_edit(int argc, char *argv[], void *userdata) {
if (strv_isempty(names))
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No units matched the specified patterns.");
if (arg_stdin && arg_full && strv_length(names) != 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"With 'edit --stdin --full', exactly one unit for editing must be specified.");
STRV_FOREACH(tmp, names) {
r = unit_is_masked(bus, *tmp);
if (r < 0)