Skip to content

Instantly share code, notes, and snippets.

@lukebakken
Last active September 28, 2015 18:34
Show Gist options
  • Select an option

  • Save lukebakken/df5ef740c82997957cba to your computer and use it in GitHub Desktop.

Select an option

Save lukebakken/df5ef740c82997957cba to your computer and use it in GitHub Desktop.

Revisions

  1. lukebakken revised this gist Sep 16, 2015. 8 changed files with 52 additions and 17 deletions.
    12 changes: 9 additions & 3 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    .PHONY: all install-deps lint run

    PROJDIR = $(realpath $(CURDIR))
    RUNCMD = go run $(wildcard *.go)

    all: install-deps lint run

    @@ -9,10 +10,15 @@ install-deps:

    lint: install-deps
    go tool vet -shadow=true -shadowstrict=true $(PROJDIR)
    go vet intro-riak-kv/...
    go vet ./...

    fmt:
    gofmt -s -w $(PROJDIR)

    run: lint
    go run ./main.go ./ex01.go ./ex02.go ./ex03.go ./ex04.go ./ex05.go ./util.go
    run: run-intro-examples run-docs-examples

    run-intro-examples: lint
    $(RUNCMD) intro

    run-docs-examples: lint
    $(RUNCMD) docs
    2 changes: 1 addition & 1 deletion ex01.go → intro-ex01.go
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    package main

    func ex1_ping() {
    func intro_ex1_ping() {
    client := BuildClient()
    defer client.Stop()

    2 changes: 1 addition & 1 deletion ex02.go → intro-ex02.go
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ import (
    riak "github.com/basho/riak-go-client"
    )

    func ex2_simple_app() {
    func intro_ex2_simple_app() {
    client := BuildClient()
    defer client.Stop()

    2 changes: 1 addition & 1 deletion ex03.go → intro-ex03.go
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ package main

    import riak "github.com/basho/riak-go-client"

    func ex3_enable_search() {
    func intro_ex3_enable_search() {
    client := BuildClient()
    defer client.Stop()

    2 changes: 1 addition & 1 deletion ex04.go → intro-ex04.go
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ type ClientInfo struct {
    key string
    }

    func ex4_do_search() {
    func intro_ex4_do_search() {
    client := BuildClient()
    defer client.Stop()

    2 changes: 1 addition & 1 deletion ex05.go → intro-ex05.go
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ import (
    riak "github.com/basho/riak-go-client"
    )

    func ex5_do_search() {
    func intro_ex5_do_search() {
    client := BuildClient()
    defer client.Stop()

    24 changes: 19 additions & 5 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,23 @@
    package main

    import (
    "errors"
    "os"
    )

    func main() {
    ex1_ping()
    ex2_simple_app()
    ex3_enable_search()
    ex4_do_search()
    ex5_do_search()
    if len(os.Args) < 2 {
    ErrExit(errors.New("must be run with one argument"))
    }
    switch os.Args[1] {
    case "intro":
    Log.Println("running Intro to Riak KV examples")
    intro_ex1_ping()
    intro_ex2_simple_app()
    intro_ex3_enable_search()
    intro_ex4_do_search()
    intro_ex5_do_search()
    case "docs":
    Log.Println("running Basho Docs examples")
    }
    }
    23 changes: 19 additions & 4 deletions make.ps1
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,8 @@
    Target to build. Can be one of the following:
    * Format - run *.go files through 'go fmt'
    * Run - Run all examples
    * Intro - Run "Intro to Riak KV" examples
    * Docs - Run Basho Docs examples
    .PARAMETER Verbose
    Use to increase verbosity.
    .EXAMPLE
    @@ -21,13 +23,13 @@
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$False, Position=0)]
    [ValidateSet('Format', 'Run', IgnoreCase = $True)]
    [ValidateSet('Format', 'Run', 'Intro', 'Docs', IgnoreCase = $True)]
    [string]$Target = 'Run'
    )

    Set-StrictMode -Version Latest

    $package = 'intro-riak-kv'
    $package = 'riak-go-examples'

    $IsDebug = $DebugPreference -ne 'SilentlyContinue'
    $IsVerbose = $VerbosePreference -ne 'SilentlyContinue'
    @@ -93,17 +95,30 @@ function Do-Vet {
    Execute $cmd $argz
    }

    function Do-Run {
    function Do-Run-Intro {
    $cmd = 'go.exe'
    $argz = 'run', 'main.go', 'util.go', 'intro-ex01.go', 'intro-ex02.go', 'intro-ex03.go', 'intro-ex04.go', 'intro-ex05.go', 'intro'
    Execute $cmd $argz
    }

    function Do-Run-Docs {
    $cmd = 'go.exe'
    $argz = 'run', './main.go', './ex01.go', './ex02.go', './ex03.go', './ex04.go', './ex05.go', './util.go'
    $argz = 'run', 'main.go', 'util.go', 'docs'
    Execute $cmd $argz
    }

    function Do-Run {
    Do-Run-Intro
    Do-Run-Docs
    }

    Write-Debug "Target: $Target"

    switch ($Target)
    {
    'Format' { Do-Format }
    'Intro' { Do-InstallDeps; Do-Vet; Do-Run-Intro; }
    'Docs' { Do-InstallDeps; Do-Vet; Do-Run-Docs; }
    'Run' { Do-InstallDeps; Do-Vet; Do-Run; }
    default { throw "Unknown target: $Target" }
    }
  2. lukebakken revised this gist Sep 16, 2015. No changes.
  3. lukebakken revised this gist Sep 16, 2015. 2 changed files with 17 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions embed-two.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <html>
    <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/gist-embed/2.2/gist-embed.min.js"></script>
    </head>
    <body>
    <p>This is an example of embedding a gist using gist-embed.</p>
    <p><code data-gist-id="df5ef740c82997957cba" data-gist-file="ex02.go" data-gist-hide-footer="true"></code></p>
    <p>Pretty slick, huh?</p>
    </body>
    </html>
    6 changes: 6 additions & 0 deletions embed.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    <html>
    <body>
    <p>This is an example of embedding a gist</p>
    <p><script src="https://gist.github.com/lukebakken/df5ef740c82997957cba.js?file=ex01.go"></script></p>
    </body>
    </html>
  4. lukebakken revised this gist Sep 15, 2015. 6 changed files with 65 additions and 24 deletions.
    2 changes: 1 addition & 1 deletion Makefile
    Original file line number Diff line number Diff line change
    @@ -15,4 +15,4 @@ fmt:
    gofmt -s -w $(PROJDIR)

    run: lint
    go run ./main.go ./ex01.go ./ex02.go ./ex03.go ./ex04.go ./util.go
    go run ./main.go ./ex01.go ./ex02.go ./ex03.go ./ex04.go ./ex05.go ./util.go
    10 changes: 5 additions & 5 deletions ex03.go
    Original file line number Diff line number Diff line change
    @@ -10,29 +10,29 @@ func ex3_enable_search() {
    var err error

    cmd, err = riak.NewStoreIndexCommandBuilder().
    WithIndexName("libraries").
    WithIndexName("clients").
    WithSchemaName("_yz_default").
    Build()
    CheckErr(err)
    err = client.Execute(cmd)
    CheckErr(err)

    cmd, err = riak.NewStoreBucketPropsCommandBuilder().
    WithBucket("libraries").
    WithSearchIndex("libraries").
    WithBucket("coding-with-riak").
    WithSearchIndex("clients").
    Build()
    CheckErr(err)
    err = client.Execute(cmd)
    CheckErr(err)

    cmd, err = riak.NewFetchBucketPropsCommandBuilder().
    WithBucket("libraries").
    WithBucket("coding-with-riak").
    Build()
    CheckErr(err)
    err = client.Execute(cmd)
    CheckErr(err)

    fcmd := cmd.(*riak.FetchBucketPropsCommand)
    resp := fcmd.Response
    Log.Println("[ex03] libraries bucket search index:", resp.SearchIndex)
    Log.Println("[ex03] coding-with-riak bucket search index:", resp.SearchIndex)
    }
    22 changes: 5 additions & 17 deletions ex04.go
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@ package main

    import (
    "encoding/json"
    "time"

    riak "github.com/basho/riak-go-client"
    )
    @@ -20,22 +21,6 @@ func ex4_do_search() {
    var cmd riak.Command
    var err error

    cmd, err = riak.NewStoreIndexCommandBuilder().
    WithIndexName("clients").
    WithSchemaName("_yz_default").
    Build()
    CheckErr(err)
    err = client.Execute(cmd)
    CheckErr(err)

    cmd, err = riak.NewStoreBucketPropsCommandBuilder().
    WithBucket("coding-with-riak").
    WithSearchIndex("clients").
    Build()
    CheckErr(err)
    err = client.Execute(cmd)
    CheckErr(err)

    clients := []ClientInfo{
    {"Ruby Client", "Basho", true, "ruby"},
    {"Go Client", "Basho", true, "go"},
    @@ -63,9 +48,12 @@ func ex4_do_search() {
    WithQuery("Maintainer_s:Basho").
    Build()
    CheckErr(err)

    time.Sleep(time.Second * 2)

    err = client.Execute(cmd)
    CheckErr(err)

    scmd := cmd.(*riak.SearchCommand)
    JsonDump(scmd.Response)
    Log.Println("[ex04] NumFound:", scmd.Response.NumFound)
    }
    52 changes: 52 additions & 0 deletions ex05.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    package main

    import (
    "encoding/json"
    "time"

    riak "github.com/basho/riak-go-client"
    )

    func ex5_do_search() {
    client := BuildClient()
    defer client.Stop()

    var cmd riak.Command
    var err error

    clientInfo := ClientInfo{"Python Client", "Basho", true, "python"}

    cj, jerr := json.Marshal(clientInfo)
    CheckErr(jerr)

    obj := &riak.Object{
    ContentType: "application/json",
    Charset: "utf-8",
    Value: cj,
    }
    cmd, err = riak.NewStoreValueCommandBuilder().
    WithBucket("coding-with-riak").
    WithKey(clientInfo.key).
    WithContent(obj).
    Build()
    CheckErr(err)

    err = client.Execute(cmd)
    CheckErr(err)

    cmd, err = riak.NewSearchCommandBuilder().
    WithIndexName("clients").
    WithQuery("Maintainer_s:Basho").
    Build()
    CheckErr(err)

    time.Sleep(time.Second * 2)

    err = client.Execute(cmd)
    CheckErr(err)

    scmd := cmd.(*riak.SearchCommand)
    for _, doc := range scmd.Response.Docs {
    Log.Printf("%s, %s", doc.Fields["Name_s"][0], doc.Fields["Maintainer_s"][0])
    }
    }
    1 change: 1 addition & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -5,4 +5,5 @@ func main() {
    ex2_simple_app()
    ex3_enable_search()
    ex4_do_search()
    ex5_do_search()
    }
    2 changes: 1 addition & 1 deletion make.ps1
    Original file line number Diff line number Diff line change
    @@ -95,7 +95,7 @@ function Do-Vet {

    function Do-Run {
    $cmd = 'go.exe'
    $argz = 'run', './main.go', './ex01.go', './ex02.go', './ex03.go', './ex04.go', './util.go'
    $argz = 'run', './main.go', './ex01.go', './ex02.go', './ex03.go', './ex04.go', './ex05.go', './util.go'
    Execute $cmd $argz
    }

  5. lukebakken revised this gist Sep 15, 2015. 4 changed files with 74 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion Makefile
    Original file line number Diff line number Diff line change
    @@ -15,4 +15,4 @@ fmt:
    gofmt -s -w $(PROJDIR)

    run: lint
    go run ./main.go ./ex01.go ./ex02.go ./ex03.go ./util.go
    go run ./main.go ./ex01.go ./ex02.go ./ex03.go ./ex04.go ./util.go
    71 changes: 71 additions & 0 deletions ex04.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    package main

    import (
    "encoding/json"

    riak "github.com/basho/riak-go-client"
    )

    type ClientInfo struct {
    Name_s string
    Maintainer_s string
    Popular_b bool
    key string
    }

    func ex4_do_search() {
    client := BuildClient()
    defer client.Stop()

    var cmd riak.Command
    var err error

    cmd, err = riak.NewStoreIndexCommandBuilder().
    WithIndexName("clients").
    WithSchemaName("_yz_default").
    Build()
    CheckErr(err)
    err = client.Execute(cmd)
    CheckErr(err)

    cmd, err = riak.NewStoreBucketPropsCommandBuilder().
    WithBucket("coding-with-riak").
    WithSearchIndex("clients").
    Build()
    CheckErr(err)
    err = client.Execute(cmd)
    CheckErr(err)

    clients := []ClientInfo{
    {"Ruby Client", "Basho", true, "ruby"},
    {"Go Client", "Basho", true, "go"},
    }
    for _, c := range clients {
    cj, jerr := json.Marshal(c)
    CheckErr(jerr)
    obj := &riak.Object{
    ContentType: "application/json",
    Charset: "utf-8",
    Value: cj,
    }
    cmd, err = riak.NewStoreValueCommandBuilder().
    WithBucket("coding-with-riak").
    WithKey(c.key).
    WithContent(obj).
    Build()
    CheckErr(err)
    err = client.Execute(cmd)
    CheckErr(err)
    }

    cmd, err = riak.NewSearchCommandBuilder().
    WithIndexName("clients").
    WithQuery("Maintainer_s:Basho").
    Build()
    CheckErr(err)
    err = client.Execute(cmd)
    CheckErr(err)

    scmd := cmd.(*riak.SearchCommand)
    JsonDump(scmd.Response)
    }
    1 change: 1 addition & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,5 @@ func main() {
    ex1_ping()
    ex2_simple_app()
    ex3_enable_search()
    ex4_do_search()
    }
    2 changes: 1 addition & 1 deletion make.ps1
    Original file line number Diff line number Diff line change
    @@ -95,7 +95,7 @@ function Do-Vet {

    function Do-Run {
    $cmd = 'go.exe'
    $argz = 'run', './main.go', './ex01.go', './ex02.go', './ex03.go', './util.go'
    $argz = 'run', './main.go', './ex01.go', './ex02.go', './ex03.go', './ex04.go', './util.go'
    Execute $cmd $argz
    }

  6. lukebakken revised this gist Sep 15, 2015. 1 changed file with 32 additions and 0 deletions.
    32 changes: 32 additions & 0 deletions ex03.go
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,38 @@
    package main

    import riak "github.com/basho/riak-go-client"

    func ex3_enable_search() {
    client := BuildClient()
    defer client.Stop()

    var cmd riak.Command
    var err error

    cmd, err = riak.NewStoreIndexCommandBuilder().
    WithIndexName("libraries").
    WithSchemaName("_yz_default").
    Build()
    CheckErr(err)
    err = client.Execute(cmd)
    CheckErr(err)

    cmd, err = riak.NewStoreBucketPropsCommandBuilder().
    WithBucket("libraries").
    WithSearchIndex("libraries").
    Build()
    CheckErr(err)
    err = client.Execute(cmd)
    CheckErr(err)

    cmd, err = riak.NewFetchBucketPropsCommandBuilder().
    WithBucket("libraries").
    Build()
    CheckErr(err)
    err = client.Execute(cmd)
    CheckErr(err)

    fcmd := cmd.(*riak.FetchBucketPropsCommand)
    resp := fcmd.Response
    Log.Println("[ex03] libraries bucket search index:", resp.SearchIndex)
    }
  7. lukebakken revised this gist Sep 15, 2015. 7 changed files with 24 additions and 16 deletions.
    2 changes: 1 addition & 1 deletion Makefile
    Original file line number Diff line number Diff line change
    @@ -15,4 +15,4 @@ fmt:
    gofmt -s -w $(PROJDIR)

    run: lint
    go run ./main.go ./ex01-ping.go ./util.go
    go run ./main.go ./ex01.go ./ex02.go ./ex03.go ./util.go
    11 changes: 2 additions & 9 deletions ex1.go → ex01.go
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,8 @@
    package main

    import (
    riak "github.com/basho/riak-go-client"
    )

    func ex1_ping() {
    opts := &riak.NewClientOptions{
    RemoteAddresses: []string{"riak-test:10017"},
    }
    client, cerr := riak.NewClient(opts)
    CheckErr(cerr)
    client := BuildClient()
    defer client.Stop()

    success, perr := client.Ping()
    CheckErr(perr)
    7 changes: 2 additions & 5 deletions ex2.go → ex02.go
    Original file line number Diff line number Diff line change
    @@ -7,11 +7,8 @@ import (
    )

    func ex2_simple_app() {
    opts := &riak.NewClientOptions{
    RemoteAddresses: []string{"riak-test:10017"},
    }
    client, cerr := riak.NewClient(opts)
    CheckErr(cerr)
    client := BuildClient()
    defer client.Stop()

    monty_python := []string{
    "Graham Chapman", "Eric Idle",
    6 changes: 6 additions & 0 deletions ex03.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    package main

    func ex3_enable_search() {
    client := BuildClient()
    defer client.Stop()
    }
    1 change: 1 addition & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -3,4 +3,5 @@ package main
    func main() {
    ex1_ping()
    ex2_simple_app()
    ex3_enable_search()
    }
    2 changes: 1 addition & 1 deletion make.ps1
    Original file line number Diff line number Diff line change
    @@ -95,7 +95,7 @@ function Do-Vet {

    function Do-Run {
    $cmd = 'go.exe'
    $argz = 'run', './main.go', './ex1.go', './ex2.go', './util.go'
    $argz = 'run', './main.go', './ex01.go', './ex02.go', './ex03.go', './util.go'
    Execute $cmd $argz
    }

    11 changes: 11 additions & 0 deletions util.go
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,8 @@ import (
    "os"
    "strconv"
    "time"

    riak "github.com/basho/riak-go-client"
    )

    const iso8601format = "2006-01-02T15:04:05"
    @@ -65,6 +67,15 @@ func GetRiakAddresses() []string {
    return addrs
    }

    func BuildClient() *riak.Client {
    opts := &riak.NewClientOptions{
    RemoteAddresses: GetRiakAddresses(),
    }
    client, err := riak.NewClient(opts)
    CheckErr(err)
    return client
    }

    func JsonDump(val interface{}) {
    if val == nil {
    Log.Println("[JsonDump]", "NIL VAL")
  8. lukebakken revised this gist Sep 14, 2015. 4 changed files with 59 additions and 1 deletion.
    File renamed without changes.
    57 changes: 57 additions & 0 deletions ex2.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    package main

    import (
    "encoding/json"

    riak "github.com/basho/riak-go-client"
    )

    func ex2_simple_app() {
    opts := &riak.NewClientOptions{
    RemoteAddresses: []string{"riak-test:10017"},
    }
    client, cerr := riak.NewClient(opts)
    CheckErr(cerr)

    monty_python := []string{
    "Graham Chapman", "Eric Idle",
    "Terry Gilliam", "Terry Jones",
    "John Cleese", "Michael Palin",
    }

    j, jerr := json.Marshal(monty_python)
    CheckErr(jerr)

    object := &riak.Object{
    ContentType: "application/json",
    Value: j,
    }

    var cmd riak.Command
    var err error
    cmd, err = riak.NewStoreValueCommandBuilder().
    WithBucket("ex2").
    WithKey("Monty Python").
    WithContent(object).
    Build()
    CheckErr(err)

    err = client.Execute(cmd)
    CheckErr(err)

    cmd, err = riak.NewFetchValueCommandBuilder().
    WithBucket("ex2").
    WithKey("Monty Python").
    Build()
    CheckErr(err)

    err = client.Execute(cmd)
    CheckErr(err)

    var fetched_pythons []string
    fcmd := cmd.(*riak.FetchValueCommand)
    val := fcmd.Response.Values[0].Value
    err = json.Unmarshal(val, &fetched_pythons)
    CheckErr(err)
    JsonDump(fetched_pythons)
    }
    1 change: 1 addition & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,5 @@ package main

    func main() {
    ex1_ping()
    ex2_simple_app()
    }
    2 changes: 1 addition & 1 deletion make.ps1
    Original file line number Diff line number Diff line change
    @@ -95,7 +95,7 @@ function Do-Vet {

    function Do-Run {
    $cmd = 'go.exe'
    $argz = 'run', './main.go', './ex1-ping.go', './util.go'
    $argz = 'run', './main.go', './ex1.go', './ex2.go', './util.go'
    Execute $cmd $argz
    }

  9. lukebakken revised this gist Sep 14, 2015. 5 changed files with 15 additions and 13 deletions.
    8 changes: 3 additions & 5 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    .PHONY: all install-deps lint run run01
    .PHONY: all install-deps lint run

    PROJDIR = $(realpath $(CURDIR))

    @@ -14,7 +14,5 @@ lint: install-deps
    fmt:
    gofmt -s -w $(PROJDIR)

    run: lint run01

    run01:
    go run ./ex01-ping.go
    run: lint
    go run ./main.go ./ex01-ping.go ./util.go
    11 changes: 5 additions & 6 deletions ex1-ping.go
    Original file line number Diff line number Diff line change
    @@ -2,22 +2,21 @@ package main

    import (
    riak "github.com/basho/riak-go-client"
    util "intro-riak-kv/util"
    )

    func main() {
    func ex1_ping() {
    opts := &riak.NewClientOptions{
    RemoteAddresses: []string{"riak-test:10017"},
    }
    client, cerr := riak.NewClient(opts)
    util.CheckErr(cerr)
    CheckErr(cerr)

    success, perr := client.Ping()
    util.CheckErr(perr)
    CheckErr(perr)

    if actual, expected := success, true; actual != expected {
    util.ErrLog.Printf("[ERROR] got %v, expected %v", actual, expected)
    ErrLog.Printf("[ERROR] got %v, expected %v", actual, expected)
    } else {
    util.Log.Printf("[INFO] ping result %v", success)
    Log.Printf("[INFO] ping result %v", success)
    }
    }
    5 changes: 5 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    package main

    func main() {
    ex1_ping()
    }
    2 changes: 1 addition & 1 deletion make.ps1
    Original file line number Diff line number Diff line change
    @@ -95,7 +95,7 @@ function Do-Vet {

    function Do-Run {
    $cmd = 'go.exe'
    $argz = 'run', './ex1-ping.go'
    $argz = 'run', './main.go', './ex1-ping.go', './util.go'
    Execute $cmd $argz
    }

    2 changes: 1 addition & 1 deletion util/util.go → util.go
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    package util
    package main

    import (
    "encoding/json"
  10. lukebakken revised this gist Sep 14, 2015. 6 changed files with 236 additions and 1 deletion.
    20 changes: 20 additions & 0 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    .PHONY: all install-deps lint run run01

    PROJDIR = $(realpath $(CURDIR))

    all: install-deps lint run

    install-deps:
    go get -t ./...

    lint: install-deps
    go tool vet -shadow=true -shadowstrict=true $(PROJDIR)
    go vet intro-riak-kv/...

    fmt:
    gofmt -s -w $(PROJDIR)

    run: lint run01

    run01:
    go run ./ex01-ping.go
    23 changes: 23 additions & 0 deletions ex1-ping.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    package main

    import (
    riak "github.com/basho/riak-go-client"
    util "intro-riak-kv/util"
    )

    func main() {
    opts := &riak.NewClientOptions{
    RemoteAddresses: []string{"riak-test:10017"},
    }
    client, cerr := riak.NewClient(opts)
    util.CheckErr(cerr)

    success, perr := client.Ping()
    util.CheckErr(perr)

    if actual, expected := success, true; actual != expected {
    util.ErrLog.Printf("[ERROR] got %v, expected %v", actual, expected)
    } else {
    util.Log.Printf("[INFO] ping result %v", success)
    }
    }
    1 change: 0 additions & 1 deletion examples.go
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    package main
    4 changes: 4 additions & 0 deletions make.cmd
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    @echo off
    setlocal
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "%~dp0\make.ps1" %*
    exit /b %ERRORLEVEL%
    111 changes: 111 additions & 0 deletions make.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,111 @@
    <#
    .SYNOPSIS
    Powershell script to run Intro to Riak KV for Go on Windows
    .DESCRIPTION
    This script will run 'go' correctly depending on parameters passed to this script.
    .PARAMETER Target
    Target to build. Can be one of the following:
    * Format - run *.go files through 'go fmt'
    * Run - Run all examples
    .PARAMETER Verbose
    Use to increase verbosity.
    .EXAMPLE
    C:\Users\Bashoman> cd $env:GOPATH\src
    C:\Users\Bashoman> git clone -o intro-riak-kv git@gist.github.com:df5ef740c82997957cba.git
    C:\Users\Bashoman> cd intro-riak-kv
    C:\Users\Bashoman\go\src\intro-riak-kv>.\make.ps1 -Target Run -Verbose
    .NOTES
    Author: Luke Bakken
    Date: August 26, 2015
    #>
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$False, Position=0)]
    [ValidateSet('Format', 'Run', IgnoreCase = $True)]
    [string]$Target = 'Run'
    )

    Set-StrictMode -Version Latest

    $package = 'intro-riak-kv'

    $IsDebug = $DebugPreference -ne 'SilentlyContinue'
    $IsVerbose = $VerbosePreference -ne 'SilentlyContinue'

    # Note:
    # Set to Continue to see DEBUG messages
    if ($IsVerbose) {
    $DebugPreference = 'Continue'
    }

    trap
    {
    Write-Error -ErrorRecord $_
    exit 1
    }

    function Get-ScriptPath {
    $scriptDir = Get-Variable PSScriptRoot -ErrorAction SilentlyContinue | ForEach-Object { $_.Value }
    if (!$scriptDir) {
    if ($MyInvocation.MyCommand.Path) {
    $scriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
    }
    }
    if (!$scriptDir) {
    if ($ExecutionContext.SessionState.Module.Path) {
    $scriptDir = Split-Path (Split-Path $ExecutionContext.SessionState.Module.Path)
    }
    }
    if (!$scriptDir) {
    $scriptDir = $PWD
    }
    return $scriptDir
    }

    function Execute($cmd, $argz) {
    Write-Verbose "$cmd $argz"
    & $cmd $argz
    if ($? -ne $True) {
    throw "'$cmd $argz' failed: $LastExitCode"
    }
    Write-Debug "'$cmd $argz' exit code: $LastExitCode"
    }

    function Do-Format {
    $script_path = Get-ScriptPath
    $cmd = 'gofmt'
    $argz = '-s', '-w', $script_path
    Execute $cmd $argz
    }

    function Do-InstallDeps {
    $cmd = 'go.exe'
    $argz = 'get', '-t', './...'
    Execute $cmd $argz
    }

    function Do-Vet {
    $cmd = 'go.exe'
    $script_path = Get-ScriptPath
    $argz = 'tool', 'vet', '-shadow=true', '-shadowstrict=true', $script_path
    Execute $cmd $argz
    $argz = 'vet', "$package/..."
    Execute $cmd $argz
    }

    function Do-Run {
    $cmd = 'go.exe'
    $argz = 'run', './ex1-ping.go'
    Execute $cmd $argz
    }

    Write-Debug "Target: $Target"

    switch ($Target)
    {
    'Format' { Do-Format }
    'Run' { Do-InstallDeps; Do-Vet; Do-Run; }
    default { throw "Unknown target: $Target" }
    }

    exit 0
    78 changes: 78 additions & 0 deletions util/util.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    package util

    import (
    "encoding/json"
    "fmt"
    "log"
    "os"
    "strconv"
    "time"
    )

    const iso8601format = "2006-01-02T15:04:05"

    var Log = log.New(os.Stdout, "", log.LstdFlags)
    var ErrLog = log.New(os.Stderr, "", log.LstdFlags)

    func Iso8601(t time.Time) string {
    return t.Format(iso8601format)
    }

    func CheckErr(err error) {
    if err != nil {
    ErrExit(err)
    }
    }

    func ErrExit(err error) {
    ErrLog.Println(err)
    os.Exit(1)
    }

    func GetRiakPort() uint16 {
    riakPort := uint16(10017)
    if portEnvVar := os.ExpandEnv("$RIAK_PORT"); portEnvVar != "" {
    if portNum, err := strconv.Atoi(portEnvVar); err == nil {
    riakPort = uint16(portNum)
    }
    }
    return riakPort
    }

    func GetRiakHost() string {
    riakHost := "riak-test"
    if hostEnvVar := os.ExpandEnv("$RIAK_HOST"); hostEnvVar != "" {
    riakHost = hostEnvVar
    }
    return riakHost
    }

    func GetRiakAddress() string {
    return fmt.Sprintf("%s:%d", GetRiakHost(), GetRiakPort())
    }

    func GetRiakAddresses() []string {
    // Assume that a 4-node devrel is being used where PB port numbers
    // increase by 10
    host := GetRiakHost()
    basePort := GetRiakPort()
    count := uint16(4)
    addrs := make([]string, count)
    for i := uint16(0); i < count; i++ {
    port := basePort + (i * 10)
    addrs[i] = fmt.Sprintf("%s:%d", host, port)
    }
    return addrs
    }

    func JsonDump(val interface{}) {
    if val == nil {
    Log.Println("[JsonDump]", "NIL VAL")
    } else {
    if json, err := json.MarshalIndent(val, "", " "); err != nil {
    ErrLog.Printf("[JsonDump ERROR] %s", err.Error())
    } else {
    Log.Println(string(json))
    }
    }
    }
  11. lukebakken created this gist Sep 14, 2015.
    1 change: 1 addition & 0 deletions examples.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    package main