Skip to content

Instantly share code, notes, and snippets.

@ToJen
Last active May 17, 2019 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ToJen/f6a4408ca47cb47cbf314e5eba96a82e to your computer and use it in GitHub Desktop.
Save ToJen/f6a4408ca47cb47cbf314e5eba96a82e to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io"
"os"
"time"
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
// "github.com/flimzy/kivik"
"github.com/go-kivik/kivik"
)
var dockerClient *client.Client
var dockerContext context.Context
var containerID string
func StartCouchDB() (string, error) {
dockerContext = context.TODO()
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}
dockerClient = cli
containerName := "couchdb-dev"
imageName := "docker.io/library/couchdb"
imageTag := "2.3.1"
imageRef := imageName + ":" + imageTag
out, err := dockerClient.ImagePull(dockerContext, imageRef, types.ImagePullOptions{})
if err != nil {
panic(err)
}
io.Copy(os.Stdout, out)
hostConfig := &container.HostConfig{
PortBindings: nat.PortMap{
"5984/tcp": {
nat.PortBinding{
HostIP: "127.0.0.1",
HostPort: "5984",
},
},
},
}
resp, err := dockerClient.ContainerCreate(dockerContext, &container.Config{
Image: imageRef,
}, hostConfig, nil, containerName)
if err != nil {
return "", err
}
containerID = resp.ID
if err := dockerClient.ContainerStart(dockerContext, containerID, types.ContainerStartOptions{}); err != nil {
return "", err
}
fmt.Printf("container name: %s\ncontainer id: %s", containerName, containerID)
return containerID, nil
}
func StopCouchDB(cid string) error {
if err := dockerClient.ContainerStop(dockerContext, cid, nil); err != nil {
return err
}
fmt.Println("Success stopped container")
return nil
}
func RemoveCouchDB(cid string) error {
if err := dockerClient.ContainerRemove(dockerContext, cid, types.ContainerRemoveOptions{
Force: true,
}); err != nil {
return err
}
fmt.Println("Successfully ")
return nil
}
func CreateDatabase(url, dbName string) (*kivik.DB, error) {
client, err := kivik.New(context.TODO(), "couch", url)
err = client.CreateDB(context.TODO(), dbName, nil)
if err != nil {
return nil, err
}
db, err := client.DB(context.TODO(), dbName)
if err != nil {
return nil, err
}
return db, nil
}
func main() {
id, err := StartCouchDB()
if err != nil {
panic(err)
}
fmt.Println(id)
time.Sleep(10 * time.Second)
dbName := "org"
db, err := CreateDatabase("http://localhost:5984", dbName)
if err != nil {
panic(err)
}
fmt.Printf("expected: %s\tactual: %s\n", dbName, db.Name())
doc := map[string]string{"foo": "bar"}
rev, err := db.Put(dockerContext, "testKey", doc, nil)
if err != nil {
panic(err)
}
fmt.Printf("rev: %s\n", rev)
if err := StopCouchDB(id); err != nil {
panic(err)
}
if err := RemoveCouchDB(id); err != nil {
panic(err)
}
}
/*
run with go run main.go
output:
---
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x1290ecc]
goroutine 1 [running]:
github.com/go-kivik/kivik.(*Client).CreateDB(0x0, 0x139fb60, 0xc0000b4010, 0x13348a1, 0x3, 0xc0001efe80, 0x1, 0x1, 0xc000398080, 0xc0003163c0)
~/go/pkg/mod/github.com/go-kivik/kivik@v1.8.1/kivik.go:129 +0x7c
main.CreateDatabase(0x133929b, 0x15, 0x13348a1, 0x3, 0x1, 0x41, 0x0)
~/go/src/github.com/tojen/couchdb-docker-go/main.go:91 +0xde
main.main()
~/go/src/github.com/tojen/couchdb-docker-go/main.go:114 +0x114
exit status 2
---
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment