Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
openwhisk debugging
package main
import (
_ "" // The CouchDB driver
// This writes/updates a doc in CouchDB. If the doc doesn't exist, it will write it. If it does exist,
// it will "clobber" it and create a new revision based on the updated content.
// TODO: ------------- Write doc to stdout
// TODO: Currently when I invoke an action in OpenWhisk, it emits
// TODO: "error": "The action did not return a dictionary."
// TODO: This happens becuse this isn't writing the doc to stdout. It should actually GET the doc based on ID
// TODO: so that the doc written to stdout has a _rev parameter.
type Params struct {
Username string
Password string
Host string
DbName string
Doc map[string]interface{}
DocId string
func main() {
ctx := context.TODO()
params := discoverParams()
dataSourceName := fmt.Sprintf(
client, err := kivik.New(ctx, "couch", dataSourceName)
if err != nil {
db, err := client.DB(ctx, params.DbName)
if err != nil {
// Cas loop where we get the latest rev of the doc
for {
fetchedDocRow, err := db.Get(ctx, params.DocId)
if err != nil {
// Assume this will be the first rev of the doc, so do nothing in this case
} else {
// Got an existing doc, update the doc being inserted to be based on this rev
// we had a valid previous rev
fetchedDoc := map[string]interface{}{}
scanDocErr := fetchedDocRow.ScanDoc(&fetchedDoc)
if scanDocErr != nil {
panic(fmt.Sprintf("Could not read doc into map"))
revRaw, ok := fetchedDoc["_rev"]
if !ok {
panic(fmt.Sprintf("Doc does not have _rev field"))
rev := revRaw.(string)
params.Doc["_rev"] = rev
log.Printf("Going to put doc: %+v", params.Doc)
rev, err := db.Put(context.TODO(), params.DocId, params.Doc)
if err != nil {
// TODO: this should write to stderr rather than stdout
log.Printf("Error putting doc, assuming to be 409 cas error. Actual err: %v", err)
fmt.Printf("%s inserted with revision %s\n", params.DocId, rev)
// TODO: do a get on the doc and write it to stdout
// remove all other debugging
func discoverParams() (params Params) {
// native actions receive one argument, the JSON object as a string
arg := os.Args[1]
// unmarshal the string to a JSON object
json.Unmarshal([]byte(arg), &params)
return params
FROM openwhisk/dockerskeleton
COPY exec /action/exec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.