rucksack/command/
set.rs

1//! # Updating Records
2//!
3//! Changing a password:
4//!
5//! ```shell
6//! rucksack set password \
7//!   --url http://example.com \
8//!   --user shelly
9//!   --password whyyyyyyyyyyyyyyyyyyy
10//! ```
11//!
12//! If the password isn't provided, you will be prompted at the terminal:
13//!
14//! ```shell
15//! rucksack set password \
16//!   --url http://example.com \
17//!   --user shelly
18//! ```
19//!
20//! ```shell
21//! Enter record password:
22//! ```
23//!
24//! Changing a user:
25//!
26//! ```shell
27//! rucksack set user \
28//!   --url http://example.com \
29//!   --old-user shelly
30//!   --new-user clammy
31//! ```
32//!
33//! Changing a URL:
34//!
35//! ```shell
36//! rucksack set url \
37//!   --old-url http://example.com \
38//!   --new-url http://shelly.com \
39//!   --user clammy
40//! ```
41//!
42//! Changing the record type:
43//!
44//! ```shell
45//! rucksack set type \
46//!   --url http://example.com \
47//!   --user clammy
48//!   --type password
49//! ```
50//!
51//! //! # All Subcommands
52//!
53//! See the full list of supported subcommands with:
54//! ```shell
55//! rucksack set -h
56//! ```
57//!
58use anyhow::Result;
59use clap::ArgMatches;
60
61use rucksack_db as store;
62
63use crate::app::App;
64use crate::option;
65use crate::query;
66
67pub fn record_type(matches: &ArgMatches, app: &App) -> Result<()> {
68    log::debug!("Setting record type ...");
69    let mut record = query::record(&app.db, matches)?;
70    record.set_kind(option::record_kind(matches));
71    app.db.insert(record);
72    app.db.close()?;
73    Ok(())
74}
75
76pub fn password(matches: &ArgMatches, app: &App) -> Result<()> {
77    log::debug!("Setting record password ...");
78    let mut record = query::record(&app.db, matches)?;
79    let key = record.key();
80    record.set_password(option::record_pwd_revealed(matches));
81    app.db.update(key, record);
82    app.db.close()?;
83    Ok(())
84}
85
86pub fn status(matches: &ArgMatches, app: &App) -> Result<()> {
87    log::debug!("Setting record status ...");
88    let mut record = query::record(&app.db, matches)?;
89    record.set_status(option::record_state(matches));
90    app.db.insert(record);
91    app.db.close()?;
92    Ok(())
93}
94
95pub fn url(matches: &ArgMatches, app: &App) -> Result<()> {
96    log::debug!("Setting record URL ...");
97    let category = option::category(matches);
98    let kind = option::record_kind(matches);
99    let user = option::user(matches);
100    let old_url = option::url_old(matches);
101    let new_url = option::url_new(matches);
102    let key = store::key(&category, kind, &user, &old_url);
103    let mut record = query::record_by_key(&app.db, key.clone())?;
104    log::debug!("Got record: {record:?}");
105    record.set_url(new_url);
106    app.db.update(key, record);
107    app.db.close()?;
108    Ok(())
109}
110
111pub fn user(matches: &ArgMatches, app: &App) -> Result<()> {
112    log::debug!("Setting record user ...");
113    let category = option::category(matches);
114    let kind = option::record_kind(matches);
115    let old_user = option::user_old(matches);
116    let new_user = option::user_new(matches);
117    let url = option::url(matches);
118    let key = store::key(&category, kind, &old_user, &url);
119    let mut record = query::record_by_key(&app.db, key.clone())?;
120    record.set_user(new_user);
121    let msg = "there was a problem deleting the old record";
122    match app.db.delete(key) {
123        Some(false) => log::error!("{msg}"),
124        Some(_) => (),
125        None => log::error!("{msg}"),
126    }
127    app.db.insert(record);
128    app.db.close()?;
129    Ok(())
130}