From: Zbigniew Jędrzejewski-Szmek Date: Wed, 10 Dec 2025 14:18:38 +0000 (+0100) Subject: homectl: split out helper parse_environment_field X-Git-Tag: v260-rc1~391^2~14 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0126bebccbd2bd2852b851fe35a023fabb840364;p=thirdparty%2Fsystemd.git homectl: split out helper parse_environment_field --- diff --git a/src/home/homectl.c b/src/home/homectl.c index d407ab0e6c3..67f492d11cb 100644 --- a/src/home/homectl.c +++ b/src/home/homectl.c @@ -3385,6 +3385,40 @@ static int parse_rlimit_field(sd_json_variant **identity, const char *field, con return 0; } +static int parse_environment_field(sd_json_variant **identity, const char *field, const char *arg) { + _cleanup_strv_free_ char **l = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *ne = NULL; + int r; + + assert(identity); + assert(field); + + if (isempty(arg)) + return drop_from_identity(field); + + sd_json_variant *e = sd_json_variant_by_key(*identity, field); + if (e) { + r = sd_json_variant_strv(e, &l); + if (r < 0) + return log_error_errno(r, "Failed to parse JSON environment field: %m"); + } + + r = strv_env_replace_strdup_passthrough(&l, arg); + if (r < 0) + return log_error_errno(r, "Cannot assign environment variable %s: %m", arg); + + strv_sort(l); + + r = sd_json_variant_new_array_strv(&ne, l); + if (r < 0) + return log_error_errno(r, "Failed to allocate json list: %m"); + + r = sd_json_variant_set_field(identity, field, ne); + if (r < 0) + return log_error_errno(r, "Failed to set %s field: %m", field); + return 0; +} + static int help(int argc, char *argv[], void *userdata) { _cleanup_free_ char *link = NULL; int r; @@ -4055,42 +4089,11 @@ static int parse_argv(int argc, char *argv[]) { return r; break; - case ARG_SETENV: { - _cleanup_strv_free_ char **l = NULL; - _cleanup_(sd_json_variant_unrefp) sd_json_variant *ne = NULL; - sd_json_variant *e; - - if (isempty(optarg)) { - r = drop_from_identity("environment"); - if (r < 0) - return r; - - break; - } - - e = sd_json_variant_by_key(match_identity ? *match_identity: arg_identity_extra, "environment"); - if (e) { - r = sd_json_variant_strv(e, &l); - if (r < 0) - return log_error_errno(r, "Failed to parse JSON environment field: %m"); - } - - r = strv_env_replace_strdup_passthrough(&l, optarg); - if (r < 0) - return log_error_errno(r, "Cannot assign environment variable %s: %m", optarg); - - strv_sort(l); - - r = sd_json_variant_new_array_strv(&ne, l); + case ARG_SETENV: + r = parse_environment_field(match_identity ?: &arg_identity_extra, "environment", optarg); if (r < 0) - return log_error_errno(r, "Failed to allocate environment list JSON: %m"); - - r = sd_json_variant_set_field(match_identity ?: &arg_identity_extra, "environment", ne); - if (r < 0) - return log_error_errno(r, "Failed to set environment list: %m"); - + return r; break; - } case ARG_TIMEZONE: if (!isempty(optarg) && !timezone_is_valid(optarg, LOG_DEBUG))