-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
146 lines (114 loc) · 4.21 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// https://github.com/mattn/go-sqlite3/issues/274
// https://github.com/mattn/go-sqlite3/issues/569
package main
import (
"database/sql"
"fmt"
"strconv"
"strings"
"bufio"
"os"
"flag"
_ "github.com/mattn/go-sqlite3"
"steadylearner.com/sqlite/models"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"steadylearner.com/sqlite/repo"
)
var (
action string
)
func important(err error, reason string) {
if err != nil {
log.Fatal(errors.Wrap(err, reason))
return
}
}
func init() {
actionHelp := "Use [list, get, create, update, delete] to handle the data from users."
flag.StringVar(&action, "action", "list", actionHelp)
flag.Parse()
}
func main() {
target := "database/users.db"
db, err := sql.Open("sqlite3", target)
important(err, fmt.Sprintf("Couldn't open %s", target))
defer db.Close()
// https://www.sqlitetutorial.net/sqlite-autoincrement/
db.Exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE)")
// db.Exec("DROP TABLE users")
// db.Exec("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL UNIQUE)")
userRepo := repo.NewUsersRepository(db)
switch action {
case "list":
list, err := userRepo.List()
important(err, "fail to read user list")
for _, u := range list {
fmt.Printf("User [%d]: %s\n", u.ID, u.Name)
}
case "get":
idReader := bufio.NewReader(os.Stdin)
fmt.Println("Type the id of a user get the data.")
id, err := idReader.ReadString('\n')
important(err, "Fail to read the id to get the data of a user.") // Substitute it with logrus
if id == "\n" {
fmt.Println("You should provide an id to make this work.")
return
}
i, err := strconv.Atoi(strings.TrimSuffix(id, "\n"))
important(err, fmt.Sprintf("Fail to convert given id(%s) to int.", id)) // Substitute it with logrus
user, err := userRepo.Get(int64(i))
important(err, fmt.Sprintf("Fail to feetch user by ID (%s) to int.", id)) // Substitute it with logrus
fmt.Printf("%+v\n", user)
case "create":
usernameReader := bufio.NewReader(os.Stdin)
fmt.Println("Type a username you want to use.")
username, err := usernameReader.ReadString('\n')
important(err, "Fail to read a username.") // Substitute it with logrus
if username == "\n" {
fmt.Println("You should provide username to make it work.")
return
}
newUser := models.User{Name: strings.TrimSpace(username)}
err = userRepo.Create(&newUser)
important(err, fmt.Sprintf("Fail to create user(%s).", username)) // Substitute it with logrus
fmt.Printf("User %d created.\n", newUser.ID)
case "update":
idReader := bufio.NewReader(os.Stdin)
fmt.Println("Type the id of a user to update its username.")
id, err := idReader.ReadString('\n')
important(err, "Fail to read the id of a user to be updated.") // Substitute it with logrus
if id == "\n" {
fmt.Println("You should provide an id to make this work.")
return
}
newUsernameReader := bufio.NewReader(os.Stdin)
fmt.Println("Type a new username you want to use.")
newUsername, err := newUsernameReader.ReadString('\n')
important(err, "Fail to read a new username.") // Substitute it with logrus
if newUsername == "\n" {
fmt.Println("You should provide username to make it work.")
return
}
i, err := strconv.Atoi(strings.TrimSuffix(id, "\n"))
important(err, fmt.Sprintf("Fail to convert given id(%s) to int.", id)) // Substitute it with logrus
upd := models.User{ID: int64(i), Name: strings.TrimSpace(newUsername)}
err = userRepo.Update(&upd)
important(err, "Fail to update user.") // Substitute it with logrus
case "delete":
idReader := bufio.NewReader(os.Stdin)
fmt.Println("Type the id of a user to delete.")
id, err := idReader.ReadString('\n')
important(err, "Fail to read the id to delete the data of a user.") // Substitute it with logrus
if id == "\n" {
fmt.Println("You should provide the id to make it work.")
return
}
i, err := strconv.Atoi(strings.TrimSuffix(id, "\n"))
important(err, fmt.Sprintf("Fail to convert given id(%s) to int.", id)) // Substitute it with logrus
err = userRepo.Delete(int64(i))
important(err, fmt.Sprintf("Fail to delete user (%d)", i))
default:
fmt.Println("You should use [list, get, create, update, delete].")
}
}