/* 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" ) // rofiNotesCmd represents the rofiNotes command var rofiNotesCmd = &cobra.Command{ Use: "rofiNotes", Short: "Rofi Menufication of keepass-cli, outputting the notes", RunE: runRofiNotes, } func init() { rootCmd.AddCommand(rofiNotesCmd) } func runRofiNotes(_ *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() } for _, v := range entry.Values { fmt.Println(v.Key, v.Value.Content) } return util.WriteToClipboard(entry.GetContent("UserName")) } } return nil }