shiori/cmd/delete.go

53 lines
1.3 KiB
Go
Raw Normal View History

2018-01-29 08:40:29 +08:00
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"os"
)
var (
deleteCmd = &cobra.Command{
Use: "delete [indices]",
Short: "Delete the saved bookmarks.",
Long: "Delete bookmarks. " +
2018-01-30 14:31:31 +08:00
"When a record is deleted, the last record is moved to the removed index. " +
2018-01-30 14:32:26 +08:00
"Accepts space-separated list of indices (e.g. 5 6 23 4 110 45), hyphenated range (e.g. 100-200) or both (e.g. 1-3 7 9). " +
"If no arguments, all records will be deleted.",
2018-01-29 08:40:29 +08:00
Run: func(cmd *cobra.Command, args []string) {
2018-01-30 14:31:31 +08:00
// Read flags
skipConfirmation, _ := cmd.Flags().GetBool("yes")
2018-01-29 08:40:29 +08:00
// If no arguments, confirm to user
2018-01-30 14:31:31 +08:00
if len(args) == 0 && !skipConfirmation {
2018-01-29 08:40:29 +08:00
confirmDelete := ""
fmt.Print("Remove ALL bookmarks? (y/n): ")
fmt.Scanln(&confirmDelete)
if confirmDelete != "y" {
fmt.Println("No bookmarks deleted")
return
}
}
// Delete bookmarks from database
oldIndices, newIndices, err := DB.DeleteBookmarks(args...)
if err != nil {
cError.Println(err)
os.Exit(1)
}
fmt.Println("Bookmarks has been deleted")
2018-01-30 14:31:31 +08:00
for i, oldIndex := range oldIndices {
newIndex := newIndices[i]
fmt.Printf("Index %d moved to %d\n", oldIndex, newIndex)
2018-01-29 08:40:29 +08:00
}
},
}
)
func init() {
2018-01-30 14:31:31 +08:00
deleteCmd.Flags().BoolP("yes", "y", false, "Skip confirmation prompt and delete ALL bookmarks")
2018-01-29 08:40:29 +08:00
rootCmd.AddCommand(deleteCmd)
}