Skip to content

Development #22

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 6 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
76 changes: 72 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ The password database is created and is active now. You can start adding entries
Password (enter to generate new):
Generating password ...done
Notes: Website uses Nginx auth
Do you want to add custom fields [y/N]:
Created new entry with id: 1

You can now list the entry with one of the list options.
Expand All @@ -163,6 +164,36 @@ You can now list the entry with one of the list options.
Modified: 2021-21-09 23:12:35
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

## Add an entry with custom fields

From version 0.3 onwards, custom fields are supported.

$ varuh -A
Title: Github token
URL: https://github.com/mydev/myproject
Username: mydev
Password (enter to generate new): ghp_ipQrStuVwxYz1a2b3cdEF10ghI689kLaMnOp
Notes: Never Expires
Do you want to add custom fields [y/N]: y
Field Name: Domain
Value for Domain: github.com
Field Name: Type
Value for Type: Auth Token
Field Name:
Created new entry with id: 6

$ varuh -l 6
ID: 6
Title: Github token
User: mydev
URL: https://github.com/mydev/myproject
Password: ghp_ipQrStuVwxYz1a2b3cdEF10ghI689kLaMnOp
Notes: Never Expires
Domain: github.com
Type: Auth Token
Modified: 2021-21-13 00:07:18


For more on listing see the [Listing and Searching](#listing-and-searching) section below.

## Edit an entry
Expand All @@ -178,6 +209,7 @@ For more on listing see the [Listing and Searching](#listing-and-searching) sect
New Password ([y/Y] to generate new, enter will keep old one):
Current Notes: Website uses Nginx auth
New Notes: Website uses Apache
Do you want to add custom fields [y/N]:
Updated entry.

$ varuh -l 1 -s
Expand All @@ -191,14 +223,50 @@ For more on listing see the [Listing and Searching](#listing-and-searching) sect
Modified: 2021-21-09 23:15:29
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

## Edit an entry with custom fields

When you edit an entry with custom fields, you get the option to change the name of the fields or delete the fields entirely.

$ varuh -E 6
Current Title: Github token
New Title:
Current URL: https://github.com/mydev/myproject
New URL:
Current Username: mydev
New Username:
Current Password: ghp_ipQrStuVwxYz1a2b3cdEF10ghI689kLaMnOp
New Password ([y/Y] to generate new, enter will keep old one):
Current Notes: Never Expires
New Notes:
Editing/deleting custom fields
Field Name: Domain
New Field Name (Enter to keep, "x" to delete): x
Field Name: Type
New Field Name (Enter to keep, "x" to delete): Token Type
Field Value: Auth Token
New Field Value (Enter to keep):
Do you want to add custom fields [y/N]:
Created 1 custom entries for entry: 21.
Updated entry.

$ varuh -l 6 -s
ID: 6
Title: Github token
User: mydev
URL: https://github.com/mydev/myproject
Password: ghp_ipQrStuVwxYz1a2b3cdEF10ghI689kLaMnOp
Notes: Never Expires
Token Type: Auth Token
Modified: 2021-21-13 00:16:41

(*-s* turns on visible passwords)

## Clone an entry

To clone (copy) an entry,

$ $ varuh -C 1
Cloned to new entry, id: 2
Cloned to new entry, id: 3

## Remove an entry

Expand All @@ -207,8 +275,8 @@ To clone (copy) an entry,

It is an error if the id does not exist.

$ varuh -R 3
No entry with id 3 was found
$ varuh -R 4
No entry with id 4 was found

## Switch to a new database

Expand Down Expand Up @@ -260,7 +328,7 @@ Manually decrypt the database using `-d` option.

Now the database is active again and you can see the listings.

$ varuh -l 2
$ varuh -l 3
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ID: 2
Title: My Blog Login
Expand Down
94 changes: 92 additions & 2 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import (
"syscall"
)

type CustomEntry struct {
fieldName string
fieldValue string
}

// Wrappers (closures) for functions accepting strings as input for in/out encryption
func WrapperMaxKryptStringFunc(fn actionFunc) actionFunc {

Expand Down Expand Up @@ -219,6 +224,7 @@ func addNewEntry() error {
var notes string
var passwd string
var err error
var customEntries []CustomEntry

if err = checkActiveDatabase(); err != nil {
return err
Expand Down Expand Up @@ -260,8 +266,10 @@ func addNewEntry() error {
return errors.New("invalid input")
}

customEntries = addCustomFields(reader)

// Trim spaces
err = addNewDatabaseEntry(title, userName, url, passwd, notes)
err = addNewDatabaseEntry(title, userName, url, passwd, notes, customEntries)

if err != nil {
fmt.Printf("Error adding entry - \"%s\"\n", err.Error())
Expand All @@ -270,6 +278,86 @@ func addNewEntry() error {
return err
}

// Function to update existing custom entries and add new ones
// The bool part of the return value indicates whether to take action
func addOrUpdateCustomFields(reader *bufio.Reader, entry *Entry) ([]CustomEntry, bool) {

var customEntries []ExtendedEntry
var editedCustomEntries []CustomEntry
var newCustomEntries []CustomEntry
var flag bool

customEntries = getExtendedEntries(entry)

if len(customEntries) > 0 {

fmt.Println("Editing/deleting custom fields")
for _, customEntry := range customEntries {
var fieldName string
var fieldValue string

fmt.Println("Field Name: " + customEntry.FieldName)
fieldName = readInput(reader, "\tNew Field Name (Enter to keep, \"x\" to delete)")
if strings.ToLower(strings.TrimSpace(fieldName)) == "x" {
fmt.Println("Deleting field " + fieldName)
} else {
if strings.TrimSpace(fieldName) == "" {
fieldName = customEntry.FieldName
}

fmt.Println("Field Value: " + customEntry.FieldValue)
fieldValue = readInput(reader, "\tNew Field Value (Enter to keep)")
if strings.TrimSpace(fieldValue) == "" {
fieldValue = customEntry.FieldValue
}

editedCustomEntries = append(editedCustomEntries, CustomEntry{fieldName, fieldValue})
}
}
}

newCustomEntries = addCustomFields(reader)

editedCustomEntries = append(editedCustomEntries, newCustomEntries...)

// Cases where length == 0
// 1. Existing entries - all deleted
flag = len(customEntries) > 0 || len(editedCustomEntries) > 0

return editedCustomEntries, flag
}

// Function to add custom fields to an entry
func addCustomFields(reader *bufio.Reader) []CustomEntry {

// Custom fields
var custom string
var customEntries []CustomEntry

custom = readInput(reader, "Do you want to add custom fields [y/N]")
if strings.ToLower(custom) == "y" {

fmt.Println("Keep entering custom field name followed by the value. Press return with no input once done.")
for true {
var customFieldName string
var customFieldValue string

customFieldName = strings.TrimSpace(readInput(reader, "Field Name"))
if customFieldName != "" {
customFieldValue = strings.TrimSpace(readInput(reader, "Value for "+customFieldName))
}

if customFieldName == "" && customFieldValue == "" {
break
}

customEntries = append(customEntries, CustomEntry{customFieldName, customFieldValue})
}
}

return customEntries
}

// Edit a current entry by id
func editCurrentEntry(idString string) error {

Expand Down Expand Up @@ -322,8 +410,10 @@ func editCurrentEntry(idString string) error {
fmt.Printf("\nCurrent Notes: %s\n", entry.Notes)
notes = readInput(reader, "New Notes")

customEntries, flag := addOrUpdateCustomFields(reader, entry)

// Update
err = updateDatabaseEntry(entry, title, userName, url, passwd, notes)
err = updateDatabaseEntry(entry, title, userName, url, passwd, notes, customEntries, flag)
if err != nil {
fmt.Printf("Error updating entry - \"%s\"\n", err.Error())
}
Expand Down
Loading