Skip to content

Commit

Permalink
Print oplogs
Browse files Browse the repository at this point in the history
  • Loading branch information
simagix committed Oct 10, 2018
1 parent 2f13c4c commit 76bed5b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
31 changes: 31 additions & 0 deletions argos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/mongodb/mongo-go-driver/core/connstring"
argos "github.com/simagix/argos/core"
)

// example: argos "mongodb://localhost:27017/prudential?replicaSet=replset" students '[{"$match": {"operationType": "update"}}]'
func main() {
flag.Parse()
if len(flag.Args()) < 2 {
fmt.Println("usage: argos uri collection")
os.Exit(1)
}

connStr, err := connstring.Parse(flag.Arg(0))
if err != nil {
panic(err)
}

var pipeline string
if len(flag.Args()) >= 3 {
pipeline = flag.Arg(2)
fmt.Println("pipeline:", pipeline)
}
argos.PrintOpLogs(connStr, flag.Arg(1), pipeline)
}
9 changes: 9 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#! /bin/bash
# Copyright 2018 Kuei-chun Chen. All rights reserved.

export version="master-$(date "+%Y%m%d.%s")"
mkdir -p build
env GOOS=linux GOARCH=amd64 go build -ldflags "-X main.version=$version" -o build/argos-linux-x64 argos.go
env GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.version=$version" -o build/argos-osx-x64 argos.go
env GOOS=windows GOARCH=amd64 go build -ldflags "-X main.version=$version" -o build/argos-win-x64.exe argos.go

4 changes: 4 additions & 0 deletions core/bson.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package argos

// M is a convenient alias for a map[string]interface{} map, useful for dealing with BSON in a native way. For instance:
type M map[string]interface{}
53 changes: 53 additions & 0 deletions core/changeStreams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package argos

import (
"context"
"encoding/json"
"fmt"
"log"

"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/core/connstring"
"github.com/mongodb/mongo-go-driver/mongo"
)

// PrintOpLogs prints oplogs in JSON format
func PrintOpLogs(connStr connstring.ConnString, collname string, pipelineStr string) {
client, err := mongo.Connect(context.Background(), connStr.String(), nil)
if err != nil {
panic(err)
}

fmt.Println("Watching", connStr.Database+"."+collname)
db := client.Database(connStr.Database)
coll := db.Collection(collname)
ctx := context.Background()
var pipeline interface{}
if pipelineStr != "" {
pipeline, _ = bson.ParseExtJSONArray(pipelineStr)
}
fmt.Println("pipeline", pipeline)
cur, err := coll.Watch(ctx, pipeline)
if err != nil {
panic(err)
}
defer cur.Close(ctx)

for cur.Next(ctx) {
elem := bson.NewDocument()

if err := cur.Decode(elem); err != nil {
log.Fatal(err)
}

bytes := []byte(elem.ToExtJSON(true))
var v = M{}
json.Unmarshal(bytes, &v)
bytes, _ = json.MarshalIndent(v, "", " ")
fmt.Println(string(bytes))
}

if err := cur.Err(); err != nil {
log.Fatal(err)
}
}

0 comments on commit 76bed5b

Please sign in to comment.