From 822b97803487083e2cbf336442e4a3eb9e76d2ba Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Wed, 9 Oct 2024 07:41:51 -0500 Subject: [PATCH] Add Username Function --- cmd/get.go | 4 +- cmd/rofi.go | 11 ++--- cmd/rofiUn.go | 108 +++++++++++++++++++++++++++++++++++++++++++++ cmd/test.go | 5 ++- models/database.go | 14 +++--- 5 files changed, 127 insertions(+), 15 deletions(-) create mode 100644 cmd/rofiUn.go diff --git a/cmd/get.go b/cmd/get.go index 00c83d4..4138fbf 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -26,7 +26,7 @@ func init() { rootCmd.AddCommand(getCmd) } -func runGetCmd(cmd *cobra.Command, args []string) error { +func runGetCmd(_ *cobra.Command, args []string) error { var path []string for i := range args { path = append(path, strings.Split(args[i], "/")...) @@ -45,7 +45,7 @@ func runGetCmd(cmd *cobra.Command, args []string) error { var entryErr error list := db.GetGroupsAndEntriesFromRoot(path) if len(list) == 0 { - return errors.New("Invalid Path") + return errors.New("invalid path") } else if len(list) > 1 { for i := range list { fmt.Printf("%d. %s\n", i+1, list[i]) diff --git a/cmd/rofi.go b/cmd/rofi.go index 436954e..071f7f5 100644 --- a/cmd/rofi.go +++ b/cmd/rofi.go @@ -4,7 +4,6 @@ Copyright © 2024 Brian Buller package cmd import ( - "errors" "fmt" "sort" "strings" @@ -26,7 +25,7 @@ func init() { rootCmd.AddCommand(rofiCmd) } -func runRofi(cmd *cobra.Command, args []string) error { +func runRofi(_ *cobra.Command, args []string) error { var db *models.KeePassDB var argIsPw bool @@ -46,9 +45,9 @@ func runRofi(cmd *cobra.Command, args []string) error { 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%v", err) + 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: %v", err) + return fmt.Errorf("db opened, but failed to persist password: %w", err) } var path []string @@ -90,6 +89,7 @@ func runRofi(cmd *cobra.Command, args []string) error { return nil } +/* func runRofiSteps(cmd *cobra.Command, args []string) error { var path []string for i := range args { @@ -122,7 +122,7 @@ func runRofiSteps(cmd *cobra.Command, args []string) error { } list := db.GetGroupsAndEntriesFromRoot(path) if len(list) == 0 { - return errors.New("Invalid Path") + return errors.New("invalid path") } else if len(list) > 1 { for i := range list { fmt.Println(strings.Join(list[i], "/")) @@ -139,3 +139,4 @@ func runRofiSteps(cmd *cobra.Command, args []string) error { return nil } +*/ diff --git a/cmd/rofiUn.go b/cmd/rofiUn.go new file mode 100644 index 0000000..4d53ee1 --- /dev/null +++ b/cmd/rofiUn.go @@ -0,0 +1,108 @@ +/* +Copyright © 2024 Brian Buller + +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 +} diff --git a/cmd/test.go b/cmd/test.go index b916ac8..8f79a5b 100644 --- a/cmd/test.go +++ b/cmd/test.go @@ -4,6 +4,8 @@ Copyright © 2024 Brian Buller package cmd import ( + "fmt" + "git.bullercodeworks.com/brian/keepass-cli/models" "github.com/spf13/cobra" ) @@ -19,8 +21,9 @@ func init() { rootCmd.AddCommand(testCmd) } -func runTestCmd(cmd *cobra.Command, args []string) error { +func runTestCmd(_ *cobra.Command, args []string) error { models.LoadFrequencies() + fmt.Println(args) /* var db *models.KeePassDB pass, err := util.PromptUserForPassword("Master Password") diff --git a/models/database.go b/models/database.go index f765686..485ba07 100644 --- a/models/database.go +++ b/models/database.go @@ -8,13 +8,12 @@ import ( "os" "strings" - "github.com/tobischo/gokeepasslib" kp "github.com/tobischo/gokeepasslib" ) type KeePassDB struct { path string - db *gokeepasslib.Database + db *kp.Database } func NewKeePassDB(filename string, auth string) (*KeePassDB, error) { @@ -44,6 +43,7 @@ func (db *KeePassDB) GetAllEntriesFromRoot() [][]string { } return res } + func (db *KeePassDB) GetAllEntriesInGroup(group kp.Group) [][]string { var res [][]string 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) { if len(path) < 2 { - return nil, errors.New("Invalid Path") + return nil, errors.New("invalid path") } groups := db.db.Content.Root.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 nil, errors.New("Invalid Path") + return nil, errors.New("invalid path") } func (db *KeePassDB) FindEntryInGroup(group kp.Group, path []string) (*kp.Entry, error) { if len(path) == 0 { - return nil, errors.New("Invalid Path") + return nil, errors.New("invalid path") } else if len(path) == 1 { // Looking for an entry in this group for _, e := range group.Entries { @@ -159,12 +159,12 @@ func (db *KeePassDB) FindEntryInGroup(group kp.Group, path []string) (*kp.Entry, return &e, nil } } - return nil, errors.New("Invalid Path") + return nil, errors.New("invalid path") } for _, g := range group.Groups { if g.Name == path[0] { return db.FindEntryInGroup(g, path[1:]) } } - return nil, errors.New("Invalid Path") + return nil, errors.New("invalid path") }