keepass-cli/cmd/rofi.go

142 lines
3.2 KiB
Go
Raw Normal View History

2024-05-15 18:42:38 +00:00
/*
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
*/
package cmd
import (
"errors"
"fmt"
2024-05-29 20:43:59 +00:00
"sort"
2024-05-15 18:42:38 +00:00
"strings"
"git.bullercodeworks.com/brian/keepass-cli/models"
"git.bullercodeworks.com/brian/keepass-cli/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// rofiCmd represents the rofi command
var rofiCmd = &cobra.Command{
Use: "rofi",
Short: "Rofi Menufication of keepass-cli",
RunE: runRofi,
}
func init() {
rootCmd.AddCommand(rofiCmd)
}
func runRofi(cmd *cobra.Command, args []string) error {
var db *models.KeePassDB
2024-05-15 20:10:38 +00:00
2024-05-17 12:50:03 +00:00
var argIsPw bool
2024-05-15 18:42:38 +00:00
pwFile := fmt.Sprintf("%s%s", ConfigDir, "pw")
2024-05-15 20:10:38 +00:00
pass, err := util.ReadPWFile(pwFile)
2024-05-15 18:42:38 +00:00
if err != nil {
// See if we have a password...
if len(args) == 1 {
pass = args[0]
2024-05-17 12:50:03 +00:00
argIsPw = true
2024-05-15 18:42:38 +00:00
} else {
fmt.Println("Enter Master Password")
return nil
}
}
2024-05-15 20:10:38 +00:00
2024-05-15 18:42:38 +00:00
db, err = models.NewKeePassDB(viper.GetString("database"), pass)
if err != nil {
2024-05-17 12:50:03 +00:00
util.RemovePWFile(pwFile)
return fmt.Errorf("Error opening DB. Re-enter Master Password:\n%v", err)
} else if err = util.WritePWFile(pwFile, pass); err != nil {
return fmt.Errorf("DB Opened, but failed to persist password: %v", err)
2024-05-15 18:42:38 +00:00
}
2024-05-17 12:50:03 +00:00
var path []string
for i := range args {
path = append(path, strings.Split(args[i], "/")...)
}
2024-05-29 20:43:59 +00:00
var freqs *models.Frequencies
2024-05-17 12:50:03 +00:00
if len(path) == 0 || argIsPw {
2024-05-15 18:42:38 +00:00
list := db.GetAllEntriesFromRoot()
2024-05-29 20:43:59 +00:00
var printList []string
2024-05-15 18:42:38 +00:00
for i := range list {
2024-05-29 20:43:59 +00:00
printList = append(printList, strings.Join(list[i], "/"))
}
freqs, err = models.LoadFrequencies()
if err == nil {
sort.Slice(printList, func(i, j int) bool {
return freqs.GetTimes(printList[i]) > freqs.GetTimes(printList[j])
})
}
for i := range printList {
fmt.Println(printList[i])
2024-05-15 18:42:38 +00:00
}
} else {
if entry, entryErr := db.FindEntryFromRoot(path); entryErr != nil {
return entryErr
} else {
2024-05-29 20:43:59 +00:00
freqPath := strings.Join(path, "/")
freqs, err = models.LoadFrequencies()
if err == nil {
freqs.IncrementEntry(freqPath)
freqs.Save()
}
2024-05-15 18:42:38 +00:00
return util.WriteToClipboard(entry.GetPassword())
}
}
return nil
}
func runRofiSteps(cmd *cobra.Command, args []string) error {
var path []string
for i := range args {
path = append(path, strings.Split(args[i], "/")...)
}
var db *models.KeePassDB
var havePassword bool
pwFile := fmt.Sprintf("%s%s", ConfigDir, "pw")
pass, err := util.ReadFile(pwFile)
if err != nil {
havePassword = false
// See if we have a password...
if len(args) == 1 {
pass = args[0]
} else {
fmt.Println("Enter Master Password")
return nil
}
} else {
havePassword = true
}
db, err = models.NewKeePassDB(viper.GetString("database"), pass)
if err != nil {
return err
} else if !havePassword {
if err = util.WritePWFile(pwFile, pass); err != nil {
return err
}
path = []string{}
}
list := db.GetGroupsAndEntriesFromRoot(path)
if len(list) == 0 {
return errors.New("Invalid Path")
} else if len(list) > 1 {
for i := range list {
fmt.Println(strings.Join(list[i], "/"))
}
} else {
// Only one... Could be a single group nested, or it's an entry
entry, entryErr := db.FindEntryFromRoot(path)
if entryErr == nil {
return util.WriteToClipboard(entry.GetPassword())
}
// It's a group
fmt.Println(strings.Join(list[0], "/"))
}
return nil
}