feat: add hostname1-zbus

This commit is contained in:
Michael Aaron Murphy 2024-04-10 14:31:34 +02:00
parent c81f428ace
commit a5024e0a3d
No known key found for this signature in database
GPG key ID: B2732D4240C9212C
4 changed files with 209 additions and 0 deletions

View file

@ -0,0 +1,67 @@
use std::process::ExitCode;
#[tokio::main]
async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
let connection = zbus::Connection::system().await?;
let proxy = hostname1_zbus::Hostname1Proxy::new(&connection).await?;
let mut parser = pico_args::Arguments::from_env();
match parser.subcommand()?.as_deref() {
Some("hostname") => match parser.subcommand()?.as_deref() {
Some("set") => match parser.free_from_str::<String>().ok().as_deref() {
Some(new_hostname) => {
if let Err(why) = proxy.set_hostname(&new_hostname, false).await {
eprintln!("error: could not set hostname: {why}");
return Ok(ExitCode::FAILURE);
}
}
None => {
eprintln!("error: hostname argument not set");
return Ok(ExitCode::FAILURE);
}
},
_ => {
println!("{}", proxy.hostname().await?);
}
},
Some("static-hostname") => match parser.subcommand()?.as_deref() {
Some("set") => match parser.free_from_str::<String>().ok().as_deref() {
Some(new_hostname) => {
if let Err(why) = proxy.set_static_hostname(&new_hostname, false).await {
eprintln!("error: could not set hostname: {why}");
return Ok(ExitCode::FAILURE);
}
}
None => {
eprintln!("error: hostname argument not set");
return Ok(ExitCode::FAILURE);
}
},
_ => {
println!("{}", proxy.static_hostname().await?);
}
},
_ => print_help(),
}
Ok(ExitCode::SUCCESS)
}
fn print_help() {
println!(
"\
hostnamectl
USAGE:
hostnamectl hostname
hostnamectl hostname set NAME
"
);
}