From 5e4fb0ea7cafd62fc93d443a659215889e0520d5 Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Sat, 17 Aug 2024 17:23:33 +0200
Subject: [PATCH] feat: Support entering custom keys value via STDIN (#64)
Reviewed-on: https://git.4rs.nl///awiteb/lprs/pulls/64
Co-authored-by: Awiteb
Co-committed-by: Awiteb
---
docs/commands/add.md | 7 +++----
docs/commands/clean.md | 4 ----
docs/commands/edit.md | 19 ++++++++++---------
docs/commands/gen.md | 1 -
docs/commands/get.md | 3 ---
docs/commands/import-export.md | 3 ---
docs/commands/list.md | 2 +-
docs/commands/remove.md | 1 -
src/clap_parsers.rs | 10 ++++++----
src/cli/add_command.rs | 20 ++++++++++++--------
src/cli/edit_command.rs | 24 ++++++++++++++++--------
src/utils.rs | 23 ++++++++++++++++++++++-
12 files changed, 70 insertions(+), 47 deletions(-)
diff --git a/docs/commands/add.md b/docs/commands/add.md
index c9617de..bbaa508 100644
--- a/docs/commands/add.md
+++ b/docs/commands/add.md
@@ -35,8 +35,10 @@ Options:
-t, --totp-secret []
The TOTP secret, if there is no value you will prompt it
- -c, --custom
+ -c, --custom
Add a custom field to the vault
+
+ If there is no value, you will enter it through a prompt
-f, --force
Force add, will not return error if there is a problem with the args.
@@ -45,9 +47,6 @@ Options:
-h, --help
Print help (see a summary with '-h')
-
- -V, --version
- Print version
```
So, to add a vault you need to provide a name for the vault, and you can provide
diff --git a/docs/commands/clean.md b/docs/commands/clean.md
index 75a29c0..255a8d0 100644
--- a/docs/commands/clean.md
+++ b/docs/commands/clean.md
@@ -4,10 +4,6 @@
```
Usage: lprs clean
-
-Options:
- -h, --help Print help
- -V, --version Print version
```
Is simple, just run `lprs clean` and the vaults file will be cleaned, removing
diff --git a/docs/commands/edit.md b/docs/commands/edit.md
index 5e11789..d3acbff 100644
--- a/docs/commands/edit.md
+++ b/docs/commands/edit.md
@@ -14,24 +14,28 @@ Options:
The new vault name
-u, --username
- The new vault username
+ The new vault username, make it empty string to delete it
-p, --password []
- The new password, if there is no value for it you will prompt it
+ The new password, make it empty string to delete it
+
+ If there is no value for it you will prompt it
-s, --service
- The new vault service
+ The new vault service, make it empty string to delete it
-o, --note
The new vault note
-t, --totp-secret []
- The TOTP secret, if there is no value you will prompt it
+ The TOTP secret, make it empty string to delete it
+
+ If there is no value you will prompt it
-c, --custom
- The custom field, make its value empty to delete it
+ The custom field, make it empty string to delete it
- If the custom field not exist will created it, if it's will update it
+ If the custom field not exist will created it, if it's will update it, if there is no value, you will enter it through a prompt (e.g `-c key`)
-f, --force
Force edit, will not return error if there is a problem with the args.
@@ -40,9 +44,6 @@ Options:
-h, --help
Print help (see a summary with '-h')
-
- -V, --version
- Print version
```
To edit a vault you need to provide the index or the name of the vault. If you
diff --git a/docs/commands/gen.md b/docs/commands/gen.md
index 6fad352..9296cca 100644
--- a/docs/commands/gen.md
+++ b/docs/commands/gen.md
@@ -14,7 +14,6 @@ Options:
-n, --numbers With numbers (0-9)
-s, --symbols With symbols (!,# ...)
-h, --help Print help
- -V, --version Print version
```
Generate a password with the specified length, by default the length is `18`,
diff --git a/docs/commands/get.md b/docs/commands/get.md
index e4f901e..69832f6 100644
--- a/docs/commands/get.md
+++ b/docs/commands/get.md
@@ -19,9 +19,6 @@ Arguments:
Options:
-h, --help
Print help (see a summary with '-h')
-
- -V, --version
- Print version
```
Get a single field from a vault, if the field is not provided, the whole vault
diff --git a/docs/commands/import-export.md b/docs/commands/import-export.md
index 42eb78b..c44e05a 100644
--- a/docs/commands/import-export.md
+++ b/docs/commands/import-export.md
@@ -24,9 +24,6 @@ Options:
-h, --help
Print help (see a summary with '-h')
-
- -V, --version
- Print version
```
## Export usage
diff --git a/docs/commands/list.md b/docs/commands/list.md
index 367c1bf..a3dfca7 100644
--- a/docs/commands/list.md
+++ b/docs/commands/list.md
@@ -10,7 +10,6 @@ Options:
-r, --regex Enable regex when use `--filter` option
--json Returns the output as `json` list of vaults
-h, --help Print help
- -V, --version Print version
```
Lprs `list` command is used to list all vaults in the vaults file, you can also
@@ -21,4 +20,5 @@ work with it with `jq`).
### Examples
+
diff --git a/docs/commands/remove.md b/docs/commands/remove.md
index ce4bdfd..7647221 100644
--- a/docs/commands/remove.md
+++ b/docs/commands/remove.md
@@ -11,7 +11,6 @@ Arguments:
Options:
-f, --force Force remove, will not return error if there is no vault with the given index or name
-h, --help Print help
- -V, --version Print version
```
To remove a vault you need to provide the index or the name of the vault. If you
diff --git a/src/clap_parsers.rs b/src/clap_parsers.rs
index 05a423e..7666505 100644
--- a/src/clap_parsers.rs
+++ b/src/clap_parsers.rs
@@ -19,12 +19,14 @@ use crate::{LprsError, LprsResult};
/// Parse the key & value arguments.
/// ## Errors
/// - If the argument value syntax not `key=value`
-pub fn kv_parser(value: &str) -> LprsResult<(String, String)> {
+pub fn kv_parser(value: &str) -> LprsResult<(String, Option)> {
if let Some((key, value)) = value.split_once('=') {
- Ok((key.trim().to_owned(), value.trim().to_owned()))
- } else {
+ Ok((key.trim().to_owned(), Some(value.trim().to_owned())))
+ } else if value.trim().is_empty() {
Err(LprsError::ArgParse(
- "There is no value, the syntax is `KEY=VALUE`".to_owned(),
+ "Invalid key, the syntax is `KEY(=VALUE)?`".to_owned(),
))
+ } else {
+ Ok((value.trim().to_owned(), None))
}
}
diff --git a/src/cli/add_command.rs b/src/cli/add_command.rs
index 1b02d0e..1fdaa8f 100644
--- a/src/cli/add_command.rs
+++ b/src/cli/add_command.rs
@@ -39,9 +39,11 @@ pub struct Add {
#[allow(clippy::option_option)]
totp_secret: Option