-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
181 lines (156 loc) · 4.88 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright 2018-2022 ObjectBox Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
"client-go/model"
"github.com/objectbox/objectbox-go/objectbox"
)
func main() {
objectBox := initObjectBox()
defer objectBox.Close()
box := model.BoxForTask(objectBox)
checkStartSyncClient(objectBox, box)
runInteractiveShell(box)
}
func runInteractiveShell(box *model.TaskBox) {
// our simple interactive shell
reader := bufio.NewReader(os.Stdin)
fmt.Println("Welcome to the ObjectBox tasks-list app example")
printHelp()
for {
fmt.Print("$ ")
input, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
//input = strings.TrimSuffix(input, "\n")
input = strings.TrimSpace(input)
args := strings.Fields(input)
switch strings.ToLower(args[0]) {
case "new":
createTask(box, strings.Join(args[1:], " "))
case "done":
if len(args) != 2 {
fmt.Fprintf(os.Stderr, "wrong number of arguments, expecting exactly one\n")
} else if id, err := strconv.ParseUint(args[1], 10, 64); err != nil {
fmt.Fprintf(os.Stderr, "could not parse ID: %s\n", err)
} else {
setDone(box, id)
}
case "ls":
if len(args) < 2 {
printList(box, false)
} else if args[1] == "-a" {
printList(box, true)
} else {
fmt.Fprintf(os.Stderr, "unknown argument %s\n", args[1])
fmt.Println()
}
case "exit":
return
case "help":
printHelp()
default:
fmt.Fprintf(os.Stderr, "unknown command %s\n", input)
printHelp()
}
}
}
func initObjectBox() *objectbox.ObjectBox {
objectBox, err := objectbox.NewBuilder().Model(model.ObjectBoxModel()).Build()
if err != nil {
panic(err)
}
return objectBox
}
func printHelp() {
fmt.Println("Available commands are: ")
fmt.Println(" ls [-a] list tasks - unfinished or all (-a flag)")
fmt.Println(" new Task text create a new task with the text 'Task text'")
fmt.Println(" done ID mark task with the given ID as done")
fmt.Println(" exit close the program")
fmt.Println(" help display this help")
}
func createTask(box *model.TaskBox, text string) {
task := &model.Task{
Text: text,
DateCreated: time.Now(),
DateFinished: time.Unix(0, 0), // use "epoch start" to unify values across platforms (e.g for Sync)
}
if id, err := box.Put(task); err != nil {
fmt.Fprintf(os.Stderr, "could not create task: %s\n", err)
} else {
task.Id = id
fmt.Printf("task ID %d successfully created\n", task.Id)
}
}
func printList(box *model.TaskBox, all bool) {
var list []*model.Task
var err error
if all { // load all tasks
list, err = box.GetAll()
} else { // load only unfinished tasks (value 0 is "epoch start")
list, err = box.Query(model.Task_.DateFinished.Equals(0)).Find()
}
if err != nil {
fmt.Fprintf(os.Stderr, "could not list tasks: %s\n", err)
}
fmt.Printf("%3s %-23s %-23s %s\n", "ID", "Created", "Finished", "Text")
for _, task := range list {
fmt.Printf("%3d %-23s %-23s %s\n",
task.Id, task.DateCreated.Format("2006-01-02 15:04:05"), task.DateFinished.Format("2006-01-02 15:04:05"), task.Text)
}
}
func setDone(box *model.TaskBox, id uint64) {
if task, err := box.Get(id); err != nil {
fmt.Fprintf(os.Stderr, "could not read task ID %d: %s\n", id, err)
} else if task == nil {
fmt.Fprintf(os.Stderr, "task ID %d doesn't exist\n", id)
} else {
task.DateFinished = time.Now()
if _, err := box.Put(task); err != nil {
fmt.Fprintf(os.Stderr, "could not update task ID %d: %s\n", id, err)
} else {
fmt.Printf("task ID %d completed at %s\n", id, task.DateFinished.String())
}
}
}
func checkStartSyncClient(ob *objectbox.ObjectBox, box *model.TaskBox) { // only if sync-enabled library is used
if objectbox.SyncIsAvailable() {
syncClient, err := objectbox.NewSyncClient(
ob,
"ws://127.0.0.1", // wss for SSL, ws for unencrypted traffic
objectbox.SyncCredentialsNone())
if err == nil {
syncClient.Start() // Connect and start syncing.
fmt.Println("Sync client started.")
syncClient.SetChangeListener(func(changes []*objectbox.SyncChange) {
fmt.Printf("received %d changes\n", len(changes))
printList(box, true)
})
} else {
fmt.Println("Could not start the sync client.")
}
} else {
fmt.Println("Sync is not available. Please go to https://sync.objectbox.io/ for more information.")
}
}