Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linting subpackages too... #302

Merged
merged 2 commits into from
Apr 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ GO := go
PKG_NAME := gomplate
PREFIX := .

ifeq ("$(CI)","true")
LINT_PROCS ?= 1
else
LINT_PROCS ?= $(shell nproc)
endif

COMMIT ?= `git rev-parse --short HEAD 2>/dev/null`
VERSION ?= `git describe --abbrev=0 --tags $(git rev-list --tags --max-count=1) 2>/dev/null | sed 's/v\(.*\)/\1/'`
BUILD_DATE ?= `date -u +"%Y-%m-%dT%H:%M:%SZ"`
Expand Down Expand Up @@ -90,13 +96,13 @@ gen-docs: docs/themes/hugo-material-docs
gomplate.png: gomplate.svg
cloudconvert -f png -c density=288 $^

ifeq ("$(CI)","true")
lint:
gometalinter -j 1 --vendor --deadline 120s --disable gotype --enable gofmt --enable goimports --enable misspell --enable unused --disable gas
else
lint:
gometalinter -j $(shell nproc) --vendor --deadline 120s --disable gotype --enable gofmt --enable goimports --enable misspell --enable unused --disable gas
endif
gometalinter -j $(LINT_PROCS) --vendor --skip test --deadline 120s --disable gotype --enable gofmt --enable goimports --enable misspell --disable gas ./...
gometalinter -j $(LINT_PROCS) --vendor --deadline 120s \
--disable gotype --disable megacheck --disable deadcode --disable gas \
--enable gofmt --enable goimports --enable misspell \
./test/integration
megacheck -tags integration ./test/integration

.PHONY: gen-changelog clean test build-x compress-all build-release build build-integration-image test-integration-docker gen-docs lint clean-images clean-containers docker-images
.DELETE_ON_ERROR:
Expand Down
19 changes: 10 additions & 9 deletions aws/ec2info.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
var describerClient InstanceDescriber

var (
co ClientOptions
coInit sync.Once
sdkSession *session.Session
co ClientOptions
coInit sync.Once
sdkSession *session.Session
sdkSessionInit sync.Once
)

Expand All @@ -39,9 +39,9 @@ type InstanceDescriber interface {
DescribeInstances(*ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error)
}

// Centralised reading of AWS_TIMEOUT
// GetClientOptions - Centralised reading of AWS_TIMEOUT
// ... but cannot use in vault/auth.go as different strconv.Atoi error handling
func GetClientOptions() (ClientOptions) {
func GetClientOptions() ClientOptions {
coInit.Do(func() {
timeout := os.Getenv("AWS_TIMEOUT")
if timeout != "" {
Expand All @@ -56,7 +56,8 @@ func GetClientOptions() (ClientOptions) {
return co
}

func SDKSession() (*session.Session) {
// SDKSession -
func SDKSession() *session.Session {
sdkSessionInit.Do(func() {
options := GetClientOptions()
timeout := options.Timeout
Expand All @@ -70,14 +71,14 @@ func SDKSession() (*session.Session) {
// Waiting for https://github.com/aws/aws-sdk-go/issues/1103
metaClient := NewEc2Meta(options)
metaRegion := metaClient.Region()
_, default1 := os.LookupEnv("AWS_REGION");
_, default2 := os.LookupEnv("AWS_DEFAULT_REGION");
_, default1 := os.LookupEnv("AWS_REGION")
_, default2 := os.LookupEnv("AWS_DEFAULT_REGION")
if metaRegion != "unknown" && !default1 && !default2 {
config = config.WithRegion(metaRegion)
}

sdkSession = session.Must(session.NewSessionWithOptions(session.Options{
Config: *config,
Config: *config,
SharedConfigState: session.SharedConfigEnable,
}))
})
Expand Down
1 change: 1 addition & 0 deletions aws/ec2meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (e *Ec2Meta) retrieveMetadata(url string, def ...string) string {
return returnDefault(def)
}

// nolint: errcheck
defer resp.Body.Close()
if resp.StatusCode > 399 {
return returnDefault(def)
Expand Down
64 changes: 36 additions & 28 deletions conv/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,10 @@ func MustAtoi(s string) int {
return i
}

// ToInt64 - taken from github.com/Masterminds/sprig
// ToInt64 - convert input to an int64, if convertible. Otherwise, returns 0.
func ToInt64(v interface{}) int64 {
if str, ok := v.(string); ok {
iv, err := strconv.ParseInt(str, 0, 64)
if err != nil {
// maybe it's a float?
var fv float64
fv, err = strconv.ParseFloat(str, 64)
if err != nil {
return 0
}
return ToInt64(fv)
}
return iv
return strToInt64(str)
}

val := reflect.Indirect(reflect.ValueOf(v))
Expand All @@ -150,7 +140,7 @@ func ToInt64(v interface{}) int64 {
case reflect.Float32, reflect.Float64:
return int64(val.Float())
case reflect.Bool:
if val.Bool() == true {
if val.Bool() {
return 1
}
return 0
Expand Down Expand Up @@ -182,22 +172,10 @@ func ToInts(in ...interface{}) []int {
return out
}

// ToFloat64 - taken from github.com/Masterminds/sprig
// ToFloat64 - convert input to a float64, if convertible. Otherwise, returns 0.
func ToFloat64(v interface{}) float64 {
if str, ok := v.(string); ok {
// this is inefficient, but it's the only way I can think of to
// properly convert octal integers to floats
iv, err := strconv.ParseInt(str, 0, 64)
if err != nil {
// ok maybe it's a float?
var fv float64
fv, err = strconv.ParseFloat(str, 64)
if err != nil {
return 0
}
return fv
}
return float64(iv)
return strToFloat64(str)
}

val := reflect.Indirect(reflect.ValueOf(v))
Expand All @@ -211,7 +189,7 @@ func ToFloat64(v interface{}) float64 {
case reflect.Float32, reflect.Float64:
return val.Float()
case reflect.Bool:
if val.Bool() == true {
if val.Bool() {
return 1
}
return 0
Expand All @@ -220,6 +198,36 @@ func ToFloat64(v interface{}) float64 {
}
}

func strToInt64(str string) int64 {
iv, err := strconv.ParseInt(str, 0, 64)
if err != nil {
// maybe it's a float?
var fv float64
fv, err = strconv.ParseFloat(str, 64)
if err != nil {
return 0
}
return ToInt64(fv)
}
return iv
}

func strToFloat64(str string) float64 {
// this is inefficient, but it's the only way I can think of to
// properly convert octal integers to floats
iv, err := strconv.ParseInt(str, 0, 64)
if err != nil {
// ok maybe it's a float?
var fv float64
fv, err = strconv.ParseFloat(str, 64)
if err != nil {
return 0
}
return fv
}
return float64(iv)
}

// ToFloat64s -
func ToFloat64s(in ...interface{}) []float64 {
out := make([]float64, len(in))
Expand Down
6 changes: 3 additions & 3 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func ToCSV(args ...interface{}) string {
if err != nil {
log.Fatal(err)
}
return string(b.Bytes())
return b.String()
}

func marshalObj(obj interface{}, f func(interface{}) ([]byte, error)) string {
Expand Down Expand Up @@ -231,7 +231,7 @@ func ToJSONPretty(indent string, in interface{}) string {
log.Fatalf("Unable to indent JSON %s: %v", b, err)
}

return string(out.Bytes())
return out.String()
}

// ToYAML - Stringify a struct as YAML
Expand All @@ -246,5 +246,5 @@ func ToTOML(in interface{}) string {
if err != nil {
log.Fatalf("Unable to marshal %s: %v", in, err)
}
return string(buf.Bytes())
return buf.String()
}
8 changes: 4 additions & 4 deletions data/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
// logFatal is defined so log.Fatal calls can be overridden for testing
var logFatalf = log.Fatalf

var json_mimetype = "application/json"
var jsonMimetype = "application/json"

// stdin - for overriding in tests
var stdin io.Reader
Expand Down Expand Up @@ -101,7 +101,7 @@ func NewData(datasourceArgs []string, headerArgs []string) *Data {
}
}

// - A subset of SSM API for use in unit testing
// AWSSMPGetter - A subset of SSM API for use in unit testing
type AWSSMPGetter interface {
GetParameter(*ssm.GetParameterInput) (*ssm.GetParameterOutput, error)
}
Expand Down Expand Up @@ -233,11 +233,11 @@ func (d *Data) Datasource(alias string, args ...string) interface{} {
if err != nil {
logFatalf("Couldn't read datasource '%s': %s", alias, err)
}
if b == nil || len(b) == 0 {
if len(b) == 0 {
logFatalf("No value found for %s from datasource '%s'", args, alias)
}
s := string(b)
if source.Type == json_mimetype {
if source.Type == jsonMimetype {
return JSON(s)
}
if source.Type == "application/yaml" {
Expand Down
14 changes: 8 additions & 6 deletions data/datasource_awssmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func parseAWSSMPArgs(origPath string, args ...string) (paramPath string, err err
}

if len(args) >= 2 {
err = errors.New("Maximum two arguments to aws+smp datasource: alias, extraPath")
err = errors.New("Maximum two arguments to aws+smp datasource: alias, extraPath")
}
return
}
Expand All @@ -28,15 +28,17 @@ func readAWSSMP(source *Source, args ...string) (output []byte, err error) {
}

paramPath, err := parseAWSSMPArgs(source.URL.Path, args...)
if err != nil {
return nil, err
}

source.Type = json_mimetype
output, err = readAWSSMPParam(source, paramPath)
return
source.Type = jsonMimetype
return readAWSSMPParam(source, paramPath)
}

func readAWSSMPParam(source *Source, paramPath string) ([]byte, error) {
input := &ssm.GetParameterInput{
Name: aws.String(paramPath),
Name: aws.String(paramPath),
WithDecryption: aws.Bool(true),
}

Expand All @@ -47,7 +49,7 @@ func readAWSSMPParam(source *Source, paramPath string) ([]byte, error) {
input, err)
return nil, err
}

result := *response.Parameter

output := ToJSON(result)
Expand Down
54 changes: 26 additions & 28 deletions data/datasource_awssmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

// DummyParamGetter - test double
type DummyParamGetter struct {
t *testing.T
param *ssm.Parameter
err awserr.Error
t *testing.T
param *ssm.Parameter
err awserr.Error
mockGetParameter func(*ssm.GetParameterInput) (*ssm.GetParameterOutput, error)
}

Expand All @@ -37,7 +37,7 @@ func (d DummyParamGetter) GetParameter(input *ssm.GetParameterInput) (*ssm.GetPa
func simpleAWSSourceHelper(dummy AWSSMPGetter) *Source {
return &Source{
Alias: "foo",
URL: &url.URL{
URL: &url.URL{
Scheme: "aws+smp",
Path: "/foo",
},
Expand Down Expand Up @@ -71,16 +71,16 @@ func TestAWSSMP_ParseArgsTooMany(t *testing.T) {
func TestAWSSMP_GetParameterSetup(t *testing.T) {
calledOk := false
s := simpleAWSSourceHelper(DummyParamGetter{
t: t,
mockGetParameter: func(input *ssm.GetParameterInput) (*ssm.GetParameterOutput, error) {
assert.Equal(t, "/foo/bar", *input.Name)
assert.True(t, *input.WithDecryption)
calledOk = true
return &ssm.GetParameterOutput{
Parameter: &ssm.Parameter{},
}, nil
},
})
t: t,
mockGetParameter: func(input *ssm.GetParameterInput) (*ssm.GetParameterOutput, error) {
assert.Equal(t, "/foo/bar", *input.Name)
assert.True(t, *input.WithDecryption)
calledOk = true
return &ssm.GetParameterOutput{
Parameter: &ssm.Parameter{},
}, nil
},
})

_, err := readAWSSMP(s, "/bar")
assert.True(t, calledOk)
Expand All @@ -89,28 +89,28 @@ func TestAWSSMP_GetParameterSetup(t *testing.T) {

func TestAWSSMP_GetParameterValidOutput(t *testing.T) {
s := simpleAWSSourceHelper(DummyParamGetter{
t: t,
param: &ssm.Parameter{
Name: aws.String("/foo"),
Type: aws.String("String"),
Value: aws.String("val"),
Version: aws.Int64(1),
},
})
t: t,
param: &ssm.Parameter{
Name: aws.String("/foo"),
Type: aws.String("String"),
Value: aws.String("val"),
Version: aws.Int64(1),
},
})

output, err := readAWSSMP(s, "")
assert.Nil(t, err)
expected := "{\"Name\":\"/foo\",\"Type\":\"String\",\"Value\":\"val\",\"Version\":1}"
assert.Equal(t, []byte(expected), output)
assert.Equal(t, json_mimetype, s.Type)
assert.Equal(t, jsonMimetype, s.Type)
}

func TestAWSSMP_GetParameterMissing(t *testing.T) {
expectedErr := awserr.New("ParameterNotFound", "Test of error message", nil)
s := simpleAWSSourceHelper(DummyParamGetter{
t: t,
err: expectedErr,
})
t: t,
err: expectedErr,
})

defer restoreLogFatalf()
setupMockLogFatalf()
Expand All @@ -119,5 +119,3 @@ func TestAWSSMP_GetParameterMissing(t *testing.T) {
})
assert.Contains(t, spyLogFatalfMsg, "Test of error message")
}


Loading