Skip to content

Commit

Permalink
Add path for entries and groups commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Raggaer committed Mar 18, 2019
1 parent 0066e6f commit 467a7ff
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
16 changes: 8 additions & 8 deletions command.go
Expand Up @@ -52,8 +52,8 @@ var commands = []command{
show(args) show(args)
} }
}, },
Help: "Shows an entry (show <entry name|number>)", Help: "Shows an entry (show <entry name|number|entry path>)",
HelpSmall: "Shows an entry (show <entry name|number>)", HelpSmall: "Shows an entry (show <entry name|number|entry path>)",
}, },
{ {
Key: "xp", Key: "xp",
Expand All @@ -62,8 +62,8 @@ var commands = []command{
xp(args) xp(args)
} }
}, },
Help: "Copies an entry password (xp <entry name|number>)", Help: "Copies an entry password (xp <entry name|number|entry path>)",
HelpSmall: "Copies an entry password (xp <entry name|number>)", HelpSmall: "Copies an entry password (xp <entry name|number|entry path>)",
}, },
{ {
Key: "xw", Key: "xw",
Expand All @@ -72,8 +72,8 @@ var commands = []command{
xw(args) xw(args)
} }
}, },
Help: "Copies an entry URL (xw <entry name|number>)", Help: "Copies an entry URL (xw <entry name|number|entry path>)",
HelpSmall: "Copies an entry URL (xw <entry name|number>)", HelpSmall: "Copies an entry URL (xw <entry name|number|entry path>)",
}, },
{ {
Key: "xu", Key: "xu",
Expand All @@ -82,8 +82,8 @@ var commands = []command{
xu(args) xu(args)
} }
}, },
Help: "Copies an entry username (xu <entry name|number>)", Help: "Copies an entry username (xu <entry name|number|entry path>)",
HelpSmall: "Copies an entry username (xu <entry name|number>)", HelpSmall: "Copies an entry username (xu <entry name|number|entry path>)",
}, },
{ {
Key: "mkdir", Key: "mkdir",
Expand Down
47 changes: 43 additions & 4 deletions entry.go
Expand Up @@ -129,6 +129,45 @@ func getEntryByNameOrId(entry string) *gokeepasslib.Entry {
return &e return &e
} }
} }

return nil
}

func getEntryByPath(path string) *gokeepasslib.Entry {
// Try to retrieve entry by path
movedPaths := 0
paths := strings.Split(path, "/")
pathRange:
for i, path := range paths {
// If its the entry from the path
if i == len(paths)-1 {
e := getEntryByNameOrId(path)
// Remove moved paths
groupHistory = groupHistory[0 : len(groupHistory)-movedPaths]
return e
}

// Move to next group
gid, err := strconv.Atoi(path)
if err != nil {
for x, g := range currentGroup().Groups {
if strings.ToLower(g.Name) == strings.ToLower(path) {
groupHistory = append(groupHistory, x)
continue pathRange
}
}
} else {
gid--
for x := range currentGroup().Groups {
if x == gid {
groupHistory = append(groupHistory, x)
movedPaths++
continue pathRange
}
}
}
}

return nil return nil
} }


Expand Down Expand Up @@ -181,7 +220,7 @@ func search(args []string) {


// Command "show" shows information about an entry // Command "show" shows information about an entry
func show(args []string) { func show(args []string) {
entry := getEntryByNameOrId(args[0]) entry := getEntryByPath(args[0])
if entry == nil { if entry == nil {
return return
} }
Expand Down Expand Up @@ -280,7 +319,7 @@ func generateEntryPassword(input string) (string, error) {
// Command "xp" copies an entry password // Command "xp" copies an entry password
func xp(args []string) { func xp(args []string) {
entry := args[0] entry := args[0]
e := getEntryByNameOrId(entry) e := getEntryByPath(entry)
if e == nil { if e == nil {
return return
} }
Expand All @@ -298,7 +337,7 @@ func xp(args []string) {
// Command "xw" copies an entry URL // Command "xw" copies an entry URL
func xw(args []string) { func xw(args []string) {
entry := args[0] entry := args[0]
e := getEntryByNameOrId(entry) e := getEntryByPath(entry)
if e == nil { if e == nil {
return return
} }
Expand All @@ -316,7 +355,7 @@ func xw(args []string) {
// Command "xu" copies an entry username // Command "xu" copies an entry username
func xu(args []string) { func xu(args []string) {
entry := args[0] entry := args[0]
e := getEntryByNameOrId(entry) e := getEntryByPath(entry)
if e == nil { if e == nil {
return return
} }
Expand Down

0 comments on commit 467a7ff

Please sign in to comment.