diff --git a/Cargo.lock b/Cargo.lock index 0f08a10..829fa69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -269,6 +269,17 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d3d0e0f38255e7fa3cf31335b3a56f05febd18025f4db5ef7a0cfb4f8da651f" +[[package]] +name = "hostname" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867" +dependencies = [ + "libc", + "match_cfg", + "winapi", +] + [[package]] name = "iana-time-zone" version = "0.1.60" @@ -363,6 +374,12 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +[[package]] +name = "match_cfg" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" + [[package]] name = "md5" version = "0.7.0" @@ -455,6 +472,7 @@ dependencies = [ "anyhow", "chrono", "clap", + "hostname", "md5", "prettytable-rs", "serde", diff --git a/Cargo.toml b/Cargo.toml index 5bb9a86..86acef3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ chrono = "0.4.33" prettytable-rs = "0.10.0" anyhow = "1.0.79" toml = "0.8.9" +hostname = "0.3.1" [profile.release] diff --git a/src/args.rs b/src/args.rs index 43ed3dd..d15cfba 100644 --- a/src/args.rs +++ b/src/args.rs @@ -62,6 +62,9 @@ pub struct Args { /// Create read-write/rw snapshots. #[clap(short = 'w', long = "rw")] pub read_write: bool, + /// Machine name to be used in the metadata. + #[clap(short, long, default_value = "")] + pub machine: String, } impl Args { @@ -148,6 +151,12 @@ impl Args { .parse() .expect("Failed to parse timeout, make sure it's a number"); } + if let Some(machine) = config.get("machine") { + self.machine = machine + .to_string() + .parse() + .expect("Failed to parse machine"); + } Ok(()) } diff --git a/src/controller.rs b/src/controller.rs index 7a0c5a7..2b3f95c 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -79,6 +79,7 @@ pub fn manage_listing(database_connection: &Connection) -> Result<()> { "KIND", "SOURCE DIR", "DESTINATION DIR", + "MACHINE", "RO/RW", "DATE" ]); @@ -90,6 +91,7 @@ pub fn manage_listing(database_connection: &Connection) -> Result<()> { data.kind, data.source, data.destination, + data.machine, data.ro_rw, data.date, ]); diff --git a/src/database.rs b/src/database.rs index 7eb7f5f..14d8de9 100644 --- a/src/database.rs +++ b/src/database.rs @@ -8,7 +8,7 @@ use { }; pub fn setup_initial_database(connection: &Connection) -> Result<()> { - connection.execute("CREATE TABLE IF NOT EXISTS snapshots (name TEXT NOT NULL, snap_id TEXT NOT NULL, kind TEXT NOT NULL, source TEXT NOT NULL, destination TEXT NOT NULL, ro_rw TEXT NOT NULL, date TEXT DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(name, snap_id))")?; + connection.execute("CREATE TABLE IF NOT EXISTS snapshots (name TEXT NOT NULL, snap_id TEXT NOT NULL, kind TEXT NOT NULL, source TEXT NOT NULL, destination TEXT NOT NULL, machine TEXT NOT NULL, ro_rw TEXT NOT NULL, date TEXT DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(name, snap_id))")?; connection.execute("PRAGMA journal_mode=WAL")?; Ok(()) @@ -16,8 +16,8 @@ pub fn setup_initial_database(connection: &Connection) -> Result<()> { pub fn commit_to_database(args: &Args, extra_args: &ExtraArgs) -> Result<()> { let statement = format!( - "INSERT INTO snapshots (name, snap_id, kind, source, destination, ro_rw) VALUES ('{}', '{}', '{}', '{}', '{}', '{}')", - &extra_args.snapshot_name, args.snapshot_id, args.snapshot_kind, args.source_dir, args.dest_dir, args.read_write + "INSERT INTO snapshots (name, snap_id, kind, source, destination, machine, ro_rw) VALUES ('{}', '{}', '{}', '{}', '{}', '{}', '{}')", + &extra_args.snapshot_name, args.snapshot_id, args.snapshot_kind, args.source_dir, args.dest_dir, args.machine, args.read_write ); extra_args.database_connection.execute(statement)?; @@ -52,7 +52,7 @@ pub fn return_all_data(connection: &Connection) -> Result> { let mut snapshots_data: Vec = Vec::new(); let mut statement = connection - .prepare("SELECT name,snap_id,kind,source,destination,ro_rw,datetime(date, 'localtime') FROM snapshots ORDER BY date DESC")?; + .prepare("SELECT name,snap_id,kind,source,destination,machine,ro_rw,datetime(date, 'localtime') FROM snapshots ORDER BY date DESC")?; while statement.next()? == State::Row { let db_struct = Database::default(); @@ -65,7 +65,7 @@ pub fn return_all_data(connection: &Connection) -> Result> { pub fn return_only_x_items(connection: &Connection, args: &Args) -> Result> { let mut snapshots_data: Vec = Vec::new(); - let mut statement = connection.prepare(&format!("SELECT name,snap_id,kind,source,destination,ro_rw,date FROM (SELECT row_number() over(ORDER BY date DESC) n,* from snapshots WHERE name like '{}%' AND kind = '{}' AND ro_rw = '{}') WHERE n > {}", args.snapshot_prefix, args.snapshot_kind, args.read_write, args.keep_only))?; + let mut statement = connection.prepare(&format!("SELECT name,snap_id,kind,source,destination,machine,ro_rw,date FROM (SELECT row_number() over(ORDER BY date DESC) n,* from snapshots WHERE name like '{}%' AND kind = '{}' AND ro_rw = '{}') WHERE n > {}", args.snapshot_prefix, args.snapshot_kind, args.read_write, args.keep_only))?; while statement.next()? == State::Row { let db_struct = Database::default(); @@ -83,8 +83,9 @@ fn populate_db_struct(row: &Statement, mut db_struct: Database) -> Result Result<()> { let mut arguments = args::Args::parse(); diff --git a/src/structs.rs b/src/structs.rs index 9f3e655..ddad18d 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -8,6 +8,7 @@ pub struct Database { pub source: String, pub destination: String, pub ro_rw: String, + pub machine: String, pub date: String, } diff --git a/src/utils.rs b/src/utils.rs index 51bc0e3..3f63313 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -42,5 +42,9 @@ pub fn check_creation_requirements(arguments: &mut Args, extra_args: &ExtraArgs) .to_string(); } + if arguments.machine.is_empty() { + arguments.machine = hostname::get()?.to_string_lossy().to_string(); + } + Ok(()) }