-
Notifications
You must be signed in to change notification settings - Fork 796
/
main.go
140 lines (114 loc) · 2.58 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
package main
import (
"fmt"
"github.com/jackc/pgx"
"os"
"strconv"
)
var conn *pgx.Conn
func main() {
var err error
conn, err = pgx.Connect(extractConfig())
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connection to database: %v\n", err)
os.Exit(1)
}
if len(os.Args) == 1 {
printHelp()
os.Exit(0)
}
switch os.Args[1] {
case "list":
err = listTasks()
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to list tasks: %v\n", err)
os.Exit(1)
}
case "add":
err = addTask(os.Args[2])
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to add task: %v\n", err)
os.Exit(1)
}
case "update":
n, err := strconv.ParseInt(os.Args[2], 10, 32)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable convert task_num into int32: %v\n", err)
os.Exit(1)
}
err = updateTask(int32(n), os.Args[3])
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to update task: %v\n", err)
os.Exit(1)
}
case "remove":
n, err := strconv.ParseInt(os.Args[2], 10, 32)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable convert task_num into int32: %v\n", err)
os.Exit(1)
}
err = removeTask(int32(n))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to remove task: %v\n", err)
os.Exit(1)
}
default:
fmt.Fprintln(os.Stderr, "Invalid command")
printHelp()
os.Exit(1)
}
}
func listTasks() error {
rows, _ := conn.Query("select * from tasks")
for rows.Next() {
var id int32
var description string
err := rows.Scan(&id, &description)
if err != nil {
return err
}
fmt.Printf("%d. %s\n", id, description)
}
return rows.Err()
}
func addTask(description string) error {
_, err := conn.Exec("insert into tasks(description) values($1)", description)
return err
}
func updateTask(itemNum int32, description string) error {
_, err := conn.Exec("update tasks set description=$1 where id=$2", description, itemNum)
return err
}
func removeTask(itemNum int32) error {
_, err := conn.Exec("delete from tasks where id=$1", itemNum)
return err
}
func printHelp() {
fmt.Print(`Todo pgx demo
Usage:
todo list
todo add task
todo update task_num item
todo remove task_num
Example:
todo add 'Learn Go'
todo list
`)
}
func extractConfig() pgx.ConnConfig {
var config pgx.ConnConfig
config.Host = os.Getenv("TODO_DB_HOST")
if config.Host == "" {
config.Host = "localhost"
}
config.User = os.Getenv("TODO_DB_USER")
if config.User == "" {
config.User = os.Getenv("USER")
}
config.Password = os.Getenv("TODO_DB_PASSWORD")
config.Database = os.Getenv("TODO_DB_DATABASE")
if config.Database == "" {
config.Database = "todo"
}
return config
}