1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

Merge pull request #2472 from 353fc443/hostname-uresult

Added UResult for hostname, hostid and dirname
This commit is contained in:
Sylvestre Ledru 2021-07-06 11:41:56 +02:00 committed by GitHub
commit 54b389fd1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 20 deletions

View file

@ -10,6 +10,7 @@ extern crate uucore;
use clap::{crate_version, App, Arg}; use clap::{crate_version, App, Arg};
use std::path::Path; use std::path::Path;
use uucore::error::{UResult, UUsageError};
use uucore::InvalidEncodingHandling; use uucore::InvalidEncodingHandling;
static ABOUT: &str = "strip last component from file name"; static ABOUT: &str = "strip last component from file name";
@ -30,7 +31,8 @@ fn get_long_usage() -> String {
) )
} }
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args let args = args
.collect_str(InvalidEncodingHandling::ConvertLossy) .collect_str(InvalidEncodingHandling::ConvertLossy)
.accept_any(); .accept_any();
@ -77,11 +79,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
print!("{}", separator); print!("{}", separator);
} }
} else { } else {
show_usage_error!("missing operand"); return Err(UUsageError::new(1, "missing operand".to_string()));
return 1;
} }
0 Ok(())
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {

View file

@ -12,6 +12,7 @@ extern crate uucore;
use clap::{crate_version, App}; use clap::{crate_version, App};
use libc::c_long; use libc::c_long;
use uucore::error::UResult;
static SYNTAX: &str = "[options]"; static SYNTAX: &str = "[options]";
@ -20,10 +21,11 @@ extern "C" {
pub fn gethostid() -> c_long; pub fn gethostid() -> c_long;
} }
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
uu_app().get_matches_from(args); uu_app().get_matches_from(args);
hostid(); hostid();
0 Ok(())
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {

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(())
} }
} }