Skip to content

Commit

Permalink
Add support for using a hostname identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Edu4rdSHL committed Feb 7, 2024
1 parent 14c0f37 commit 230b2ba
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 11 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 9 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(())
}
Expand Down
2 changes: 2 additions & 0 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub fn manage_listing(database_connection: &Connection) -> Result<()> {
"KIND",
"SOURCE DIR",
"DESTINATION DIR",
"MACHINE",
"RO/RW",
"DATE"
]);
Expand All @@ -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,
]);
Expand Down
15 changes: 8 additions & 7 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ 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(())
}

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)?;

Expand Down Expand Up @@ -52,7 +52,7 @@ pub fn return_all_data(connection: &Connection) -> Result<Vec<Database>> {
let mut snapshots_data: Vec<Database> = 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();
Expand All @@ -65,7 +65,7 @@ pub fn return_all_data(connection: &Connection) -> Result<Vec<Database>> {
pub fn return_only_x_items(connection: &Connection, args: &Args) -> Result<Vec<Database>> {
let mut snapshots_data: Vec<Database> = 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();
Expand All @@ -83,8 +83,9 @@ fn populate_db_struct(row: &Statement, mut db_struct: Database) -> Result<Databa
db_struct.kind = row.read(2)?;
db_struct.source = row.read(3)?;
db_struct.destination = row.read(4)?;
db_struct.ro_rw = row.read(5)?;
db_struct.date = row.read(6)?;
db_struct.machine = row.read(5)?;
db_struct.ro_rw = row.read(6)?;
db_struct.date = row.read(7)?;

Ok(db_struct)
}
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::Result;
use chrono::Utc;
use clap::Parser;
use rusnapshot::{args, controller, structs::ExtraArgs, utils};
use {
anyhow::Result,
chrono::Utc,
clap::Parser,
rusnapshot::{args, controller, structs::ExtraArgs, utils},
};

fn try_run() -> Result<()> {
let mut arguments = args::Args::parse();
Expand Down
1 change: 1 addition & 0 deletions src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct Database {
pub source: String,
pub destination: String,
pub ro_rw: String,
pub machine: String,
pub date: String,
}

Expand Down
4 changes: 4 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit 230b2ba

Please sign in to comment.