storj/examples/pointerdb-client/main.go
Egon Elbre 0f5a2f4ef5 Enable more linters (#272)
* enable more linters

* Run gofmt -s

* run goimports

* run unconvert

* fix naked return

* fix misspellings

* fix ineffectual assigments

* fix missing declaration

* don't use deprecated grpc.Errof

* check errors in tests

* run gofmt -w -r "assert.Nil(err) -> assert.NoError(err)"

* fix directory permissions

* don't use nil Context

* simplify boolean expressions

* use bytes.Equal instead of bytes.Compare

* merge variable declarations, remove redundant returns

* fix some golint errors

* run goimports

* handle more errors

* delete empty TestMain

* delete empty TestMain

* ignore examples for now

* fix lint errors

* remove unused values

* more fixes

* run gofmt -w -s .

* add more comments

* fix naming

* more lint fixes

* try switching travis to go1.11

* fix unnecessary conversions

* fix deprecated methods

* use go1.10 and disable gofmt/goimports for now

* switch to 1.10

* don't re-enable gofmt and goimports

* switch covermode to atomic because of -race

* gofmt
2018-08-27 11:28:16 -06:00

120 lines
2.9 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
p "storj.io/storj/pkg/paths"
client "storj.io/storj/pkg/pointerdb/pdbclient"
"storj.io/storj/pkg/provider"
"storj.io/storj/pkg/storage/meta"
proto "storj.io/storj/protos/pointerdb"
)
var (
pointerdbClientPort string
ctx = context.Background()
)
func initializeFlags() {
flag.StringVar(&pointerdbClientPort, "pointerdbPort", ":8080", "this is your port")
flag.Parse()
}
func main() {
initializeFlags()
logger, _ := zap.NewDevelopment()
defer logger.Sync()
ca, err := provider.NewCA(ctx, 12, 4)
if err != nil {
logger.Error("Failed to create certificate authority: ", zap.Error(err))
os.Exit(1)
}
identity, err := ca.NewIdentity()
if err != nil {
logger.Error("Failed to create full identity: ", zap.Error(err))
os.Exit(1)
}
APIKey := []byte("abc123")
pdbclient, err := client.NewClient(identity, pointerdbClientPort, APIKey)
if err != nil {
logger.Error("Failed to dial: ", zap.Error(err))
os.Exit(1)
}
logger.Debug(fmt.Sprintf("client dialed port %s", pointerdbClientPort))
ctx := context.Background()
// Example parameters to pass into API calls
var path = p.New("fold1/fold2/fold3/file.txt")
pointer := &proto.Pointer{
Type: proto.Pointer_INLINE,
InlineSegment: []byte("popcorn"),
}
// Example Put1
err = pdbclient.Put(ctx, path, pointer)
if err != nil || status.Code(err) == codes.Internal {
logger.Error("couldn't put pointer in db", zap.Error(err))
} else {
logger.Debug("Success: put pointer in db")
}
// Example Put2
err = pdbclient.Put(ctx, p.New("fold1/fold2"), pointer)
if err != nil || status.Code(err) == codes.Internal {
logger.Error("couldn't put pointer in db", zap.Error(err))
} else {
logger.Debug("Success: put pointer in db")
}
// Example Get
getRes, err := pdbclient.Get(ctx, path)
if err != nil {
logger.Error("couldn't GET pointer from db", zap.Error(err))
} else {
logger.Info("Success: got Pointer from db",
zap.String("pointer", getRes.String()),
)
}
// Example List with pagination
prefix := p.New("fold1")
items, more, err := pdbclient.List(ctx, prefix, nil, nil, true, 1, meta.None)
if err != nil || status.Code(err) == codes.Internal {
logger.Error("failed to list file paths", zap.Error(err))
} else {
var stringList []string
for _, item := range items {
stringList = append(stringList, item.Path.String())
}
logger.Debug("Success: listed paths: " + strings.Join(stringList, ", ") + "; more: " + fmt.Sprintf("%t", more))
}
// Example Delete
err = pdbclient.Delete(ctx, path)
if err != nil || status.Code(err) == codes.Internal {
logger.Error("Error in deleteing file from db", zap.Error(err))
} else {
logger.Debug("Success: file is deleted from db")
}
}