Skip to content

Commit

Permalink
Merge pull request #7 from rdooley/feelin-testy
Browse files Browse the repository at this point in the history
Testing PR
  • Loading branch information
Rees Dooley authored May 14, 2019
2 parents 42e84f1 + eab098d commit 87bf0e0
Show file tree
Hide file tree
Showing 13 changed files with 383 additions and 150 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
vendor
tags
build

coverage.txt
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: go

go:
- "1.12"

script:
- env GO111MODULE=on go build
- env GO111MODULE=on go test
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PROJECT_NAME=confidynt ## @build Project name
DEFAULT_TARGET=$(BUILD_DIR)/$(PROJECT_NAME)
MAKEFILE_LIST = Makefile

.PHONY: clean build install help help-variables
.PHONY: clean build generate install help help-variables

clean: ## @build Clean stuff
rm -rf $(BUILD_DIR)
Expand All @@ -17,6 +17,9 @@ build $(DEFAULT_TARGET): ## @build Build the actual thing
# build for specific operating systems
BUILD_DIR=$(BUILD_DIR) PROJECT_NAME=$(PROJECT_NAME) ./build.sh "linux/amd64" "darwin/amd64"

generate:
go generate ./...

install: build ## @build Install make-doc to /usr/local/bin
sudo cp $(DEFAULT_TARGET) /usr/local/bin

Expand Down
45 changes: 45 additions & 0 deletions cli/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cli

import (
"fmt"
"io"

"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/dynamodb"

"github.com/rdooley/confidynt/service"
)

// Read a config from dynamo db
func Read(table, key, value string, ds service.Dynamo, w io.Writer) {
config, err := ds.Read(table, key, value)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case dynamodb.ErrCodeProvisionedThroughputExceededException:
fmt.Fprintln(w, dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
case dynamodb.ErrCodeResourceNotFoundException:
fmt.Fprintln(w, dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
case dynamodb.ErrCodeRequestLimitExceeded:
fmt.Fprintln(w, dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
case dynamodb.ErrCodeInternalServerError:
fmt.Fprintln(w, dynamodb.ErrCodeInternalServerError, aerr.Error())
default:
fmt.Fprintln(w, aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Fprintln(w, err.Error())
}
return
}

fmt.Fprintf(w, "%s=%s\n", key, config[key])
for k, v := range config {
if k != key {
fmt.Fprintf(w, "%s=%s\n", k, v)
}
}

}
36 changes: 36 additions & 0 deletions cli/read_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cli

import (
"bytes"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/rdooley/confidynt/service"
"github.com/rdooley/confidynt/types"
)

func TestRead(t *testing.T) {
t.Log("Testing Read")
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

table := "table"
key := "key"
val := "val"

conf := types.Config{}
conf[key] = val
conf["other_key"] = "other_val"

expected := "key=val\n"
expected += "other_key=other_val\n"

buf := new(bytes.Buffer)

mockDynamo := service.NewMockDynamo(mockCtrl)
mockDynamo.EXPECT().Read(table, key, val).Return(conf, nil)
Read(table, key, val, mockDynamo, buf)
assert.Equal(t, buf.String(), expected)
}
88 changes: 88 additions & 0 deletions cli/write.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cli

import (
"bufio"
"fmt"
"io"
"log"
"os"
"regexp"
"strings"

"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/dynamodb"

"github.com/rdooley/confidynt/service"
"github.com/rdooley/confidynt/types"
)

var propRe = regexp.MustCompile(`^(\w+)=(.*)$`)

// Write a given config file to a dynamo db table
func Write(table string, path string, ds service.Dynamo, w io.Writer) {
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
config := types.Config{}
var key string
var value string
for scanner.Scan() {
line := scanner.Text()
if line == "" {
// Ignore blank lines
continue
}
if propRe.MatchString(line) {
if key != "" {
config[key] = value
}
matches := propRe.FindStringSubmatch(line)
key = matches[1]
value = matches[2]
} else {
// Continuation of a previous property "^ indented..."
value += "\n" + strings.TrimRight(line, " \t")
}
}
// Catch final prop
if key != "" {
config[key] = value
}

if err := scanner.Err(); err != nil {
log.Fatal(err)
}
err = ds.Write(table, config)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case dynamodb.ErrCodeConditionalCheckFailedException:
fmt.Fprintln(w, dynamodb.ErrCodeConditionalCheckFailedException, aerr.Error())
case dynamodb.ErrCodeProvisionedThroughputExceededException:
fmt.Fprintln(w, dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
case dynamodb.ErrCodeResourceNotFoundException:
fmt.Fprintln(w, dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
case dynamodb.ErrCodeItemCollectionSizeLimitExceededException:
fmt.Fprintln(w, dynamodb.ErrCodeItemCollectionSizeLimitExceededException, aerr.Error())
case dynamodb.ErrCodeTransactionConflictException:
fmt.Fprintln(w, dynamodb.ErrCodeTransactionConflictException, aerr.Error())
case dynamodb.ErrCodeRequestLimitExceeded:
fmt.Fprintln(w, dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
case dynamodb.ErrCodeInternalServerError:
fmt.Fprintln(w, dynamodb.ErrCodeInternalServerError, aerr.Error())
default:
fmt.Fprintln(w, aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Fprintln(w, err.Error())
}
return
}
fmt.Fprintf(w, "%s written to %s\n", path, table)
}
44 changes: 44 additions & 0 deletions cli/write_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cli

import (
"bytes"
"io/ioutil"
"os"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/rdooley/confidynt/service"
"github.com/rdooley/confidynt/types"
)

func TestWrite(t *testing.T) {
t.Log("Testing Write")
path := "test.conf"
mockCtrl := gomock.NewController(t)
defer func() {
os.Remove(path)
mockCtrl.Finish()
}()

table := "table"
key := "key"
val := "val"

conf := types.Config{}
conf[key] = val
conf["other_key"] = "other_val"

text := "key=val\n"
text += "other_key=other_val\n"
ioutil.WriteFile(path, []byte(text), 0644)

buf := new(bytes.Buffer)

mockDynamo := service.NewMockDynamo(mockCtrl)
mockDynamo.EXPECT().Write(table, conf).Return(nil)

Write(table, path, mockDynamo, buf)
assert.Equal(t, buf.String(), "test.conf written to table\n")
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/aws/aws-sdk-go v1.19.29
github.com/stretchr/testify v1.3.0 // indirect
github.com/golang/mock v1.3.1
github.com/stretchr/testify v1.3.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ github.com/aws/aws-sdk-go v1.19.29 h1:Uusurqi30wm5djEqi3R+9F+TNcGc3aSDSizoPcVp9S
github.com/aws/aws-sdk-go v1.19.29/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
Loading

0 comments on commit 87bf0e0

Please sign in to comment.