X Tutup
Skip to content

Commit 572c55e

Browse files
programmer04poettering
authored andcommitted
hostnamectl: deprecate set-* methods and expose getters by only using nouns in commands
1 parent c2503e3 commit 572c55e

File tree

4 files changed

+96
-89
lines changed

4 files changed

+96
-89
lines changed

man/hostnamectl.xml

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,16 @@
6363
<varlistentry>
6464
<term><command>status</command></term>
6565

66-
<listitem><para>Show current system hostname and related information. If no command is specified,
66+
<listitem><para>Show system hostname and related information. If no command is specified,
6767
this is the implied default.</para></listitem>
6868
</varlistentry>
6969

7070
<varlistentry>
71-
<term><command>set-hostname <replaceable>NAME</replaceable></command></term>
71+
<term><command>hostname</command> [<replaceable>NAME</replaceable>]</term>
7272

73-
<listitem><para>Set the system hostname to <replaceable>NAME</replaceable>. By default, this will alter the
73+
<listitem><para>If no argument is given, print the system hostname. If an
74+
optional argument <replaceable>NAME</replaceable> is provided then the command changes the
75+
system hostname to <replaceable>NAME</replaceable>. By default, this will alter the
7476
pretty, the static, and the transient hostname alike; however, if one or more of <option>--static</option>,
7577
<option>--transient</option>, <option>--pretty</option> are used, only the selected hostnames are changed. If
7678
the pretty hostname is being set, and static or transient are being set as well, the specified hostname will be
@@ -82,35 +84,29 @@
8284
<para>The static and transient hostnames must each be either a single DNS label (a string composed of
8385
7-bit ASCII lower-case characters and no spaces or dots, limited to the format allowed for DNS domain
8486
name labels), or a sequence of such labels separated by single dots that forms a valid DNS FQDN. The
85-
hostname must be at most 64 characters, which is a Linux limitation (DNS allows longer names).</para>
86-
87-
<para>Pass the empty string <literal></literal> as the hostname to reset the selected hostnames to
88-
their default (usually <literal>&FALLBACK_HOSTNAME;</literal>).</para></listitem>
87+
hostname must be at most 64 characters, which is a Linux limitation (DNS allows longer names).</para></listitem>
8988
</varlistentry>
9089

9190
<varlistentry>
92-
<term><command>set-icon-name <replaceable>NAME</replaceable></command></term>
91+
<term><command>icon-name</command> [<replaceable>NAME</replaceable>]</term>
9392

94-
<listitem><para>Set the system icon name to
95-
<replaceable>NAME</replaceable>. The icon name is used by some
93+
<listitem><para>If no argument is given, print the icon name of the system. If an
94+
optional argument <replaceable>NAME</replaceable> is provided then the command changes the
95+
icon name to <replaceable>NAME</replaceable>. The icon name is used by some
9696
graphical applications to visualize this host. The icon name
9797
should follow the <ulink
9898
url="http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html">Icon
99-
Naming Specification</ulink>.</para>
100-
101-
<para>Pass an empty string to reset the icon name to the
102-
default value, which is determined from chassis type (see
103-
below) and possibly other parameters.</para></listitem>
99+
Naming Specification</ulink>.</para></listitem>
104100
</varlistentry>
105101

106102
<varlistentry>
107-
<term><command>set-chassis <replaceable>TYPE</replaceable></command></term>
103+
<term><command>chassis</command> [<replaceable>TYPE</replaceable>]</term>
108104

109-
<listitem><para>Set the chassis type to
110-
<replaceable>TYPE</replaceable>. The chassis type is used by
111-
some graphical applications to visualize the host or alter
112-
user interaction. Currently, the following chassis types are
113-
defined:
105+
<listitem><para>If no argument is given, print the chassis type. If an
106+
optional argument <replaceable>TYPE</replaceable> is provided then the command changes the
107+
chassis type to <replaceable>TYPE</replaceable>. The chassis type is used by
108+
some graphical applications to visualize the host or alter user interaction.
109+
Currently, the following chassis types are defined:
114110
<literal>desktop</literal>,
115111
<literal>laptop</literal>,
116112
<literal>convertible</literal>,
@@ -123,43 +119,36 @@
123119
<literal>vm</literal> and
124120
<literal>container</literal> for virtualized systems that lack
125121
an immediate physical chassis.</para>
126-
127-
<para>Pass an empty string to reset the chassis type to the
128-
default value which is determined from the firmware and
129-
possibly other parameters.</para>
130122
</listitem>
131123
</varlistentry>
132124

133125
<varlistentry>
134-
<term><command>set-deployment <replaceable>ENVIRONMENT</replaceable></command></term>
126+
<term><command>deployment</command> [<replaceable>ENVIRONMENT</replaceable>]</term>
135127

136-
<listitem><para>Set the deployment environment description.
137-
<replaceable>ENVIRONMENT</replaceable> must be a single word
138-
without any control characters. One of the following is
139-
suggested:
128+
<listitem><para>If no argument is given, print the deployment environment. If an
129+
optional argument <replaceable>ENVIRONMENT</replaceable> is provided then the command changes the
130+
deployment environment to <replaceable>ENVIRONMENT</replaceable>.
131+
Argument <replaceable>ENVIRONMENT</replaceable>
132+
must be a single word without any control characters. One of the following is suggested:
140133
<literal>development</literal>,
141134
<literal>integration</literal>,
142135
<literal>staging</literal>,
143136
<literal>production</literal>.
144137
</para>
145-
146-
<para>Pass an empty string to reset to the default empty
147-
value.</para>
148138
</listitem>
149139
</varlistentry>
150140

151141
<varlistentry>
152-
<term><command>set-location <replaceable>LOCATION</replaceable></command></term>
142+
<term><command>location</command> [<replaceable>LOCATION</replaceable>]</term>
153143

154-
<listitem><para>Set the location string for the system, if it
155-
is known. <replaceable>LOCATION</replaceable> should be a
144+
<listitem><para>If no argument is given, print the location string for the system. If an
145+
optional argument <replaceable>LOCATION</replaceable> is provided then the command changes the
146+
location string for the system to <replaceable>LOCATION</replaceable>.
147+
Argument <replaceable>LOCATION</replaceable> should be a
156148
human-friendly, free-form string describing the physical
157149
location of the system, if it is known and applicable. This
158150
may be as generic as <literal>Berlin, Germany</literal> or as
159151
specific as <literal>Left Rack, 2nd Shelf</literal>.</para>
160-
161-
<para>Pass an empty string to reset to the default empty
162-
value.</para>
163152
</listitem>
164153
</varlistentry>
165154
</variablelist>

shell-completion/bash/hostnamectl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ _hostnamectl() {
3838

3939
local -A VERBS=(
4040
[STANDALONE]='status'
41-
[ICONS]='set-icon-name'
42-
[NAME]='set-hostname set-deployment set-location'
43-
[CHASSIS]='set-chassis'
41+
[ICONS]='icon-name'
42+
[NAME]='hostname deployment location'
43+
[CHASSIS]='chassis'
4444
)
4545

4646
for ((i=0; i < COMP_CWORD; i++)); do

shell-completion/zsh/_hostnamectl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
#compdef hostnamectl -*- shell-script -*-
22
# SPDX-License-Identifier: LGPL-2.1-or-later
33

4-
(( $+functions[_hostnamectl_set-hostname] )) ||
5-
_hostnamectl_set-hostname() {
4+
(( $+functions[_hostnamectl_hostname] )) ||
5+
_hostnamectl_hostname() {
66
if (( CURRENT <= 3 )); then
77
_message "new hostname"
88
else
99
_message "no more options"
1010
fi
1111
}
1212

13-
(( $+functions[_hostnamectl_set-icon-name] )) ||
14-
_hostnamectl_set-icon-name() {
13+
(( $+functions[_hostnamectl_icon-name] )) ||
14+
_hostnamectl_icon-name() {
1515
if (( CURRENT <= 3 )); then
1616
_message "new icon name"
1717
else
1818
_message "no more options"
1919
fi
2020
}
2121

22-
(( $+functions[_hostnamectl_set-chassis] )) ||
23-
_hostnamectl_set-chassis() {
22+
(( $+functions[_hostnamectl_chassis] )) ||
23+
_hostnamectl_chassis() {
2424
if (( CURRENT <= 3 )); then
2525
_chassis=( desktop laptop convertible server tablet handset watch embedded vm container )
2626
_describe chassis _chassis
@@ -29,17 +29,17 @@ _hostnamectl_set-chassis() {
2929
fi
3030
}
3131

32-
(( $+functions[_hostnamectl_set-deployment] )) ||
33-
_hostnamectl_set-deployment() {
32+
(( $+functions[_hostnamectl_deployment] )) ||
33+
_hostnamectl_deployment() {
3434
if (( CURRENT <= 3 )); then
3535
_message "new environment"
3636
else
3737
_message "no more options"
3838
fi
3939
}
4040

41-
(( $+functions[_hostnamectl_set-location] )) ||
42-
_hostnamectl_set-location() {
41+
(( $+functions[_hostnamectl_location] )) ||
42+
_hostnamectl_location() {
4343
if (( CURRENT <= 3 )); then
4444
_message "new location"
4545
else
@@ -52,11 +52,11 @@ _hostnamectl_commands() {
5252
local -a _hostnamectl_cmds
5353
_hostnamectl_cmds=(
5454
"status:Show current hostname settings"
55-
"set-hostname:Set system hostname"
56-
"set-icon-name:Set icon name for host"
57-
"set-chassis:Set chassis type for host"
58-
"set-deployment:Set deployment environment for host"
59-
"set-location:Set location for host"
55+
"hostname:Get/set system hostname"
56+
"icon-name:Get/set icon name for host"
57+
"chassis:Get/set chassis type for host"
58+
"deployment:Get/set deployment environment for host"
59+
"location:Get/set location for host"
6060
)
6161
if (( CURRENT == 1 )); then
6262
_describe -t commands 'hostnamectl commands' _hostnamectl_cmds || compadd "$@"

src/hostname/hostnamectl.c

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,19 @@ static int show_all_names(sd_bus *bus) {
318318
return print_status_info(&info);
319319
}
320320

321+
static int get_hostname_based_on_flag(sd_bus *bus) {
322+
const char *attr;
323+
324+
if (!!arg_static + !!arg_pretty + !!arg_transient > 1)
325+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
326+
"Cannot query more than one name type at a time");
327+
328+
attr = arg_pretty ? "PrettyHostname" :
329+
arg_static ? "StaticHostname" : "Hostname";
330+
331+
return get_one_name(bus, attr, NULL);
332+
}
333+
321334
static int show_status(int argc, char **argv, void *userdata) {
322335
sd_bus *bus = userdata;
323336
int r;
@@ -352,22 +365,13 @@ static int show_status(int argc, char **argv, void *userdata) {
352365
return 0;
353366
}
354367

355-
if (arg_pretty || arg_static || arg_transient) {
356-
const char *attr;
357-
358-
if (!!arg_static + !!arg_pretty + !!arg_transient > 1)
359-
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
360-
"Cannot query more than one name type at a time");
361-
362-
attr = arg_pretty ? "PrettyHostname" :
363-
arg_static ? "StaticHostname" : "Hostname";
364-
365-
return get_one_name(bus, attr, NULL);
366-
}
368+
if (arg_pretty || arg_static || arg_transient)
369+
return get_hostname_based_on_flag(bus);
367370

368371
return show_all_names(bus);
369372
}
370373

374+
371375
static int set_simple_string_internal(sd_bus *bus, sd_bus_error *error, const char *target, const char *method, const char *value) {
372376
_cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
373377
int r;
@@ -481,20 +485,29 @@ static int set_hostname(int argc, char **argv, void *userdata) {
481485
return ret;
482486
}
483487

484-
static int set_icon_name(int argc, char **argv, void *userdata) {
485-
return set_simple_string(userdata, "icon", "SetIconName", argv[1]);
488+
static int get_or_set_hostname(int argc, char **argv, void *userdata) {
489+
return argc == 1 ? get_hostname_based_on_flag(userdata) :
490+
set_hostname(argc, argv, userdata);
491+
}
492+
493+
static int get_or_set_icon_name(int argc, char **argv, void *userdata) {
494+
return argc == 1 ? get_one_name(userdata, "IconName", NULL) :
495+
set_simple_string(userdata, "icon", "SetIconName", argv[1]);
486496
}
487497

488-
static int set_chassis(int argc, char **argv, void *userdata) {
489-
return set_simple_string(userdata, "chassis", "SetChassis", argv[1]);
498+
static int get_or_set_chassis(int argc, char **argv, void *userdata) {
499+
return argc == 1 ? get_one_name(userdata, "Chassis", NULL) :
500+
set_simple_string(userdata, "chassis", "SetChassis", argv[1]);
490501
}
491502

492-
static int set_deployment(int argc, char **argv, void *userdata) {
493-
return set_simple_string(userdata, "deployment", "SetDeployment", argv[1]);
503+
static int get_or_set_deployment(int argc, char **argv, void *userdata) {
504+
return argc == 1 ? get_one_name(userdata, "Deployment", NULL) :
505+
set_simple_string(userdata, "deployment", "SetDeployment", argv[1]);
494506
}
495507

496-
static int set_location(int argc, char **argv, void *userdata) {
497-
return set_simple_string(userdata, "location", "SetLocation", argv[1]);
508+
static int get_or_set_location(int argc, char **argv, void *userdata) {
509+
return argc == 1 ? get_one_name(userdata, "Location", NULL) :
510+
set_simple_string(userdata, "location", "SetLocation", argv[1]);
498511
}
499512

500513
static int help(void) {
@@ -509,11 +522,11 @@ static int help(void) {
509522
"%sQuery or change system hostname.%s\n"
510523
"\nCommands:\n"
511524
" status Show current hostname settings\n"
512-
" set-hostname NAME Set system hostname\n"
513-
" set-icon-name NAME Set icon name for host\n"
514-
" set-chassis NAME Set chassis type for host\n"
515-
" set-deployment NAME Set deployment environment for host\n"
516-
" set-location NAME Set location for host\n"
525+
" hostname [NAME] Get/set system hostname\n"
526+
" icon-name [NAME] Get/set icon name for host\n"
527+
" chassis [NAME] Get/set chassis type for host\n"
528+
" deployment [NAME] Get/set deployment environment for host\n"
529+
" location [NAME] Get/set location for host\n"
517530
"\nOptions:\n"
518531
" -h --help Show this help\n"
519532
" --version Show package version\n"
@@ -623,13 +636,18 @@ static int parse_argv(int argc, char *argv[]) {
623636
static int hostnamectl_main(sd_bus *bus, int argc, char *argv[]) {
624637

625638
static const Verb verbs[] = {
626-
{ "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
627-
{ "set-hostname", 2, 2, 0, set_hostname },
628-
{ "set-icon-name", 2, 2, 0, set_icon_name },
629-
{ "set-chassis", 2, 2, 0, set_chassis },
630-
{ "set-deployment", 2, 2, 0, set_deployment },
631-
{ "set-location", 2, 2, 0, set_location },
632-
{ "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
639+
{ "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
640+
{ "hostname", VERB_ANY, 2, 0, get_or_set_hostname },
641+
{ "set-hostname", 2, 2, 0, get_or_set_hostname }, /* obsolete */
642+
{ "icon-name", VERB_ANY, 2, 0, get_or_set_icon_name },
643+
{ "set-icon-name", 2, 2, 0, get_or_set_icon_name }, /* obsolete */
644+
{ "chassis", VERB_ANY, 2, 0, get_or_set_chassis },
645+
{ "set-chassis", 2, 2, 0, get_or_set_chassis }, /* obsolete */
646+
{ "deployment", VERB_ANY, 2, 0, get_or_set_deployment },
647+
{ "set-deployment", 2, 2, 0, get_or_set_deployment }, /* obsolete */
648+
{ "location", VERB_ANY, 2, 0, get_or_set_location },
649+
{ "set-location", 2, 2, 0, get_or_set_location }, /* obsolete */
650+
{ "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
633651
{}
634652
};
635653

0 commit comments

Comments
 (0)
X Tutup