Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #42 from CaioDallaqua/patch-1
Browse files Browse the repository at this point in the history
Adding flush command
  • Loading branch information
shadowspore authored Jan 13, 2022
2 parents a4c7dc6 + a700318 commit 4e11326
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 4 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# Tile38 Client
# Tile38 Client for Go
[![Go](https://github.com/xjem/t38c/workflows/Go/badge.svg)](https://github.com/xjem/t38c/actions)
[![Documentation](https://pkg.go.dev/badge/github.com/xjem/t38c)](https://pkg.go.dev/github.com/xjem/t38c?tab=doc)
[![Go Report Card](https://goreportcard.com/badge/github.com/xjem/t38c)](https://goreportcard.com/report/github.com/xjem/t38c)
[![codecov](https://codecov.io/gh/xjem/t38c/branch/master/graph/badge.svg)](https://codecov.io/gh/xjem/t38c)
[![license](https://img.shields.io/github/license/xjem/t38c.svg)](https://github.com/xjem/t38c/blob/master/LICENSE)

Supported features: [click](TODO.md)
See what [Tile38](https://tile38.com/) is all about.

- [Supported features](TODO.md)
- [Examples](examples)

### Installation

```
go get github.com/xjem/t38c
Expand Down Expand Up @@ -54,4 +59,3 @@ func main() {
fmt.Println(response.Points[0].ID, response.Points[0].Point)
}
```
More examples: [click](examples)
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@

# Group 'server'
- [ ] CONFIG GET
- [ ] FLUSHDB
- [x] FLUSHDB
- [ ] SERVER
- [ ] READONLY
- [ ] CONFIG REWRITE
Expand Down
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Client struct {
Channels *Channels
Scripting *Scripting
Geofence *Geofence
Server *Server
}

type clientParams struct {
Expand Down Expand Up @@ -91,6 +92,7 @@ func NewWithExecutor(exec Executor, debug bool) (*Client, error) {
client.Search = &Search{client}
client.Scripting = &Scripting{client}
client.Channels = &Channels{client}
client.Server = &Server{client}

return client, nil
}
Expand Down
61 changes: 61 additions & 0 deletions examples/flush_db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// +build ignore

package main

import (
"log"

"github.com/xjem/t38c"
)

/*
* FLUSHDB Example
*
* Shows how to erase all data in Tile38
* database using the FLUSHDB command.
*
*/

func main() {
// Variables to be used along the way.
var (
err error
tile38 *t38c.Client
)

// Create a Tile38 client.
tile38, err = t38c.New("localhost:9851", t38c.Debug)
if err != nil {
log.Fatal(err)
}
defer tile38.Close()

// Add a point named 'truck1' to a collection named 'first fleet'.
if err = tile38.Keys.Set("first fleet", "truck1").Point(33.5123, -112.2693).Do(); err != nil {
log.Fatal(err)
}

// Add a point named 'truck2' to a collection named 'second fleet'.
if err = tile38.Keys.Set("second fleet", "truck2").Point(23.6951, -92.3581).Do(); err != nil {
log.Fatal(err)
}

// Get all keys.
// Returns ["first fleet","second fleet"].
_, err = tile38.Keys.Keys("*")
if err != nil {
log.Fatal(err)
}

// Flush ALL data in Tile38 database.
if err = tile38.Server.FlushDB(); err != nil {
log.Fatal(err)
}

// Get all keys again.
// Returns [].
_, err = tile38.Keys.Keys("*")
if err != nil {
log.Fatal(err)
}
}
19 changes: 19 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package t38c

type Server struct {
client tile38Client
}

// WARNING: This erases all data in Tile38 DB!
func (sv *Server) FlushDB() error {
var resp struct{}

err := sv.client.jExecute(&resp, "FLUSHDB")

// Explicit is better than implicit.
if err != nil {
return err
}

return nil
}

0 comments on commit 4e11326

Please sign in to comment.