hostnamectl: deprecate set-* methods and expose getters by only using nouns in commands

This commit is contained in:
Jakub Warczarek 2021-04-23 18:41:45 +00:00 committed by Lennart Poettering
parent c2503e359a
commit 572c55ee3c
4 changed files with 96 additions and 89 deletions

View file

@ -63,14 +63,16 @@
<varlistentry> <varlistentry>
<term><command>status</command></term> <term><command>status</command></term>
<listitem><para>Show current system hostname and related information. If no command is specified, <listitem><para>Show system hostname and related information. If no command is specified,
this is the implied default.</para></listitem> this is the implied default.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><command>set-hostname <replaceable>NAME</replaceable></command></term> <term><command>hostname</command> [<replaceable>NAME</replaceable>]</term>
<listitem><para>Set the system hostname to <replaceable>NAME</replaceable>. By default, this will alter the <listitem><para>If no argument is given, print the system hostname. If an
optional argument <replaceable>NAME</replaceable> is provided then the command changes the
system hostname to <replaceable>NAME</replaceable>. By default, this will alter the
pretty, the static, and the transient hostname alike; however, if one or more of <option>--static</option>, pretty, the static, and the transient hostname alike; however, if one or more of <option>--static</option>,
<option>--transient</option>, <option>--pretty</option> are used, only the selected hostnames are changed. If <option>--transient</option>, <option>--pretty</option> are used, only the selected hostnames are changed. If
the pretty hostname is being set, and static or transient are being set as well, the specified hostname will be the pretty hostname is being set, and static or transient are being set as well, the specified hostname will be
@ -82,35 +84,29 @@
<para>The static and transient hostnames must each be either a single DNS label (a string composed of <para>The static and transient hostnames must each be either a single DNS label (a string composed of
7-bit ASCII lower-case characters and no spaces or dots, limited to the format allowed for DNS domain 7-bit ASCII lower-case characters and no spaces or dots, limited to the format allowed for DNS domain
name labels), or a sequence of such labels separated by single dots that forms a valid DNS FQDN. The name labels), or a sequence of such labels separated by single dots that forms a valid DNS FQDN. The
hostname must be at most 64 characters, which is a Linux limitation (DNS allows longer names).</para> hostname must be at most 64 characters, which is a Linux limitation (DNS allows longer names).</para></listitem>
<para>Pass the empty string <literal></literal> as the hostname to reset the selected hostnames to
their default (usually <literal>&FALLBACK_HOSTNAME;</literal>).</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><command>set-icon-name <replaceable>NAME</replaceable></command></term> <term><command>icon-name</command> [<replaceable>NAME</replaceable>]</term>
<listitem><para>Set the system icon name to <listitem><para>If no argument is given, print the icon name of the system. If an
<replaceable>NAME</replaceable>. The icon name is used by some optional argument <replaceable>NAME</replaceable> is provided then the command changes the
icon name to <replaceable>NAME</replaceable>. The icon name is used by some
graphical applications to visualize this host. The icon name graphical applications to visualize this host. The icon name
should follow the <ulink should follow the <ulink
url="http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html">Icon url="http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html">Icon
Naming Specification</ulink>.</para> Naming Specification</ulink>.</para></listitem>
<para>Pass an empty string to reset the icon name to the
default value, which is determined from chassis type (see
below) and possibly other parameters.</para></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><command>set-chassis <replaceable>TYPE</replaceable></command></term> <term><command>chassis</command> [<replaceable>TYPE</replaceable>]</term>
<listitem><para>Set the chassis type to <listitem><para>If no argument is given, print the chassis type. If an
<replaceable>TYPE</replaceable>. The chassis type is used by optional argument <replaceable>TYPE</replaceable> is provided then the command changes the
some graphical applications to visualize the host or alter chassis type to <replaceable>TYPE</replaceable>. The chassis type is used by
user interaction. Currently, the following chassis types are some graphical applications to visualize the host or alter user interaction.
defined: Currently, the following chassis types are defined:
<literal>desktop</literal>, <literal>desktop</literal>,
<literal>laptop</literal>, <literal>laptop</literal>,
<literal>convertible</literal>, <literal>convertible</literal>,
@ -123,43 +119,36 @@
<literal>vm</literal> and <literal>vm</literal> and
<literal>container</literal> for virtualized systems that lack <literal>container</literal> for virtualized systems that lack
an immediate physical chassis.</para> an immediate physical chassis.</para>
<para>Pass an empty string to reset the chassis type to the
default value which is determined from the firmware and
possibly other parameters.</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><command>set-deployment <replaceable>ENVIRONMENT</replaceable></command></term> <term><command>deployment</command> [<replaceable>ENVIRONMENT</replaceable>]</term>
<listitem><para>Set the deployment environment description. <listitem><para>If no argument is given, print the deployment environment. If an
<replaceable>ENVIRONMENT</replaceable> must be a single word optional argument <replaceable>ENVIRONMENT</replaceable> is provided then the command changes the
without any control characters. One of the following is deployment environment to <replaceable>ENVIRONMENT</replaceable>.
suggested: Argument <replaceable>ENVIRONMENT</replaceable>
must be a single word without any control characters. One of the following is suggested:
<literal>development</literal>, <literal>development</literal>,
<literal>integration</literal>, <literal>integration</literal>,
<literal>staging</literal>, <literal>staging</literal>,
<literal>production</literal>. <literal>production</literal>.
</para> </para>
<para>Pass an empty string to reset to the default empty
value.</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><command>set-location <replaceable>LOCATION</replaceable></command></term> <term><command>location</command> [<replaceable>LOCATION</replaceable>]</term>
<listitem><para>Set the location string for the system, if it <listitem><para>If no argument is given, print the location string for the system. If an
is known. <replaceable>LOCATION</replaceable> should be a optional argument <replaceable>LOCATION</replaceable> is provided then the command changes the
location string for the system to <replaceable>LOCATION</replaceable>.
Argument <replaceable>LOCATION</replaceable> should be a
human-friendly, free-form string describing the physical human-friendly, free-form string describing the physical
location of the system, if it is known and applicable. This location of the system, if it is known and applicable. This
may be as generic as <literal>Berlin, Germany</literal> or as may be as generic as <literal>Berlin, Germany</literal> or as
specific as <literal>Left Rack, 2nd Shelf</literal>.</para> specific as <literal>Left Rack, 2nd Shelf</literal>.</para>
<para>Pass an empty string to reset to the default empty
value.</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
</variablelist> </variablelist>

View file

@ -38,9 +38,9 @@ _hostnamectl() {
local -A VERBS=( local -A VERBS=(
[STANDALONE]='status' [STANDALONE]='status'
[ICONS]='set-icon-name' [ICONS]='icon-name'
[NAME]='set-hostname set-deployment set-location' [NAME]='hostname deployment location'
[CHASSIS]='set-chassis' [CHASSIS]='chassis'
) )
for ((i=0; i < COMP_CWORD; i++)); do for ((i=0; i < COMP_CWORD; i++)); do

View file

@ -1,8 +1,8 @@
#compdef hostnamectl -*- shell-script -*- #compdef hostnamectl -*- shell-script -*-
# SPDX-License-Identifier: LGPL-2.1-or-later # SPDX-License-Identifier: LGPL-2.1-or-later
(( $+functions[_hostnamectl_set-hostname] )) || (( $+functions[_hostnamectl_hostname] )) ||
_hostnamectl_set-hostname() { _hostnamectl_hostname() {
if (( CURRENT <= 3 )); then if (( CURRENT <= 3 )); then
_message "new hostname" _message "new hostname"
else else
@ -10,8 +10,8 @@ _hostnamectl_set-hostname() {
fi fi
} }
(( $+functions[_hostnamectl_set-icon-name] )) || (( $+functions[_hostnamectl_icon-name] )) ||
_hostnamectl_set-icon-name() { _hostnamectl_icon-name() {
if (( CURRENT <= 3 )); then if (( CURRENT <= 3 )); then
_message "new icon name" _message "new icon name"
else else
@ -19,8 +19,8 @@ _hostnamectl_set-icon-name() {
fi fi
} }
(( $+functions[_hostnamectl_set-chassis] )) || (( $+functions[_hostnamectl_chassis] )) ||
_hostnamectl_set-chassis() { _hostnamectl_chassis() {
if (( CURRENT <= 3 )); then if (( CURRENT <= 3 )); then
_chassis=( desktop laptop convertible server tablet handset watch embedded vm container ) _chassis=( desktop laptop convertible server tablet handset watch embedded vm container )
_describe chassis _chassis _describe chassis _chassis
@ -29,8 +29,8 @@ _hostnamectl_set-chassis() {
fi fi
} }
(( $+functions[_hostnamectl_set-deployment] )) || (( $+functions[_hostnamectl_deployment] )) ||
_hostnamectl_set-deployment() { _hostnamectl_deployment() {
if (( CURRENT <= 3 )); then if (( CURRENT <= 3 )); then
_message "new environment" _message "new environment"
else else
@ -38,8 +38,8 @@ _hostnamectl_set-deployment() {
fi fi
} }
(( $+functions[_hostnamectl_set-location] )) || (( $+functions[_hostnamectl_location] )) ||
_hostnamectl_set-location() { _hostnamectl_location() {
if (( CURRENT <= 3 )); then if (( CURRENT <= 3 )); then
_message "new location" _message "new location"
else else
@ -52,11 +52,11 @@ _hostnamectl_commands() {
local -a _hostnamectl_cmds local -a _hostnamectl_cmds
_hostnamectl_cmds=( _hostnamectl_cmds=(
"status:Show current hostname settings" "status:Show current hostname settings"
"set-hostname:Set system hostname" "hostname:Get/set system hostname"
"set-icon-name:Set icon name for host" "icon-name:Get/set icon name for host"
"set-chassis:Set chassis type for host" "chassis:Get/set chassis type for host"
"set-deployment:Set deployment environment for host" "deployment:Get/set deployment environment for host"
"set-location:Set location for host" "location:Get/set location for host"
) )
if (( CURRENT == 1 )); then if (( CURRENT == 1 )); then
_describe -t commands 'hostnamectl commands' _hostnamectl_cmds || compadd "$@" _describe -t commands 'hostnamectl commands' _hostnamectl_cmds || compadd "$@"

View file

@ -318,6 +318,19 @@ static int show_all_names(sd_bus *bus) {
return print_status_info(&info); return print_status_info(&info);
} }
static int get_hostname_based_on_flag(sd_bus *bus) {
const char *attr;
if (!!arg_static + !!arg_pretty + !!arg_transient > 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Cannot query more than one name type at a time");
attr = arg_pretty ? "PrettyHostname" :
arg_static ? "StaticHostname" : "Hostname";
return get_one_name(bus, attr, NULL);
}
static int show_status(int argc, char **argv, void *userdata) { static int show_status(int argc, char **argv, void *userdata) {
sd_bus *bus = userdata; sd_bus *bus = userdata;
int r; int r;
@ -352,22 +365,13 @@ static int show_status(int argc, char **argv, void *userdata) {
return 0; return 0;
} }
if (arg_pretty || arg_static || arg_transient) { if (arg_pretty || arg_static || arg_transient)
const char *attr; return get_hostname_based_on_flag(bus);
if (!!arg_static + !!arg_pretty + !!arg_transient > 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Cannot query more than one name type at a time");
attr = arg_pretty ? "PrettyHostname" :
arg_static ? "StaticHostname" : "Hostname";
return get_one_name(bus, attr, NULL);
}
return show_all_names(bus); return show_all_names(bus);
} }
static int set_simple_string_internal(sd_bus *bus, sd_bus_error *error, const char *target, const char *method, const char *value) { static int set_simple_string_internal(sd_bus *bus, sd_bus_error *error, const char *target, const char *method, const char *value) {
_cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
int r; int r;
@ -481,20 +485,29 @@ static int set_hostname(int argc, char **argv, void *userdata) {
return ret; return ret;
} }
static int set_icon_name(int argc, char **argv, void *userdata) { static int get_or_set_hostname(int argc, char **argv, void *userdata) {
return set_simple_string(userdata, "icon", "SetIconName", argv[1]); return argc == 1 ? get_hostname_based_on_flag(userdata) :
set_hostname(argc, argv, userdata);
} }
static int set_chassis(int argc, char **argv, void *userdata) { static int get_or_set_icon_name(int argc, char **argv, void *userdata) {
return set_simple_string(userdata, "chassis", "SetChassis", argv[1]); return argc == 1 ? get_one_name(userdata, "IconName", NULL) :
set_simple_string(userdata, "icon", "SetIconName", argv[1]);
} }
static int set_deployment(int argc, char **argv, void *userdata) { static int get_or_set_chassis(int argc, char **argv, void *userdata) {
return set_simple_string(userdata, "deployment", "SetDeployment", argv[1]); return argc == 1 ? get_one_name(userdata, "Chassis", NULL) :
set_simple_string(userdata, "chassis", "SetChassis", argv[1]);
} }
static int set_location(int argc, char **argv, void *userdata) { static int get_or_set_deployment(int argc, char **argv, void *userdata) {
return set_simple_string(userdata, "location", "SetLocation", argv[1]); return argc == 1 ? get_one_name(userdata, "Deployment", NULL) :
set_simple_string(userdata, "deployment", "SetDeployment", argv[1]);
}
static int get_or_set_location(int argc, char **argv, void *userdata) {
return argc == 1 ? get_one_name(userdata, "Location", NULL) :
set_simple_string(userdata, "location", "SetLocation", argv[1]);
} }
static int help(void) { static int help(void) {
@ -509,11 +522,11 @@ static int help(void) {
"%sQuery or change system hostname.%s\n" "%sQuery or change system hostname.%s\n"
"\nCommands:\n" "\nCommands:\n"
" status Show current hostname settings\n" " status Show current hostname settings\n"
" set-hostname NAME Set system hostname\n" " hostname [NAME] Get/set system hostname\n"
" set-icon-name NAME Set icon name for host\n" " icon-name [NAME] Get/set icon name for host\n"
" set-chassis NAME Set chassis type for host\n" " chassis [NAME] Get/set chassis type for host\n"
" set-deployment NAME Set deployment environment for host\n" " deployment [NAME] Get/set deployment environment for host\n"
" set-location NAME Set location for host\n" " location [NAME] Get/set location for host\n"
"\nOptions:\n" "\nOptions:\n"
" -h --help Show this help\n" " -h --help Show this help\n"
" --version Show package version\n" " --version Show package version\n"
@ -623,13 +636,18 @@ static int parse_argv(int argc, char *argv[]) {
static int hostnamectl_main(sd_bus *bus, int argc, char *argv[]) { static int hostnamectl_main(sd_bus *bus, int argc, char *argv[]) {
static const Verb verbs[] = { static const Verb verbs[] = {
{ "status", VERB_ANY, 1, VERB_DEFAULT, show_status }, { "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
{ "set-hostname", 2, 2, 0, set_hostname }, { "hostname", VERB_ANY, 2, 0, get_or_set_hostname },
{ "set-icon-name", 2, 2, 0, set_icon_name }, { "set-hostname", 2, 2, 0, get_or_set_hostname }, /* obsolete */
{ "set-chassis", 2, 2, 0, set_chassis }, { "icon-name", VERB_ANY, 2, 0, get_or_set_icon_name },
{ "set-deployment", 2, 2, 0, set_deployment }, { "set-icon-name", 2, 2, 0, get_or_set_icon_name }, /* obsolete */
{ "set-location", 2, 2, 0, set_location }, { "chassis", VERB_ANY, 2, 0, get_or_set_chassis },
{ "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */ { "set-chassis", 2, 2, 0, get_or_set_chassis }, /* obsolete */
{ "deployment", VERB_ANY, 2, 0, get_or_set_deployment },
{ "set-deployment", 2, 2, 0, get_or_set_deployment }, /* obsolete */
{ "location", VERB_ANY, 2, 0, get_or_set_location },
{ "set-location", 2, 2, 0, get_or_set_location }, /* obsolete */
{ "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
{} {}
}; };