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.
Riak - Go client examples
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)
success, perr := client.Ping()
CheckErr(perr)
if actual, expected := success, true; actual != expected {
ErrLog.Printf("[ERROR] got %v, expected %v", actual, expected)
} else {
Log.Printf("[INFO] ping result %v", success)
}
}
package main
func main() {
ex1_ping()
}
@echo off
setlocal
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "%~dp0\make.ps1" %*
exit /b %ERRORLEVEL%
<#
.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', './main.go', './ex1-ping.go', './util.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
.PHONY: all install-deps lint run
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
go run ./main.go ./ex01-ping.go ./util.go
package main
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))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment