|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +use adminapi::{new_object::NewObject, query::Query}; |
| 4 | + |
| 5 | +#[tokio::main] |
| 6 | +async fn main() -> anyhow::Result<()> { |
| 7 | + env_logger::init(); |
| 8 | + let args = clap::Command::new("adminclone") |
| 9 | + .arg(clap::arg!(<from> "The origin object identified by it's hostname")) |
| 10 | + .arg( |
| 11 | + clap::arg!(-a <add_attribute> "Adds a value to a multi-attribute field") |
| 12 | + .action(clap::ArgAction::Append), |
| 13 | + ) |
| 14 | + .arg( |
| 15 | + clap::arg!(-d <delete_attribute> "Deletes a value from a multi-attribute field") |
| 16 | + .action(clap::ArgAction::Append), |
| 17 | + ) |
| 18 | + .arg( |
| 19 | + clap::arg!(-s <set_attribute> "Sets the value for an attribute") |
| 20 | + .action(clap::ArgAction::Append), |
| 21 | + ) |
| 22 | + .arg(clap::arg!(-c <clear_attribute> "Clears an attribute").action(clap::ArgAction::Append)) |
| 23 | + .get_matches(); |
| 24 | + |
| 25 | + let hostname = args |
| 26 | + .get_one::<String>("from") |
| 27 | + .ok_or(anyhow::anyhow!("Missing from argument")) |
| 28 | + .cloned()?; |
| 29 | + |
| 30 | + let mut query = Query::builder().filter("hostname", hostname).build(); |
| 31 | + query.restrict = HashSet::new(); // We want to get all attributes here |
| 32 | + let mut server = query.request().await?.one()?; |
| 33 | + server.attributes.clear("hostname"); |
| 34 | + let servertype = server.get("servertype"); |
| 35 | + let mut server = NewObject::from_dataset(server.attributes); |
| 36 | + server.set("servertype", servertype)?; |
| 37 | + |
| 38 | + for pair in args.get_many::<String>("set_attribute").unwrap_or_default() { |
| 39 | + let Some((name, value)) = pair.split_once("=") else { |
| 40 | + return Err(anyhow::anyhow!("Got attribute set without '=': {pair}")); |
| 41 | + }; |
| 42 | + |
| 43 | + server.set(name.to_string(), value.to_string())?; |
| 44 | + } |
| 45 | + |
| 46 | + for pair in args.get_many::<String>("add_attribute").unwrap_or_default() { |
| 47 | + let Some((name, value)) = pair.split_once("=") else { |
| 48 | + return Err(anyhow::anyhow!("Got attribute set without '=': {pair}")); |
| 49 | + }; |
| 50 | + |
| 51 | + server.attributes.add(name.to_string(), value.to_string()); |
| 52 | + } |
| 53 | + |
| 54 | + for pair in args |
| 55 | + .get_many::<String>("delete_attribute") |
| 56 | + .unwrap_or_default() |
| 57 | + { |
| 58 | + let Some((name, value)) = pair.split_once("=") else { |
| 59 | + return Err(anyhow::anyhow!("Got attribute set without '=': {pair}")); |
| 60 | + }; |
| 61 | + |
| 62 | + server |
| 63 | + .attributes |
| 64 | + .remove(name.to_string(), value.to_string()); |
| 65 | + } |
| 66 | + |
| 67 | + for name in args |
| 68 | + .get_many::<String>("clear_attribute") |
| 69 | + .unwrap_or_default() |
| 70 | + { |
| 71 | + server.attributes.clear(name.to_string()); |
| 72 | + } |
| 73 | + |
| 74 | + let server = server.commit().await?; |
| 75 | + |
| 76 | + log::info!("Server cloned"); |
| 77 | + |
| 78 | + Ok(()) |
| 79 | +} |
0 commit comments