feat: rename create and delete commands to add and del
This commit is contained in:
51
cmd/del.go
Normal file
51
cmd/del.go
Normal file
@@ -0,0 +1,51 @@
|
||||
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, "")
|
||||
}
|
||||
Reference in New Issue
Block a user