-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
55 lines (47 loc) · 1.36 KB
/
main.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
package main
import (
"flag"
"log"
"os"
"path/filepath"
"strconv"
"github.com/fatih/color"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Todos struct {
gorm.Model
Name string
Status bool
}
func main() {
// Initializing the db in home dir
dirname, _ := os.UserHomeDir()
p := filepath.Join(dirname, "main.db")
db, err := gorm.Open(sqlite.Open(p), &gorm.Config{})
if err != nil {
log.Panic("You got a err")
}
setup(db)
// Flags management
useListTodo := flag.Bool("ls", false, "List all todos")
useDeleteAllTodo := flag.Bool("da", false, "Deletes all todos")
useDeleteSingle := flag.Bool("del", false, "Deletes single todos with provided id of todo")
useAddTodo := flag.Bool("add", false, "Adds a todo with defined string")
useUpdateTodo := flag.Bool("ud", false, "Updates a todo with given id and status [ remember status should be boolean ]")
flag.Parse()
if *useListTodo && flag.Arg(0) == "" {
listTodo(db)
} else if *useDeleteAllTodo && flag.Arg(0) == "" {
deleteAll(db)
} else if *useAddTodo && flag.Arg(0) != "" {
newTodo(db, &Todos{Name: flag.Arg(0), Status: false})
} else if *useDeleteSingle && flag.Arg(0) != "" {
deleteTodo(flag.Arg(0), db)
} else if *useUpdateTodo && flag.Arg(1) != "" {
status, _ := strconv.ParseBool(flag.Arg(1))
updateTodo(status, flag.Arg(0), db)
} else {
color.Red("Wrong value see --help for usage")
}
}