Skip to content

Development #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 44 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,38 +69,39 @@ The binary will be installed in `/usr/local/bin` folder.
Usage
=====

$ ./varuh -h
$ varuh -h
usage: varuh [-h|--help] [-I|--init "<value>"] [-d|--decrypt "<value>"]
[-C|--clone "<value>"] [-R|--remove "<value>"] [-U|--use-db
"<value>"] [-f|--find "<value>"] [-E|--edit "<value>"]
[-l|--list-entry "<value>"] [-x|--export "<value>"] [-e|--encrypt]
[-A|--add] [-p|--path] [-a|--list-all] [-g|--genpass] [-s|--show]
[-c|--copy] [-y|--assume-yes] [-v|--version]
"<value>"] [-E|--edit "<value>"] [-l|--list-entry "<value>"]
[-x|--export "<value>"] [-f|--find "<value>" [-f|--find "<value>"
...]] [-e|--encrypt] [-A|--add] [-p|--path] [-a|--list-all]
[-g|--genpass] [-s|--show] [-c|--copy] [-y|--assume-yes]
[-v|--version]

Password manager for the command line for Unix like operating
systems

Options:

-h --help Print help information
-I --init <path> Initialize a new database
-d --decrypt <path> Decrypt password database
-C --clone <id> Clone an entry with <id>
-R --remove <id> Remove an entry with <id> or <id-range>
-U --use-db <path> Set <path> as active database
-f --find <term> Search entries with <term>
-E --edit <id> Edit entry by <id>
-l --list-entry <id> List entry by <id>
-x --export <filename> Export all entries to <filename>
-e --encrypt Encrypt the current database
-A --add Add a new entry
-p --path Show current database path
-a --list-all List all entries in current database
-g --genpass Generate a strong password (length: 12 - 16)
-s --show Show passwords when listing entries
-c --copy Copy password to clipboard
-y --assume-yes Assume yes to actions requiring confirmation
-v --version Show version information and exit
-h --help Print help information
-I --init <path> Initialize a new database
-d --decrypt <path> Decrypt password database
-C --clone <id> Clone an entry with <id>
-R --remove <id> Remove an entry with <id> or <id-range>
-U --use-db <path> Set <path> as active database
-E --edit <id> Edit entry by <id>
-l --list-entry <id> List entry by <id>
-x --export <filename> Export all entries to <filename>
-f --find <t1> <t2> ... Search entries with terms
-e --encrypt Encrypt the current database
-A --add Add a new entry
-p --path Show current database path
-a --list-all List all entries in current database
-g --genpass Generate a strong password (length: 12 - 16)
-s --show Show passwords when listing entries
-c --copy Copy password to clipboard
-y --assume-yes Assume yes to actions requiring confirmation
-v --version Show version information and exit


AUTHORS
Expand Down Expand Up @@ -441,6 +442,25 @@ An entry can be searched on its title, username, URL or notes. Search is case-in
Modified: 2021-21-25 15:09:51
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


## To search using multiple terms

The `-f` option supports multiple terms, so you can specify this more than one time to narrow a search down to a specific entry.

$ varuh -f google -f anand
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ID: 8
Title: Google account
User: [email protected]
URL:
Password: **********
Notes:
Modified: 2021-21-25 15:02:50
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

$ varuh -f google -f priya
Entry for "google priya" not found

## To list all entries

To list all entries, use the option `-a`.
Expand Down
5 changes: 4 additions & 1 deletion actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,15 @@ func findCurrentEntry(term string) error {

var err error
var entries []Entry
var terms []string

if err = checkActiveDatabase(); err != nil {
return err
}

err, entries = searchDatabaseEntry(term)
terms = strings.Split(term, " ")

err, entries = searchDatabaseEntries(terms, "AND")
if err != nil || len(entries) == 0 {
fmt.Printf("Entry for query \"%s\" not found\n", term)
return err
Expand Down
69 changes: 68 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func addNewDatabaseEntry(title, userName, url, passwd, notes string, customEntri

err, db = openActiveDatabase()
if err == nil && db != nil {
// result := db.Debug().Create(&entry)
// result := db.Debug().Create(&entry)
result := db.Create(&entry)
if result.Error == nil && result.RowsAffected == 1 {
// Add custom fields if given
Expand Down Expand Up @@ -341,6 +341,73 @@ func searchDatabaseEntry(term string) (error, []Entry) {

}

// Union of two entry arrays
func union(entry1 []Entry, entry2 []Entry) []Entry {

m := make(map[int]bool)

for _, item := range entry1 {
m[item.ID] = true
}

for _, item := range entry2 {
if _, ok := m[item.ID]; !ok {
entry1 = append(entry1, item)
}
}

return entry1
}

// Intersection of two entry arrays
func intersection(entry1 []Entry, entry2 []Entry) []Entry {

var common []Entry

m := make(map[int]bool)

for _, item := range entry1 {
m[item.ID] = true
}

for _, item := range entry2 {
if _, ok := m[item.ID]; ok {
common = append(common, item)
}
}

return common
}

// Search database for the given terms and returns matches according to operator
func searchDatabaseEntries(terms []string, operator string) (error, []Entry) {

var err error
var finalEntries []Entry

for idx, term := range terms {
var entries []Entry

err, entries = searchDatabaseEntry(term)
if err != nil {
fmt.Printf("Error searching for term: %s - \"%s\"\n", term, err.Error())
return err, entries
}

if idx == 0 {
finalEntries = entries
} else {
if operator == "AND" {
finalEntries = intersection(finalEntries, entries)
} else if operator == "OR" {
finalEntries = union(finalEntries, entries)
}
}
}

return nil, finalEntries
}

// Remove a given database entry
func removeDatabaseEntry(entry *Entry) error {

Expand Down
27 changes: 25 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/pythonhacker/argparse"
"os"
"strings"
)

const VERSION = 0.3
Expand Down Expand Up @@ -86,13 +87,16 @@ func performAction(optMap map[string]interface{}) {
"edit": WrapperMaxKryptStringFunc(editCurrentEntry),
"init": initNewDatabase,
"list-entry": WrapperMaxKryptStringFunc(listCurrentEntry),
"find": WrapperMaxKryptStringFunc(findCurrentEntry),
"remove": WrapperMaxKryptStringFunc(removeCurrentEntry),
"clone": WrapperMaxKryptStringFunc(copyCurrentEntry),
"use-db": setActiveDatabasePath,
"export": exportToFile,
}

stringListActionsMap := map[string]actionFunc{
"find": WrapperMaxKryptStringFunc(findCurrentEntry),
}

stringActions2Map := map[string]actionFunc2{
"decrypt": decryptDatabase,
}
Expand Down Expand Up @@ -146,6 +150,18 @@ func performAction(optMap map[string]interface{}) {
}
}

for key, mappedFunc := range stringListActionsMap {
if len(*optMap[key].(*[]string)) > 0 {

var vals = *(optMap[key].(*[]string))
// Convert to single string
var singleVal = strings.Join(vals, " ")
mappedFunc(singleVal)
flag = true
break
}
}

if flag {
return
}
Expand All @@ -171,7 +187,6 @@ func initializeCmdLine(parser *argparse.Parser) map[string]interface{} {
{"C", "clone", "Clone an entry with <id>", "<id>", ""},
{"R", "remove", "Remove an entry with <id> or <id-range>", "<id>", ""},
{"U", "use-db", "Set <path> as active database", "<path>", ""},
{"f", "find", "Search entries with <term>", "<term>", ""},
{"E", "edit", "Edit entry by <id>", "<id>", ""},
{"l", "list-entry", "List entry by <id>", "<id>", ""},
{"x", "export", "Export all entries to <filename>", "<filename>", ""},
Expand All @@ -181,6 +196,14 @@ func initializeCmdLine(parser *argparse.Parser) map[string]interface{} {
optMap[opt.Long] = parser.String(opt.Short, opt.Long, &argparse.Options{Help: opt.Help, Path: opt.Path})
}

stringListOptions := []CmdOption{
{"f", "find", "Search entries with terms", "<t1> <t2> ...", ""},
}

for _, opt := range stringListOptions {
optMap[opt.Long] = parser.StringList(opt.Short, opt.Long, &argparse.Options{Help: opt.Help, Path: opt.Path})
}

boolOptions := []CmdOption{
{"e", "encrypt", "Encrypt the current database", "", ""},
{"A", "add", "Add a new entry", "", ""},
Expand Down