From 572c55ee3cbeb2a7ed4600aec2530ffa7e7a3a87 Mon Sep 17 00:00:00 2001 From: Jakub Warczarek Date: Fri, 23 Apr 2021 18:41:45 +0000 Subject: hostnamectl: deprecate set-* methods and expose getters by only using nouns in commands --- man/hostnamectl.xml | 67 +++++++++++++------------------- shell-completion/bash/hostnamectl | 6 +-- shell-completion/zsh/_hostnamectl | 30 +++++++------- src/hostname/hostnamectl.c | 82 ++++++++++++++++++++++++--------------- 4 files changed, 96 insertions(+), 89 deletions(-) diff --git a/man/hostnamectl.xml b/man/hostnamectl.xml index ed2dabef3b..51f83d1cd6 100644 --- a/man/hostnamectl.xml +++ b/man/hostnamectl.xml @@ -63,14 +63,16 @@ status - Show current system hostname and related information. If no command is specified, + Show system hostname and related information. If no command is specified, this is the implied default. - set-hostname NAME + hostname [NAME] - Set the system hostname to NAME. By default, this will alter the + If no argument is given, print the system hostname. If an + optional argument NAME is provided then the command changes the + system hostname to NAME. By default, this will alter the pretty, the static, and the transient hostname alike; however, if one or more of , , 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 @@ 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). - - Pass the empty string as the hostname to reset the selected hostnames to - their default (usually &FALLBACK_HOSTNAME;). + hostname must be at most 64 characters, which is a Linux limitation (DNS allows longer names). - set-icon-name NAME + icon-name [NAME] - Set the system icon name to - NAME. The icon name is used by some + If no argument is given, print the icon name of the system. If an + optional argument NAME is provided then the command changes the + icon name to NAME. The icon name is used by some graphical applications to visualize this host. The icon name should follow the Icon - Naming Specification. - - 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. + Naming Specification. - set-chassis TYPE + chassis [TYPE] - Set the chassis type to - TYPE. The chassis type is used by - some graphical applications to visualize the host or alter - user interaction. Currently, the following chassis types are - defined: + If no argument is given, print the chassis type. If an + optional argument TYPE is provided then the command changes the + chassis type to TYPE. The chassis type is used by + some graphical applications to visualize the host or alter user interaction. + Currently, the following chassis types are defined: desktop, laptop, convertible, @@ -123,43 +119,36 @@ vm and container for virtualized systems that lack an immediate physical chassis. - - Pass an empty string to reset the chassis type to the - default value which is determined from the firmware and - possibly other parameters. - set-deployment ENVIRONMENT + deployment [ENVIRONMENT] - Set the deployment environment description. - ENVIRONMENT must be a single word - without any control characters. One of the following is - suggested: + If no argument is given, print the deployment environment. If an + optional argument ENVIRONMENT is provided then the command changes the + deployment environment to ENVIRONMENT. + Argument ENVIRONMENT + must be a single word without any control characters. One of the following is suggested: development, integration, staging, production. - - Pass an empty string to reset to the default empty - value. - set-location LOCATION + location [LOCATION] - Set the location string for the system, if it - is known. LOCATION should be a + If no argument is given, print the location string for the system. If an + optional argument LOCATION is provided then the command changes the + location string for the system to LOCATION. + Argument LOCATION 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 Berlin, Germany or as specific as Left Rack, 2nd Shelf. - - Pass an empty string to reset to the default empty - value. diff --git a/shell-completion/bash/hostnamectl b/shell-completion/bash/hostnamectl index e3765c315c..5c8d3975d7 100644 --- a/shell-completion/bash/hostnamectl +++ b/shell-completion/bash/hostnamectl @@ -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 diff --git a/shell-completion/zsh/_hostnamectl b/shell-completion/zsh/_hostnamectl index d05c46bd43..2a6bf4d125 100644 --- a/shell-completion/zsh/_hostnamectl +++ b/shell-completion/zsh/_hostnamectl @@ -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 "$@" diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index 19c04869dd..852da6bb8e 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -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 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_chassis(int argc, char **argv, void *userdata) { - return set_simple_string(userdata, "chassis", "SetChassis", 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_deployment(int argc, char **argv, void *userdata) { - return set_simple_string(userdata, "deployment", "SetDeployment", 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 set_location(int argc, char **argv, void *userdata) { - return set_simple_string(userdata, "location", "SetLocation", 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. */ {} }; -- cgit v1.2.3