52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"axolotl/db"
|
|
"axolotl/output"
|
|
"axolotl/service"
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var dForce bool
|
|
var delCmd = &cobra.Command{
|
|
Use: "del <id>", Short: "Delete a node", Args: cobra.ExactArgs(1),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
d, err := db.GetDB()
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
return
|
|
}
|
|
svc := service.NewSQLiteNodeService(d.DB, cfg.GetUser())
|
|
n, err := svc.GetByID(args[0])
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, " node not found:", args[0])
|
|
return
|
|
}
|
|
|
|
if !dForce {
|
|
fmt.Printf("Delete %s '%s'? [y/N]: ", n.GetProperty("type"), n.Title)
|
|
r, _ := bufio.NewReader(os.Stdin).ReadString('\n')
|
|
if r = strings.TrimSpace(strings.ToLower(r)); r != "y" && r != "yes" {
|
|
fmt.Println("Cancelled.")
|
|
return
|
|
}
|
|
}
|
|
|
|
if err := svc.Delete(args[0]); err != nil {
|
|
fmt.Fprintln(os.Stderr, "failed to delete: ", err)
|
|
} else {
|
|
output.PrintAction(cmd.OutOrStdout(), "Deleted", args[0], true)
|
|
}
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(delCmd)
|
|
delCmd.Flags().BoolVarP(&dForce, "force", "f", false, "")
|
|
}
|