|
| 1 | +/* |
| 2 | +Copyright © 2024 Jacob Peyron <[email protected]> |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in |
| 12 | +all copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +THE SOFTWARE. |
| 21 | +*/ |
| 22 | +package cmd |
| 23 | + |
| 24 | +import ( |
| 25 | + "fmt" |
| 26 | + "os" |
| 27 | + |
| 28 | + "github.com/spf13/cobra" |
| 29 | +) |
| 30 | + |
| 31 | +func noteClean(cmd *cobra.Command, args []string) { |
| 32 | + db := dbOpen() |
| 33 | + defer db.Close() |
| 34 | + |
| 35 | + // allInSpaceArg is set and no args provided means find all notes in specific space |
| 36 | + allNotesInSpace, err := db.SelectNotes([]string{TrashSpace}, true, nil, nil) |
| 37 | + if err != nil { |
| 38 | + quitError("db list", err) |
| 39 | + } |
| 40 | + |
| 41 | + ids := allNotesInSpace.GetIDs() |
| 42 | + uniqueIds := removeDuplicates(ids) |
| 43 | + if len(uniqueIds) == 0 { |
| 44 | + fmt.Println("Trash is empty") |
| 45 | + os.Exit(0) |
| 46 | + } |
| 47 | + |
| 48 | + if !noConfirmArg { |
| 49 | + fmt.Printf("WARNING: You are about to permanently remove %v note(s).\n", len(uniqueIds)) |
| 50 | + fmt.Printf("Write 'yes' to confirm permanent delete: ") |
| 51 | + response := readUserInput() |
| 52 | + if response != "yes" { |
| 53 | + os.Exit(2) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if err := db.PermanentRemoveNotes(uniqueIds); err != nil { |
| 58 | + quitError("db remove", err) |
| 59 | + } |
| 60 | + |
| 61 | + count := len(uniqueIds) |
| 62 | + if count == 1 { |
| 63 | + fmt.Printf("Note removed from %v\n", TrashSpace) |
| 64 | + } else { |
| 65 | + fmt.Printf("%v notes removed from %v\n", count, TrashSpace) |
| 66 | + } |
| 67 | +} |
0 commit comments