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>
<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>
</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>,
<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
@ -82,35 +84,29 @@
<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
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>
<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>
hostname must be at most 64 characters, which is a Linux limitation (DNS allows longer names).</para></listitem>
</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
<replaceable>NAME</replaceable>. The icon name is used by some
<listitem><para>If no argument is given, print the icon name of the system. If an
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
should follow the <ulink
url="http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html">Icon
Naming Specification</ulink>.</para>
<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>
Naming Specification</ulink>.</para></listitem>
</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
<replaceable>TYPE</replaceable>. The chassis type is used by
some graphical applications to visualize the host or alter
user interaction. Currently, the following chassis types are
defined:
<listitem><para>If no argument is given, print the chassis type. If an
optional argument <replaceable>TYPE</replaceable> is provided then the command changes the
chassis type to <replaceable>TYPE</replaceable>. The chassis type is used by
some graphical applications to visualize the host or alter user interaction.
Currently, the following chassis types are defined:
<literal>desktop</literal>,
<literal>laptop</literal>,
<literal>convertible</literal>,
@ -123,43 +119,36 @@
<literal>vm</literal> and
<literal>container</literal> for virtualized systems that lack
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>
</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.
<replaceable>ENVIRONMENT</replaceable> must be a single word
without any control characters. One of the following is
suggested:
<listitem><para>If no argument is given, print the deployment environment. If an
optional argument <replaceable>ENVIRONMENT</replaceable> is provided then the command changes the
deployment environment to <replaceable>ENVIRONMENT</replaceable>.
Argument <replaceable>ENVIRONMENT</replaceable>
must be a single word without any control characters. One of the following is suggested:
<literal>development</literal>,
<literal>integration</literal>,
<literal>staging</literal>,
<literal>production</literal>.
</para>
<para>Pass an empty string to reset to the default empty
value.</para>
</listitem>
</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
is known. <replaceable>LOCATION</replaceable> should be a
<listitem><para>If no argument is given, print the location string for the system. If an
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
location of the system, if it is known and applicable. This
may be as generic as <literal>Berlin, Germany</literal> or as
specific as <literal>Left Rack, 2nd Shelf</literal>.</para>
<para>Pass an empty string to reset to the default empty
value.</para>
</listitem>
</varlistentry>
</variablelist>

View File

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

View File

@ -1,8 +1,8 @@
#compdef hostnamectl -*- shell-script -*-
# SPDX-License-Identifier: LGPL-2.1-or-later
(( $+functions[_hostnamectl_set-hostname] )) ||
_hostnamectl_set-hostname() {
(( $+functions[_hostnamectl_hostname] )) ||
_hostnamectl_hostname() {
if (( CURRENT <= 3 )); then
_message "new hostname"
else
@ -10,8 +10,8 @@ _hostnamectl_set-hostname() {
fi
}
(( $+functions[_hostnamectl_set-icon-name] )) ||
_hostnamectl_set-icon-name() {
(( $+functions[_hostnamectl_icon-name] )) ||
_hostnamectl_icon-name() {
if (( CURRENT <= 3 )); then
_message "new icon name"
else
@ -19,8 +19,8 @@ _hostnamectl_set-icon-name() {
fi
}
(( $+functions[_hostnamectl_set-chassis] )) ||
_hostnamectl_set-chassis() {
(( $+functions[_hostnamectl_chassis] )) ||
_hostnamectl_chassis() {
if (( CURRENT <= 3 )); then
_chassis=( desktop laptop convertible server tablet handset watch embedded vm container )
_describe chassis _chassis
@ -29,8 +29,8 @@ _hostnamectl_set-chassis() {
fi
}
(( $+functions[_hostnamectl_set-deployment] )) ||
_hostnamectl_set-deployment() {
(( $+functions[_hostnamectl_deployment] )) ||
_hostnamectl_deployment() {
if (( CURRENT <= 3 )); then
_message "new environment"
else
@ -38,8 +38,8 @@ _hostnamectl_set-deployment() {
fi
}
(( $+functions[_hostnamectl_set-location] )) ||
_hostnamectl_set-location() {
(( $+functions[_hostnamectl_location] )) ||
_hostnamectl_location() {
if (( CURRENT <= 3 )); then
_message "new location"
else
@ -52,11 +52,11 @@ _hostnamectl_commands() {
local -a _hostnamectl_cmds
_hostnamectl_cmds=(
"status:Show current hostname settings"
"set-hostname:Set system hostname"
"set-icon-name:Set icon name for host"
"set-chassis:Set chassis type for host"
"set-deployment:Set deployment environment for host"
"set-location:Set location for host"
"hostname:Get/set system hostname"
"icon-name:Get/set icon name for host"
"chassis:Get/set chassis type for host"
"deployment:Get/set deployment environment for host"
"location:Get/set location for host"
)
if (( CURRENT == 1 )); then
_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);
}
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) {
sd_bus *bus = userdata;
int r;
@ -352,22 +365,13 @@ static int show_status(int argc, char **argv, void *userdata) {
return 0;
}
if (arg_pretty || arg_static || arg_transient) {
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);
}
if (arg_pretty || arg_static || arg_transient)
return get_hostname_based_on_flag(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) {
_cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
int r;
@ -481,20 +485,29 @@ static int set_hostname(int argc, char **argv, void *userdata) {
return ret;
}
static int set_icon_name(int argc, char **argv, void *userdata) {
return set_simple_string(userdata, "icon", "SetIconName", argv[1]);
static int get_or_set_hostname(int argc, char **argv, void *userdata) {
return argc == 1 ? get_hostname_based_on_flag(userdata) :
set_hostname(argc, argv, userdata);
}
static int set_chassis(int argc, char **argv, void *userdata) {
return set_simple_string(userdata, "chassis", "SetChassis", argv[1]);
static int get_or_set_icon_name(int argc, char **argv, void *userdata) {
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) {
return set_simple_string(userdata, "deployment", "SetDeployment", argv[1]);
static int get_or_set_chassis(int argc, char **argv, void *userdata) {
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) {
return set_simple_string(userdata, "location", "SetLocation", argv[1]);
static int get_or_set_deployment(int argc, char **argv, void *userdata) {
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) {
@ -509,11 +522,11 @@ static int help(void) {
"%sQuery or change system hostname.%s\n"
"\nCommands:\n"
" status Show current hostname settings\n"
" set-hostname NAME Set system hostname\n"
" set-icon-name NAME Set icon name for host\n"
" set-chassis NAME Set chassis type for host\n"
" set-deployment NAME Set deployment environment for host\n"
" set-location NAME Set location for host\n"
" hostname [NAME] Get/set system hostname\n"
" icon-name [NAME] Get/set icon name for host\n"
" chassis [NAME] Get/set chassis type for host\n"
" deployment [NAME] Get/set deployment environment for host\n"
" location [NAME] Get/set location for host\n"
"\nOptions:\n"
" -h --help Show this help\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 const Verb verbs[] = {
{ "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
{ "set-hostname", 2, 2, 0, set_hostname },
{ "set-icon-name", 2, 2, 0, set_icon_name },
{ "set-chassis", 2, 2, 0, set_chassis },
{ "set-deployment", 2, 2, 0, set_deployment },
{ "set-location", 2, 2, 0, set_location },
{ "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
{ "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
{ "hostname", VERB_ANY, 2, 0, get_or_set_hostname },
{ "set-hostname", 2, 2, 0, get_or_set_hostname }, /* obsolete */
{ "icon-name", VERB_ANY, 2, 0, get_or_set_icon_name },
{ "set-icon-name", 2, 2, 0, get_or_set_icon_name }, /* obsolete */
{ "chassis", VERB_ANY, 2, 0, get_or_set_chassis },
{ "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. */
{}
};