-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteMovieWatch.go
60 lines (50 loc) · 1.24 KB
/
deleteMovieWatch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"context"
"database/sql"
"fmt"
"log"
"strings"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/timothyrenner/movies-app/database"
)
var deleteMovieWatchCmd = &cobra.Command{
Use: "delete-movie-watch",
Short: "Deletes a movie watch.",
Run: deleteMovieWatch,
Args: cobra.ExactArgs(1),
}
func init() {
rootCmd.AddCommand(deleteMovieWatchCmd)
}
func deleteMovieWatch(cmd *cobra.Command, args []string) {
db, err := sql.Open("sqlite3", DB)
if err != nil {
log.Panicf("Error opening db: %v", err)
}
defer db.Close()
queries := database.New(db)
ctx := context.Background()
movieWatchUuid := args[0]
validate := func(input string) error {
decasedInput := strings.ToLower(input)
if (decasedInput != "y") && (decasedInput != "n") {
return fmt.Errorf("choose 'y', or 'n', not %v", input)
}
return nil
}
prompt := promptui.Prompt{
Label: fmt.Sprintf("Really delete movie watch %v ?", movieWatchUuid),
Validate: validate,
}
if _,err := prompt.Run(); err != nil {
log.Panicf("Aborting delete: %v", err)
}
if err := queries.DeleteMovieWatch(ctx, movieWatchUuid); err != nil {
log.Panicf("Error deleting movie watch: %v", err)
}
}