Skip to content
Permalink
Browse files

Add command shortcuts

  • Loading branch information...
Raggaer committed Mar 26, 2019
1 parent 467a7ff commit fec3a7eb2cd74e51831bc99347fef7c6295e6cb2
Showing with 216 additions and 137 deletions.
  1. +10 −0 README.md
  2. +151 −136 command.go
  3. +12 −1 main.go
  4. +43 −0 shortcut.go
@@ -44,6 +44,16 @@ You can use the `help` command while a database is opened to view the list of co
- `backup` Backups the database file
- `xx` Clears the clipboard

### Shortcuts

You can declare shortcuts inside `~/.config/gokpcli/config` with the following structure:

`alias command input`

For example, a shortcut called `g1` to retrieve a password from group `Email` and entry `Gmail` would like like this:

`g1 xp Email/Gmail`

## 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).
@@ -14,152 +14,155 @@ type command struct {
HelpSmall string
}

var commands = []command{
{
Key: "backup",
Fn: func(args []string) {
if err := backupDatabase(); err != nil {
fmt.Println("Unable to backup database:")
fmt.Println(err.Error())
} else {
fmt.Println("Database backup created")
}
var (
shortcuts = []*shortcut{}
commands = []command{
{
Key: "backup",
Fn: func(args []string) {
if err := backupDatabase(); err != nil {
fmt.Println("Unable to backup database:")
fmt.Println(err.Error())
} else {
fmt.Println("Database backup created")
}
},
Help: "Backups the database file. The new backup is saved as yyyy-mm-dd_hh-ii-ss_name.kdbx",
HelpSmall: "Backups the database file. The new backup is saved as yyyy-mm-dd_hh-ii-ss_name.kdbx",
},
Help: "Backups the database file. The new backup is saved as yyyy-mm-dd_hh-ii-ss_name.kdbx",
HelpSmall: "Backups the database file. The new backup is saved as yyyy-mm-dd_hh-ii-ss_name.kdbx",
},
{
Key: "save",
Fn: func(args []string) {
if err := saveDatabase(); err != nil {
fmt.Println("Unable to save database")
fmt.Println(err.Error())
}
{
Key: "save",
Fn: func(args []string) {
if err := saveDatabase(); err != nil {
fmt.Println("Unable to save database")
fmt.Println(err.Error())
}
},
Help: "Saves the database to disk",
HelpSmall: "Saves the database to disk",
},
Help: "Saves the database to disk",
HelpSmall: "Saves the database to disk",
},
{
Key: "exit",
Fn: exit,
Help: "Exits the application",
HelpSmall: "Exits the application",
},
{
Key: "show",
Fn: func(args []string) {
if len(args) >= 1 {
show(args)
}
{
Key: "exit",
Fn: exit,
Help: "Exits the application",
HelpSmall: "Exits the application",
},
Help: "Shows an entry (show <entry name|number|entry path>)",
HelpSmall: "Shows an entry (show <entry name|number|entry path>)",
},
{
Key: "xp",
Fn: func(args []string) {
if len(args) >= 1 {
xp(args)
}
{
Key: "show",
Fn: func(args []string) {
if len(args) >= 1 {
show(args)
}
},
Help: "Shows an entry (show <entry name|number|entry path>)",
HelpSmall: "Shows an entry (show <entry name|number|entry path>)",
},
Help: "Copies an entry password (xp <entry name|number|entry path>)",
HelpSmall: "Copies an entry password (xp <entry name|number|entry path>)",
},
{
Key: "xw",
Fn: func(args []string) {
if len(args) >= 1 {
xw(args)
}
{
Key: "xp",
Fn: func(args []string) {
if len(args) >= 1 {
xp(args)
}
},
Help: "Copies an entry password (xp <entry name|number|entry path>)",
HelpSmall: "Copies an entry password (xp <entry name|number|entry path>)",
},
Help: "Copies an entry URL (xw <entry name|number|entry path>)",
HelpSmall: "Copies an entry URL (xw <entry name|number|entry path>)",
},
{
Key: "xu",
Fn: func(args []string) {
if len(args) >= 1 {
xu(args)
}
{
Key: "xw",
Fn: func(args []string) {
if len(args) >= 1 {
xw(args)
}
},
Help: "Copies an entry URL (xw <entry name|number|entry path>)",
HelpSmall: "Copies an entry URL (xw <entry name|number|entry path>)",
},
Help: "Copies an entry username (xu <entry name|number|entry path>)",
HelpSmall: "Copies an entry username (xu <entry name|number|entry path>)",
},
{
Key: "mkdir",
Fn: mkdir,
Help: "Creates a new group inside the current group",
HelpSmall: "Creates a new group inside the current group",
},
{
Key: "new",
Fn: ne,
Help: "Creates a new entry inside the current group",
HelpSmall: "Creates a new entry inside the current group",
},
{
Key: "ls",
Fn: ls,
Help: "Lists entries and groups of the current group",
HelpSmall: "Lists entries and groups of the current group",
},
{
Key: "cd",
Fn: func(args []string) {
if len(args) >= 1 {
cd(args)
}
{
Key: "xu",
Fn: func(args []string) {
if len(args) >= 1 {
xu(args)
}
},
Help: "Copies an entry username (xu <entry name|number|entry path>)",
HelpSmall: "Copies an entry username (xu <entry name|number|entry path>)",
},
Help: "Change directory (path to a group)",
HelpSmall: "Change directory (path to a group)",
},
{
Key: "rm",
Fn: func(args []string) {
if len(args) >= 1 {
rm(args)
}
{
Key: "mkdir",
Fn: mkdir,
Help: "Creates a new group inside the current group",
HelpSmall: "Creates a new group inside the current group",
},
Help: "Removes an entry of the current group (rm <entry name|number>)",
HelpSmall: "Removes an entry of the current group (rm <entry name|number>)",
},
{
Key: "search",
Fn: func(args []string) {
if len(args) >= 1 {
search(args)
}
{
Key: "new",
Fn: ne,
Help: "Creates a new entry inside the current group",
HelpSmall: "Creates a new entry inside the current group",
},
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)
}
{
Key: "ls",
Fn: ls,
Help: "Lists entries and groups of the current group",
HelpSmall: "Lists entries and groups of the current group",
},
Help: "Deletes a group (rmdir <group name|number>)",
HelpSmall: "Deletes a group (rmdir <group name|number>)",
},
{
Key: "edit",
Fn: func(args []string) {
if len(args) >= 1 {
edit(args)
}
{
Key: "cd",
Fn: func(args []string) {
if len(args) >= 1 {
cd(args)
}
},
Help: "Change directory (path to a group)",
HelpSmall: "Change directory (path to a group)",
},
Help: "Modifies an entry (edit <path|number>)",
HelpSmall: "Modifies an entry (edit <path|number>)",
},
{
Key: "xx",
Fn: xx,
Help: "Clears the clipboard",
HelpSmall: "Clears the clipboard",
},
}
{
Key: "rm",
Fn: func(args []string) {
if len(args) >= 1 {
rm(args)
}
},
Help: "Removes an entry of the current group (rm <entry name|number>)",
HelpSmall: "Removes an entry of the current group (rm <entry name|number>)",
},
{
Key: "search",
Fn: func(args []string) {
if len(args) >= 1 {
search(args)
}
},
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>)",
},
{
Key: "edit",
Fn: func(args []string) {
if len(args) >= 1 {
edit(args)
}
},
Help: "Modifies an entry (edit <path|number>)",
HelpSmall: "Modifies an entry (edit <path|number>)",
},
{
Key: "xx",
Fn: xx,
Help: "Clears the clipboard",
HelpSmall: "Clears the clipboard",
},
}
)

func handleUserInput(input string) {
args := strings.Split(input, " ")
@@ -182,7 +185,19 @@ func handleUserInput(input string) {
for _, command := range commands {
if command.Key == args[0] {
command.Fn(args[1:])
break
return
}
}

// If no command match then try to match against shortcuts
for _, shortcut := range shortcuts {
if shortcut.Alias == args[0] {
for _, command := range commands {
if command.Key == shortcut.Command {
command.Fn(shortcut.Input)
return
}
}
}
}
}
13 main.go
@@ -7,6 +7,8 @@ import (
"io/ioutil"
"os"
"os/signal"
"os/user"
"strconv"
"strings"
"syscall"
"time"
@@ -40,7 +42,16 @@ func main() {
return
}

fmt.Println("gokpcli is ready for operation")
// Load command shortcuts
user, err := user.Current()
if err != nil {
fmt.Println("Unable to retrieve current user information")
fmt.Println(err.Error())
return
}
shortcuts = append(loadShortcuts(user.HomeDir + "/.config/gokpcli/config"))

fmt.Println("gokpcli is ready for operation. Loaded " + strconv.Itoa(len(shortcuts)) + " shortcuts")
fmt.Println("Type 'help' for a description of available commands")
fmt.Println("Type 'help <command>' for details on individual commands")
fmt.Print(waitCommandMessage)
@@ -0,0 +1,43 @@
package main

import (
"bufio"
"bytes"
"io/ioutil"
"strings"
)

type shortcut struct {
Alias string
Command string
Input []string
}

func loadShortcuts(path string) []*shortcut {
content, err := ioutil.ReadFile(path)
if err != nil {
return nil
}
scanner := bufio.NewScanner(bytes.NewBuffer(content))
shortcuts := []*shortcut{}
for scanner.Scan() {
tt := strings.TrimSpace(scanner.Text())
// Skip comments
if strings.HasPrefix(tt, "#") {
continue
}
parts := strings.Split(tt, " ")
if len(parts) <= 1 {
continue
}
alias, parts := parts[0], parts[1:]
command, parts := parts[0], parts[1:]
s := &shortcut{
Command: command,
Alias: alias,
Input: parts,
}
shortcuts = append(shortcuts, s)
}
return shortcuts
}

0 comments on commit fec3a7e

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