~cyborg/crypt

7ee6f0ea29b14f9fee93edbc1c78222c802bbaf8 — christian-cleberg 2 months ago 11b2dec
implement half-working delete & edit functions
2 files changed, 60 insertions(+), 11 deletions(-)

M README.md
M src/main.rs
M README.md => README.md +1 -1
@@ 76,7 76,7 @@ crypt ARGUMENT [VALUES]
    </tr>
    <tr>
      <td>-e</td>
      <td>--edit [UUID] [FIELD]</td>
      <td>--edit [UUID] [FIELD_NAME]</td>
      <td>Edit a saved account</td>
    </tr>
    <tr>

M src/main.rs => src/main.rs +59 -10
@@ 97,13 97,53 @@ fn create_db() -> Result<()> {
}

// Insert data into the database
fn insert_db(uuid: String, application: String, username: String, password: String, url: String) -> Result<()> {
fn insert_account(uuid: String, application: String, username: String, password: String, url: String) -> Result<()> {
    let conn = Connection::open(SQLITE_DB)?;
    conn.execute(
        "INSERT INTO accounts (uuid, application, username, password, url) values (?1, ?2, ?3, ?4, ?5)",
        [uuid, application, username, password, url],
    )?;
    Ok(())
}

// Delete data from the database
fn update_account(uuid: String, field_string: String, new_value: String) -> Result<()> {
    let mut field: usize = usize::MAX;
    if (field_string == "title") | (field_string == "Title") {
        field = 0;
    } else if (field_string == "username") | (field_string == "Username") {
        field = 1;
    } else if (field_string == "password") | (field_string == "Password") {
        field = 2;
    } else if (field_string == "url") | (field_string == "URL") {
        field = 3;
    } else {
        eprintln!("Error: Provided field to edit does not match a field in the database.");
    }
    println!("Field: {}: {}", field_string, field);
    println!("New Value/UUID: {}: {}", new_value, uuid);
    let queries = vec![
        "UPDATE accounts SET application = ?1 WHERE uuid = ?2",
        "UPDATE accounts SET username = ?1 WHERE uuid = ?2",
        "UPDATE accounts SET password = ?1 WHERE uuid = ?2",
        "UPDATE accounts SET url = ?1 WHERE uuid = ?2",
    ];
    println!("Query: {}", queries[field]);
    let conn = Connection::open(SQLITE_DB)?;
    conn.execute(
        queries[field],
        [new_value, uuid],
    )?;
    Ok(())
}

// Delete data from the database
fn delete_account(uuid: String) -> Result<()> {
    let conn = Connection::open(SQLITE_DB)?;
    conn.execute(
        "DELETE FROM accounts WHERE uuid = ?1",
        [uuid],
    )?;
    Ok(())
}



@@ 215,7 255,7 @@ fn new() {

    // Create the database, if necessary, and insert data
    create_db();
    insert_db(account.uuid, account.title, account.username, account.password, account.url);
    insert_account(account.uuid, account.title, account.username, account.password, account.url);
    println!("Account saved to the vault. Use `crypt --list` to see all saved accounts.");
}



@@ 226,13 266,17 @@ fn list() -> Result<()> {
}

// TODO: Edit a saved account
fn edit() {
    println!();
// WARNING: This process does not currently work as expected; /
// I think the encrypted UUID differs from the encrypted UUID in the database
fn edit(uuid: String, field_name: String, new_value: String) {
    update_account(encrypt(uuid), field_name, encrypt(new_value));
}

// TODO: Delete a saved account
fn delete() {
    println!();
// WARNING: This process does not currently work as expected; /
// I think the encrypted UUID differs from the encrypted UUID in the database
fn delete(uuid: String) {
    delete_account(uuid);
}

// TODO: Delete all saved accounts and delete the vault file


@@ 285,8 329,7 @@ fn main() {
            .short("e")
            .long("edit")
            .help("Edit a saved account")
            .value_name("uuid")
            .value_name("field")
            .value_names(&["uuid", "field_name", "new_value"])
            .takes_value(true))
        .arg(Arg::with_name("delete")
            .short("d")


@@ 306,9 349,15 @@ fn main() {
    } else if matches.is_present("list") {
        list();
    } else if matches.is_present("edit") {
        edit();
        let values: Vec<_> = matches.values_of("edit").unwrap().collect();
        edit(
            String::from(values[0]),
            String::from(values[1]),
            String::from(values[2]),
        );
    } else if matches.is_present("delete") {
        delete();
        let values: Vec<_> = matches.values_of("delete").unwrap().collect();
        delete(String::from(values[0]));
    } else if matches.is_present("purge") {
        purge();
    }