Add Username Function

This commit is contained in:
Brian Buller 2024-10-09 07:41:51 -05:00
parent 365e343a95
commit 822b978034
5 changed files with 127 additions and 15 deletions

View File

@ -26,7 +26,7 @@ func init() {
rootCmd.AddCommand(getCmd) rootCmd.AddCommand(getCmd)
} }
func runGetCmd(cmd *cobra.Command, args []string) error { func runGetCmd(_ *cobra.Command, args []string) error {
var path []string var path []string
for i := range args { for i := range args {
path = append(path, strings.Split(args[i], "/")...) path = append(path, strings.Split(args[i], "/")...)
@ -45,7 +45,7 @@ func runGetCmd(cmd *cobra.Command, args []string) error {
var entryErr error var entryErr error
list := db.GetGroupsAndEntriesFromRoot(path) list := db.GetGroupsAndEntriesFromRoot(path)
if len(list) == 0 { if len(list) == 0 {
return errors.New("Invalid Path") return errors.New("invalid path")
} else if len(list) > 1 { } else if len(list) > 1 {
for i := range list { for i := range list {
fmt.Printf("%d. %s\n", i+1, list[i]) fmt.Printf("%d. %s\n", i+1, list[i])

View File

@ -4,7 +4,6 @@ Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
package cmd package cmd
import ( import (
"errors"
"fmt" "fmt"
"sort" "sort"
"strings" "strings"
@ -26,7 +25,7 @@ func init() {
rootCmd.AddCommand(rofiCmd) rootCmd.AddCommand(rofiCmd)
} }
func runRofi(cmd *cobra.Command, args []string) error { func runRofi(_ *cobra.Command, args []string) error {
var db *models.KeePassDB var db *models.KeePassDB
var argIsPw bool var argIsPw bool
@ -46,9 +45,9 @@ func runRofi(cmd *cobra.Command, args []string) error {
db, err = models.NewKeePassDB(viper.GetString("database"), pass) db, err = models.NewKeePassDB(viper.GetString("database"), pass)
if err != nil { if err != nil {
util.RemovePWFile(pwFile) util.RemovePWFile(pwFile)
return fmt.Errorf("Error opening DB. Re-enter Master Password:\n%v", err) return fmt.Errorf("error opening db. re-enter master password:\n%w", err)
} else if err = util.WritePWFile(pwFile, pass); err != nil { } else if err = util.WritePWFile(pwFile, pass); err != nil {
return fmt.Errorf("DB Opened, but failed to persist password: %v", err) return fmt.Errorf("db opened, but failed to persist password: %w", err)
} }
var path []string var path []string
@ -90,6 +89,7 @@ func runRofi(cmd *cobra.Command, args []string) error {
return nil return nil
} }
/*
func runRofiSteps(cmd *cobra.Command, args []string) error { func runRofiSteps(cmd *cobra.Command, args []string) error {
var path []string var path []string
for i := range args { for i := range args {
@ -122,7 +122,7 @@ func runRofiSteps(cmd *cobra.Command, args []string) error {
} }
list := db.GetGroupsAndEntriesFromRoot(path) list := db.GetGroupsAndEntriesFromRoot(path)
if len(list) == 0 { if len(list) == 0 {
return errors.New("Invalid Path") return errors.New("invalid path")
} else if len(list) > 1 { } else if len(list) > 1 {
for i := range list { for i := range list {
fmt.Println(strings.Join(list[i], "/")) fmt.Println(strings.Join(list[i], "/"))
@ -139,3 +139,4 @@ func runRofiSteps(cmd *cobra.Command, args []string) error {
return nil return nil
} }
*/

108
cmd/rofiUn.go Normal file
View File

@ -0,0 +1,108 @@
/*
Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd
import (
"fmt"
"sort"
"strings"
"git.bullercodeworks.com/brian/keepass-cli/models"
"git.bullercodeworks.com/brian/keepass-cli/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// rofiUnCmd represents the rofiUn command
var rofiUnCmd = &cobra.Command{
Use: "rofiUn",
Short: "Rofi Menufication of keepass-cli, returning a username",
RunE: runRofiUn,
}
func init() {
rootCmd.AddCommand(rofiUnCmd)
}
func runRofiUn(_ *cobra.Command, args []string) error {
var db *models.KeePassDB
var argIsPw bool
pwFile := fmt.Sprintf("%s%s", ConfigDir, "pw")
pass, err := util.ReadPWFile(pwFile)
if err != nil {
// See if we have a password...
if len(args) == 1 {
pass = args[0]
argIsPw = true
} else {
fmt.Println("Enter Master Password")
return nil
}
}
db, err = models.NewKeePassDB(viper.GetString("database"), pass)
if err != nil {
util.RemovePWFile(pwFile)
return fmt.Errorf("error opening db. re-enter master password:\n%w", err)
} else if err = util.WritePWFile(pwFile, pass); err != nil {
return fmt.Errorf("db opened, but failed to persist password: %w", err)
}
var path []string
for i := range args {
path = append(path, strings.Split(args[i], "/")...)
}
var freqs *models.Frequencies
if len(path) == 0 || argIsPw {
list := db.GetAllEntriesFromRoot()
var printList []string
for i := range list {
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])
}
} else {
if entry, entryErr := db.FindEntryFromRoot(path); entryErr != nil {
return entryErr
} else {
freqPath := strings.Join(path, "/")
freqs, err = models.LoadFrequencies()
if err == nil {
freqs.IncrementEntry(freqPath)
freqs.Save()
}
return util.WriteToClipboard(entry.GetContent("UserName"))
}
}
return nil
}

View File

@ -4,6 +4,8 @@ Copyright © 2024 Brian Buller <brian@bullercodeworks.com>
package cmd package cmd
import ( import (
"fmt"
"git.bullercodeworks.com/brian/keepass-cli/models" "git.bullercodeworks.com/brian/keepass-cli/models"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -19,8 +21,9 @@ func init() {
rootCmd.AddCommand(testCmd) rootCmd.AddCommand(testCmd)
} }
func runTestCmd(cmd *cobra.Command, args []string) error { func runTestCmd(_ *cobra.Command, args []string) error {
models.LoadFrequencies() models.LoadFrequencies()
fmt.Println(args)
/* /*
var db *models.KeePassDB var db *models.KeePassDB
pass, err := util.PromptUserForPassword("Master Password") pass, err := util.PromptUserForPassword("Master Password")

View File

@ -8,13 +8,12 @@ import (
"os" "os"
"strings" "strings"
"github.com/tobischo/gokeepasslib"
kp "github.com/tobischo/gokeepasslib" kp "github.com/tobischo/gokeepasslib"
) )
type KeePassDB struct { type KeePassDB struct {
path string path string
db *gokeepasslib.Database db *kp.Database
} }
func NewKeePassDB(filename string, auth string) (*KeePassDB, error) { func NewKeePassDB(filename string, auth string) (*KeePassDB, error) {
@ -44,6 +43,7 @@ func (db *KeePassDB) GetAllEntriesFromRoot() [][]string {
} }
return res return res
} }
func (db *KeePassDB) GetAllEntriesInGroup(group kp.Group) [][]string { func (db *KeePassDB) GetAllEntriesInGroup(group kp.Group) [][]string {
var res [][]string var res [][]string
for _, g := range group.Groups { for _, g := range group.Groups {
@ -138,7 +138,7 @@ func (db *KeePassDB) GetEntryPassword(path []string) (string, error) {
func (db *KeePassDB) FindEntryFromRoot(path []string) (*kp.Entry, error) { func (db *KeePassDB) FindEntryFromRoot(path []string) (*kp.Entry, error) {
if len(path) < 2 { if len(path) < 2 {
return nil, errors.New("Invalid Path") return nil, errors.New("invalid path")
} }
groups := db.db.Content.Root.Groups groups := db.db.Content.Root.Groups
for _, g := range groups { for _, g := range groups {
@ -146,12 +146,12 @@ func (db *KeePassDB) FindEntryFromRoot(path []string) (*kp.Entry, error) {
return db.FindEntryInGroup(g, path[1:]) return db.FindEntryInGroup(g, path[1:])
} }
} }
return nil, errors.New("Invalid Path") return nil, errors.New("invalid path")
} }
func (db *KeePassDB) FindEntryInGroup(group kp.Group, path []string) (*kp.Entry, error) { func (db *KeePassDB) FindEntryInGroup(group kp.Group, path []string) (*kp.Entry, error) {
if len(path) == 0 { if len(path) == 0 {
return nil, errors.New("Invalid Path") return nil, errors.New("invalid path")
} else if len(path) == 1 { } else if len(path) == 1 {
// Looking for an entry in this group // Looking for an entry in this group
for _, e := range group.Entries { for _, e := range group.Entries {
@ -159,12 +159,12 @@ func (db *KeePassDB) FindEntryInGroup(group kp.Group, path []string) (*kp.Entry,
return &e, nil return &e, nil
} }
} }
return nil, errors.New("Invalid Path") return nil, errors.New("invalid path")
} }
for _, g := range group.Groups { for _, g := range group.Groups {
if g.Name == path[0] { if g.Name == path[0] {
return db.FindEntryInGroup(g, path[1:]) return db.FindEntryInGroup(g, path[1:])
} }
} }
return nil, errors.New("Invalid Path") return nil, errors.New("invalid path")
} }