1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-02 05:57:46 +00:00

Added UResult for hostname

This commit is contained in:
353fc443 2021-07-02 08:19:33 +00:00
parent 5a40148602
commit 330db2eb3e
No known key found for this signature in database
GPG key ID: D58B14ED3D42A937

View file

@ -14,6 +14,7 @@ use clap::{crate_version, App, Arg, ArgMatches};
use std::collections::hash_set::HashSet; use std::collections::hash_set::HashSet;
use std::net::ToSocketAddrs; use std::net::ToSocketAddrs;
use std::str; use std::str;
use uucore::error::{UResult, USimpleError};
#[cfg(windows)] #[cfg(windows)]
use winapi::shared::minwindef::MAKEWORD; use winapi::shared::minwindef::MAKEWORD;
@ -28,15 +29,15 @@ static OPT_FQDN: &str = "fqdn";
static OPT_SHORT: &str = "short"; static OPT_SHORT: &str = "short";
static OPT_HOST: &str = "host"; static OPT_HOST: &str = "host";
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
#![allow(clippy::let_and_return)] #![allow(clippy::let_and_return)]
#[cfg(windows)] #[cfg(windows)]
unsafe { unsafe {
#[allow(deprecated)] #[allow(deprecated)]
let mut data = std::mem::uninitialized(); let mut data = std::mem::uninitialized();
if WSAStartup(MAKEWORD(2, 2), &mut data as *mut _) != 0 { if WSAStartup(MAKEWORD(2, 2), &mut data as *mut _) != 0 {
eprintln!("Failed to start Winsock 2.2"); return Err(USimpleError::new(1, format!("Failed to start Winsock 2.2")));
return 1;
} }
} }
let result = execute(args); let result = execute(args);
@ -50,7 +51,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
fn get_usage() -> String { fn get_usage() -> String {
format!("{0} [OPTION]... [HOSTNAME]", executable!()) format!("{0} [OPTION]... [HOSTNAME]", executable!())
} }
fn execute(args: impl uucore::Args) -> i32 { fn execute(args: impl uucore::Args) -> UResult<()> {
let usage = get_usage(); let usage = get_usage();
let matches = uu_app().usage(&usage[..]).get_matches_from(args); let matches = uu_app().usage(&usage[..]).get_matches_from(args);
@ -58,10 +59,9 @@ fn execute(args: impl uucore::Args) -> i32 {
None => display_hostname(&matches), None => display_hostname(&matches),
Some(host) => { Some(host) => {
if let Err(err) = hostname::set(host) { if let Err(err) = hostname::set(host) {
show_error!("{}", err); return Err(USimpleError::new(1, format!("{}", err)));
1
} else { } else {
0 Ok(())
} }
} }
} }
@ -97,7 +97,7 @@ pub fn uu_app() -> App<'static, 'static> {
.arg(Arg::with_name(OPT_HOST)) .arg(Arg::with_name(OPT_HOST))
} }
fn display_hostname(matches: &ArgMatches) -> i32 { fn display_hostname(matches: &ArgMatches) -> UResult<()> {
let hostname = hostname::get().unwrap().into_string().unwrap(); let hostname = hostname::get().unwrap().into_string().unwrap();
if matches.is_present(OPT_IP_ADDRESS) { if matches.is_present(OPT_IP_ADDRESS) {
@ -127,12 +127,10 @@ fn display_hostname(matches: &ArgMatches) -> i32 {
println!("{}", &output[0..len - 1]); println!("{}", &output[0..len - 1]);
} }
0 Ok(())
} }
Err(f) => { Err(f) => {
show_error!("{}", f); return Err(USimpleError::new(1, format!("{}", f)));
1
} }
} }
} else { } else {
@ -144,12 +142,12 @@ fn display_hostname(matches: &ArgMatches) -> i32 {
} else { } else {
println!("{}", &hostname[ci.0 + 1..]); println!("{}", &hostname[ci.0 + 1..]);
} }
return 0; return Ok(());
} }
} }
println!("{}", hostname); println!("{}", hostname);
0 Ok(())
} }
} }