Skip to content
Permalink
Browse files

Add rmdir command to remove groups

  • Loading branch information...
Raggaer committed Feb 21, 2019
1 parent 1800c44 commit ad001455c147bc6159292ec28fa4e47b9adefc11
Showing with 91 additions and 2 deletions.
  1. +7 −1 README.md
  2. +10 −0 command.go
  3. +1 −1 entry.go
  4. +73 −0 group.go
@@ -19,12 +19,18 @@ You can use the `help` command while a database is opened to view the list of co
- `exit` Closes the application
- `save` Saves the database
- `mkdir` Shows and processes a form to create a new group
- `rmdir` Removes a group (sends the group to the recycle bin)
- `new` Shows and processes a form to create a new entry
- `rm` Removes an entry from the current working group
- `rm` Removes an entry from the current working group (sends the entry to the recycle bin)
- `show` Shows an entry from the current working group
- `search` Searches entries (by title) from the current working group
- `save` Saves the database to disk

## Deleting groups and entries

When an entry or a group is deleted we move it to the `Recycle Bin` group (this will be created if its missing).
You can delete delete the entry forever or just leave it there as some sort of backup folder

## Clipboard

Commands like `xu` and `xp` copy the content to the system clipboard, making use of [https://github.com/atotto/clipboard](https://github.com/atotto/clipboard).
@@ -118,6 +118,16 @@ var commands = []command{
Help: "Performs a fuzzy search on all the current group entries, by title (search <query>)",
HelpSmall: "Performs a fuzzy search on all the current group entries, by title (search <query>)",
},
{
Key: "rmdir",
Fn: func(args []string) {
if len(args) >= 1 {
rmdir(args)
}
},
Help: "Deletes a group (rmdir <group_name|number>)",
HelpSmall: "Deletes a group (rmdir <group_name|number>)",
},
}

func handleUserInput(input string) {
@@ -193,7 +193,7 @@ func xp(args []string) {
}

// Command "xw" copies an entry URL
func xp(args []string) {
func xw(args []string) {
entry := args[0]
e := getEntryByNameOrId(entry)
if e == nil {
@@ -2,6 +2,9 @@ package main

import (
"fmt"
"log"
"strconv"
"strings"

"github.com/tobischo/gokeepasslib/v2"
)
@@ -20,6 +23,76 @@ func mkdir(args []string) {
}
}

// Command "rmdir" deletes a group from the current working group
func rmdir(args []string) {
log.Println(args[0])
group := getGroupByNameOrId(args[0])
log.Println(group)
if group == nil || args[0] == recycleBinGroup {
return
}
if !deleteGroup(group) {
return
}
fmt.Printf("Group '%s' was removed\r\n", group.Name)
fmt.Print("Database was changed. Save database? (y/N): ")
activeForm = &form{
Fn: databaseChangedSaveAlert,
}
}

func deleteGroup(group *gokeepasslib.Group) bool {
g := currentGroup()
for k, e := range g.Groups {
if strings.ToLower(e.Name) == strings.ToLower(group.Name) {
// Delete group from the current group
g.Groups = append(g.Groups[:k], g.Groups[k+1:]...)

// Move group to recycle bin
moveGroupToRecycleBin(group)
return true
}
}
return false
}

func getGroupByNameOrId(group string) *gokeepasslib.Group {
eid, err := strconv.Atoi(group)
if err != nil {
for _, e := range currentGroup().Groups {
if strings.ToLower(e.Name) == strings.ToLower(group) {
return &e
}
}
return nil
}
eid--
for k := range currentGroup().Groups {
if k == eid {
e := currentGroup().Groups[eid]
return &e
}
}
return nil
}

func moveGroupToRecycleBin(group *gokeepasslib.Group) {
// Recycle bin group is at the root, if it does not exist we create it
for i, g := range database.Content.Root.Groups[0].Groups {
if g.Name == recycleBinGroup {
n := gokeepasslib.NewGroup()
n.Name = group.Name
n.Entries = group.Entries
database.Content.Root.Groups[0].Groups[i].Groups = append(database.Content.Root.Groups[0].Groups[i].Groups, n)
return
}
}
bin := gokeepasslib.NewGroup()
bin.Name = recycleBinGroup
database.Content.Root.Groups[0].Groups = append(database.Content.Root.Groups[0].Groups, bin)

}

func createNewGroup(f *form, input string) {
// Retrieve form data
data, ok := f.Data.(*newGroupForm)

0 comments on commit ad00145

Please sign in to comment.
You can’t perform that action at this time.