networkctl: support edit --stdin

This commit is contained in:
Mike Yuan
2024-08-12 16:23:23 +02:00
parent 40f5c372c2
commit 119cba7835
7 changed files with 45 additions and 8 deletions
+10 -3
View File
@@ -396,23 +396,30 @@ static int reload_daemons(ReloadFlags flags) {
}
int verb_edit(int argc, char *argv[], void *userdata) {
char **args = ASSERT_PTR(strv_skip(argv, 1));
_cleanup_(edit_file_context_done) EditFileContext context = {
.marker_start = DROPIN_MARKER_START,
.marker_end = DROPIN_MARKER_END,
.remove_parent = !!arg_drop_in,
.stdin = arg_stdin,
};
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
ReloadFlags reload = 0;
int r;
if (!on_tty())
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit network config files if not on a tty.");
if (!on_tty() && !arg_stdin)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit network config files interactively if not on a tty.");
/* Duplicating main configs makes no sense. This also mimics the behavior of systemctl. */
if (arg_stdin && !arg_drop_in && strv_length(args) != 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"When 'edit --stdin' without '--drop-in=', exactly one config file for editing must be specified.");
r = mac_selinux_init();
if (r < 0)
return r;
STRV_FOREACH(name, strv_skip(argv, 1)) {
STRV_FOREACH(name, args) {
_cleanup_strv_free_ char **dropins = NULL;
_cleanup_free_ char *path = NULL;
const char *link_config;
+8
View File
@@ -91,6 +91,7 @@ bool arg_all = false;
bool arg_stats = false;
bool arg_full = false;
bool arg_runtime = false;
bool arg_stdin = false;
unsigned arg_lines = 10;
char *arg_drop_in = NULL;
sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
@@ -3025,6 +3026,7 @@ static int help(void) {
" after editing network config\n"
" --drop-in=NAME Edit specified drop-in instead of main config file\n"
" --runtime Edit runtime config files\n"
" --stdin Read new contents of edited file from stdin\n"
"\nSee the %s for details.\n",
program_invocation_short_name,
ansi_highlight(),
@@ -3043,6 +3045,7 @@ static int parse_argv(int argc, char *argv[]) {
ARG_NO_RELOAD,
ARG_DROP_IN,
ARG_RUNTIME,
ARG_STDIN,
};
static const struct option options[] = {
@@ -3058,6 +3061,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "no-reload", no_argument, NULL, ARG_NO_RELOAD },
{ "drop-in", required_argument, NULL, ARG_DROP_IN },
{ "runtime", no_argument, NULL, ARG_RUNTIME },
{ "stdin", no_argument, NULL, ARG_STDIN },
{}
};
@@ -3092,6 +3096,10 @@ static int parse_argv(int argc, char *argv[]) {
arg_runtime = true;
break;
case ARG_STDIN:
arg_stdin = true;
break;
case ARG_DROP_IN:
if (isempty(optarg))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Empty drop-in file name.");
+1
View File
@@ -15,6 +15,7 @@ extern bool arg_all;
extern bool arg_stats;
extern bool arg_full;
extern bool arg_runtime;
extern bool arg_stdin;
extern unsigned arg_lines;
extern char *arg_drop_in;
extern sd_json_format_flags_t arg_json_format_flags;