Skip to content

Commit

Permalink
docopt parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Traun Leyden committed Apr 29, 2015
1 parent 256e81b commit aba5d61
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cli/elastic-thought/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
package main

import (
"fmt"

"github.com/couchbaselabs/logg"
"github.com/docopt/docopt-go"
"github.com/gin-gonic/gin"
et "github.com/tleyden/elastic-thought"
)
Expand All @@ -13,8 +16,25 @@ func init() {

func main() {

usage := `ElasticThought REST API server.
Usage:
elastic-thought [--sync-gw-url=<sgu>] [--blob-store-url=<bsu>]
Options:
-h --help Show this screen.
--sync-gw-url=<sgu> Sync Gateway DB URL [default: http://localhost:4985/elastic-thought].
--blob-store-url=<bsu> Blob store URL [default: file:///tmp].`

parsedDocOptArgs, _ := docopt.Parse(usage, nil, true, "ElasticThought alpha", false)
fmt.Println(parsedDocOptArgs)

config := *(et.NewDefaultConfiguration()) // TODO: get these vals from cmd line args

// parse command line args into struct

// config = config.Merge(parsedDocOptArgs)

if err := et.EnvironmentSanityCheck(config); err != nil {
logg.LogFatal("Failed environment sanity check: %v", err)
return
Expand Down
23 changes: 23 additions & 0 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,26 @@ func (c Configuration) DbConnection() couch.Database {
func (c Configuration) NewBlobStoreClient() (BlobStore, error) {
return NewBlobStore(c.CbfsUrl)
}

// Add values from parsedDocOpts into Configuration and return a new instance
// Example map:
// map[--help:false --blob-store-url:file:///tmp --sync-gw-url:http://blah.com:4985/et]
func (c Configuration) Merge(parsedDocOpts map[string]interface{}) (Configuration, error) {

// Sync Gateway URL
syncGwUrl, ok := parsedDocOpts["--sync-gw-url"].(string)
if !ok {
return c, fmt.Errorf("Expected string arg in --sync-gw-url, got %T", syncGwUrl)
}
c.DbUrl = syncGwUrl

// Blob Store URL
blobStoreUrl, ok := parsedDocOpts["--blob-store-url"].(string)
if !ok {
return c, fmt.Errorf("Expected string arg in --blob-store-url, got %T", blobStoreUrl)
}
c.CbfsUrl = blobStoreUrl

return c, nil

}

0 comments on commit aba5d61

Please sign in to comment.