Skip to content

Commit c586f88

Browse files
committed
clean (.trash) command implemented
1 parent dd9ae19 commit c586f88

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

cmd/clean.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}

cmd/root.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ To remove notes permanently you need to specify the '--permanent' flag. It is
151151
possible to remove all notes in a space, by specifying the '--all-in-space'
152152
argument, followed by the space you want to empty.`,
153153
}
154+
cleanCmd = &cobra.Command{
155+
Use: "clean",
156+
Short: "Empty the .trash space",
157+
Run: noteClean,
158+
}
154159
getCmd = &cobra.Command{
155160
Use: "get id [id...]",
156161
Short: "Get specific note(s)",
@@ -408,6 +413,9 @@ func init() {
408413
removeFlags.BoolVar(&noConfirmArg, "no-confirm", false, "skip confirmation dialog")
409414
removeFlags.BoolVar(&permanentArg, "permanent", false, "note is completely removed from the db")
410415

416+
cleanFlags := cleanCmd.Flags()
417+
cleanFlags.BoolVar(&noConfirmArg, "no-confirm", false, "skip confirmation dialog")
418+
411419
printFlagSet := pflag.NewFlagSet("print", pflag.ExitOnError)
412420
_ = printFlagSet.String("style", string(LightStyle), "output style (raw, light, full)")
413421
_ = printFlagSet.String("color", "auto", "color option (auto, no|never, yes|always)")
@@ -470,8 +478,9 @@ func init() {
470478
rootCmd.AddCommand(
471479
initCmd,
472480
versionCmd,
473-
addCmd, removeCmd, getCmd, findCmd,
474-
listCmd, tableCmd, idCmd, spaceCmd,
481+
addCmd, removeCmd, cleanCmd,
482+
getCmd, findCmd, listCmd,
483+
tableCmd, idCmd, spaceCmd,
475484
editCmd, pinCmd, unpinCmd, moveCmd,
476485
importCmd, exportCmd,
477486
)

0 commit comments

Comments
 (0)