Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
tab completion, stubs for lock/unlock
Browse files Browse the repository at this point in the history
  • Loading branch information
ojensen5115 committed Jun 15, 2017
1 parent f6c3115 commit ee9a405
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pw"
version = "0.1.0"
version = "0.1.1"
authors = ["Oliver Jensen <[email protected]>"]

[dependencies.rusqlite]
Expand Down
28 changes: 28 additions & 0 deletions bash_completion
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
_pw()
{
local cur prev
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"

commands="add edit delete list show copy lock unlock"

case "$prev" in
pw)
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
return 0
;;
edit|delete|show|copy)
COMPREPLY=( $(compgen -W "`pw --comp-name`" -- ${cur}) )
return 0
;;
add|list)
COMPREPLY=( $(compgen -W "sections `pw --comp-sec`" -- ${cur}) )
return 0
;;
*)
;;
esac

}
complete -F _pw pw
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ Make sure you have rust / cargo / etc. installed. Then:
git clone [email protected]:ojensen5115/pw.git # Clone the repo
cd pw # Enter the directory
cargo install # Copy the binary to your local path
cat bash_completion >> ~/.bash_completion # Set up bash completion
source ~/.bash_completion # Get bash completion going in this shell
```
## Data Storage
Expand Down
42 changes: 42 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ Usage:
pw list <category>
pw show <name>
pw copy <name> (u|p)
pw (lock | unlock)
pw [options]
Options:
-h --help Show this screen.
--version Show version.
--comp-name List credential names for tab completion
--comp-sec List categories for tab completion
";

#[derive(Debug, Deserialize)]
Expand All @@ -64,6 +67,12 @@ struct Args {
cmd_u: bool,
cmd_p: bool,

cmd_lock: bool,
cmd_unlock: bool,

flag_comp_name: bool,
flag_comp_sec: bool,

arg_name: String,
arg_category: Option<String>
}
Expand Down Expand Up @@ -110,6 +119,18 @@ fn main() {
else if args.cmd_delete {
delete_credential(&conn, args.arg_name);
}
else if args.cmd_lock {
println!("Not yet implemented");
}
else if args.cmd_unlock {
println!("Not yet implemented");
}
else if args.flag_comp_name {
completion_name(&conn);
}
else if args.flag_comp_sec {
completion_sec(&conn);
}

}

Expand All @@ -118,6 +139,9 @@ fn new_credential(conn: &rusqlite::Connection, category: Option<String>, name: S
Some(c) => c,
None => "".to_string()
};
// To make tab completion reasonable, we replace spaces with underscores in name and category
let name = name.replace(" ", "_");
let category = category.replace(" ", "_");

if category == "categories" {
println!("You cannot use the category 'categories'.");
Expand Down Expand Up @@ -185,6 +209,24 @@ fn list_credentials(conn: &rusqlite::Connection, category: Option<String>) {
}
}

fn completion_name(conn: &rusqlite::Connection) {
let mut statement = conn.prepare("SELECT name FROM credentials").unwrap();
let mut rows = statement.query(&[]).unwrap();
while let Some(result_row) = rows.next() {
let entry: String = result_row.unwrap().get(0);
print!("{} ", entry);
}
}

fn completion_sec(conn: &rusqlite::Connection) {
let mut statement = conn.prepare("SELECT distinct(category) FROM credentials").unwrap();
let mut rows = statement.query(&[]).unwrap();
while let Some(result_row) = rows.next() {
let entry: String = result_row.unwrap().get(0);
print!("{} ", entry);
}
}

fn show_credential(conn: &rusqlite::Connection, name: String) {
let credential = get_credential(conn, name);
println!("{}:\n {}\n {}", credential.name, credential.username, credential.password);
Expand Down

0 comments on commit ee9a405

Please sign in to comment.