homectl: split out helper parse_rebalance_weight

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-01-13 17:59:54 +01:00
parent a161867b00
commit 0c2199b4cc
+38 -31
View File
@@ -3373,6 +3373,41 @@ static int parse_auto_resize_mode_field(sd_json_variant **identity, const char *
return parse_string_field(identity, field, arg);
}
static int parse_rebalance_weight(sd_json_variant **identity, const char *field, const char *arg) {
int r;
assert(identity);
assert(field);
if (isempty(arg))
return drop_from_identity(field);
uint64_t u;
if (streq(arg, "off"))
u = REBALANCE_WEIGHT_OFF;
else {
r = safe_atou64(arg, &u);
if (r < 0)
return log_error_errno(r, "Failed to parse rebalance weight parameter: %s", arg);
if (u < REBALANCE_WEIGHT_MIN || u > REBALANCE_WEIGHT_MAX)
return log_error_errno(SYNTHETIC_ERRNO(ERANGE),
"Rebalancing weight out of valid range %" PRIu64 "%s%" PRIu64 ": %s",
REBALANCE_WEIGHT_MIN, glyph(GLYPH_ELLIPSIS), REBALANCE_WEIGHT_MAX,
arg);
}
/* Drop from per machine stuff and everywhere */
r = drop_from_identity(field);
if (r < 0)
return r;
r = sd_json_variant_set_field_unsigned(identity, field, u);
if (r < 0)
return log_error_errno(r, "Failed to set %s field: %m", field);
return 0;
}
static int parse_rlimit_field(sd_json_variant **identity, const char *field, const char *arg) {
int r;
@@ -4636,40 +4671,12 @@ static int parse_argv(int argc, char *argv[]) {
return r;
break;
case ARG_REBALANCE_WEIGHT: {
uint64_t u;
if (isempty(optarg)) {
r = drop_from_identity("rebalanceWeight");
if (r < 0)
return r;
break;
}
if (streq(optarg, "off"))
u = REBALANCE_WEIGHT_OFF;
else {
r = safe_atou64(optarg, &u);
if (r < 0)
return log_error_errno(r, "Failed to parse --rebalance-weight= argument: %s", optarg);
if (u < REBALANCE_WEIGHT_MIN || u > REBALANCE_WEIGHT_MAX)
return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Rebalancing weight out of valid range %" PRIu64 "%s%" PRIu64 ": %s",
REBALANCE_WEIGHT_MIN, glyph(GLYPH_ELLIPSIS), REBALANCE_WEIGHT_MAX, optarg);
}
/* Drop from per machine stuff and everywhere */
r = drop_from_identity("rebalanceWeight");
case ARG_REBALANCE_WEIGHT:
r = parse_rebalance_weight(match_identity ?: &arg_identity_extra,
"rebalanceWeight", optarg);
if (r < 0)
return r;
/* Add to main identity */
r = sd_json_variant_set_field_unsigned(match_identity ?: &arg_identity_extra, "rebalanceWeight", u);
if (r < 0)
return log_error_errno(r, "Failed to set rebalanceWeight field: %m");
break;
}
case 'j':
arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO;