Skip to content

Improve code quality using gofmt and go lint #148

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

Merged
merged 1 commit into from
Aug 29, 2019
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
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ install: build
cp bin/diff $(HELM_HOME)/plugins/helm-diff/bin
cp plugin.yaml $(HELM_HOME)/plugins/helm-diff/

.PHONY: lint
lint:
scripts/update-gofmt.sh
scripts/verify-gofmt.sh
scripts/verify-golint.sh
scripts/verify-govet.sh

.PHONY: build
build:
build: lint
mkdir -p bin/
go build -i -v -o bin/diff -ldflags="$(LDFLAGS)"

Expand Down Expand Up @@ -61,7 +68,7 @@ dist:
tar -C build/ -zcvf $(CURDIR)/release/helm-diff-windows.tgz diff/

.PHONY: release
release: dist
release: lint dist
ifndef GITHUB_TOKEN
$(error GITHUB_TOKEN is undefined)
endif
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Helm Diff Plugin
[![Go Report Card](https://goreportcard.com/badge/github.com/databus23/helm-diff)](https://goreportcard.com/report/github.com/databus23/helm-diff)
[![GoDoc](https://godoc.org/github.com/databus23/helm-diff?status.svg)](https://godoc.org/github.com/databus23/helm-diff)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/databus23/helm-diff/blob/master/LICENSE)

This is a Helm plugin giving your a preview of what a `helm upgrade` would change.
Expand Down
1 change: 1 addition & 0 deletions cmd/error.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cmd

// Error to report errors
type Error struct {
error
Code int
Expand Down
16 changes: 8 additions & 8 deletions cmd/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func (v *valueFiles) Valid() error {

if errStr == "" {
return nil
} else {
return errors.New(errStr)
}

return errors.New(errStr)
}

func (v *valueFiles) Type() string {
Expand Down Expand Up @@ -148,12 +148,12 @@ func (d *diffCmd) vals() ([]byte, error) {
currentMap := map[string]interface{}{}

var bytes []byte
var err error
if strings.TrimSpace(filePath) == "-" {
bytes, err = ioutil.ReadAll(os.Stdin)
} else {
bytes, err = ioutil.ReadFile(filePath)
}
var err error
if strings.TrimSpace(filePath) == "-" {
bytes, err = ioutil.ReadAll(os.Stdin)
} else {
bytes, err = ioutil.ReadFile(filePath)
}
if err != nil {
return []byte{}, err
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const (
)

var (
settings helm_env.EnvSettings
settings helm_env.EnvSettings
// DefaultHelmHome to hold default home path of .helm dir
DefaultHelmHome = filepath.Join(homedir.HomeDir(), ".helm")
)

Expand Down Expand Up @@ -59,7 +60,7 @@ func createHelmClient() helm.Interface {
}

func expandTLSPaths() {
settings.TLSCaCertFile = os.ExpandEnv(settings.TLSCaCertFile)
settings.TLSCertFile = os.ExpandEnv(settings.TLSCertFile)
settings.TLSKeyFile = os.ExpandEnv(settings.TLSKeyFile)
settings.TLSCaCertFile = os.ExpandEnv(settings.TLSCaCertFile)
settings.TLSCertFile = os.ExpandEnv(settings.TLSCertFile)
settings.TLSKeyFile = os.ExpandEnv(settings.TLSKeyFile)
}
2 changes: 1 addition & 1 deletion cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (d *release) differentiate() error {
}

if releaseResponse1.Release.Chart.Metadata.Name == releaseResponse2.Release.Chart.Metadata.Name {
seenAnyChanges := diff.DiffReleases(
seenAnyChanges := diff.Releases(
manifest.ParseRelease(releaseResponse1.Release, d.includeTests),
manifest.ParseRelease(releaseResponse2.Release, d.includeTests),
d.suppressedKinds,
Expand Down
4 changes: 2 additions & 2 deletions cmd/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (d *revision) differentiate() error {
return prettyError(err)
}

diff.DiffManifests(
diff.Manifests(
manifest.ParseRelease(revisionResponse.Release, d.includeTests),
manifest.ParseRelease(releaseResponse.Release, d.includeTests),
d.suppressedKinds,
Expand All @@ -128,7 +128,7 @@ func (d *revision) differentiate() error {
return prettyError(err)
}

seenAnyChanges := diff.DiffManifests(
seenAnyChanges := diff.Manifests(
manifest.ParseRelease(revisionResponse1.Release, d.includeTests),
manifest.ParseRelease(revisionResponse2.Release, d.includeTests),
d.suppressedKinds,
Expand Down
2 changes: 1 addition & 1 deletion cmd/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (d *rollback) backcast() error {
}

// create a diff between the current manifest and the version of the manifest that a user is intended to rollback
seenAnyChanges := diff.DiffManifests(
seenAnyChanges := diff.Manifests(
manifest.ParseRelease(releaseResponse.Release, d.includeTests),
manifest.ParseRelease(revisionResponse.Release, d.includeTests),
d.suppressedKinds,
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The Helm Diff Plugin
helm rollback will perform.
`

// New creates a new cobra client
func New() *cobra.Command {

chartCommand := newChartCommand()
Expand Down
2 changes: 1 addition & 1 deletion cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (d *diffCmd) run() error {
}
}

seenAnyChanges := diff.DiffManifests(currentSpecs, newSpecs, d.suppressedKinds, d.outputContext, os.Stdout)
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, d.suppressedKinds, d.outputContext, os.Stdout)

if d.detailedExitCode && seenAnyChanges {
return Error{
Expand Down
9 changes: 5 additions & 4 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
"github.com/databus23/helm-diff/manifest"
)

func DiffManifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressedKinds []string, context int, to io.Writer) bool {
// Manifests diff on manifests
func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressedKinds []string, context int, to io.Writer) bool {
seenAnyChanges := false
emptyMapping := &manifest.MappingResult{}
for key, oldContent := range oldIndex {
Expand Down Expand Up @@ -52,11 +53,11 @@ func DiffManifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppre
return seenAnyChanges
}

// DiffReleases reindex the content based on the template names and pass it to DiffManifests
func DiffReleases(oldIndex, newIndex map[string]*manifest.MappingResult, suppressedKinds []string, context int, to io.Writer) bool {
// Releases reindex the content based on the template names and pass it to Manifests
func Releases(oldIndex, newIndex map[string]*manifest.MappingResult, suppressedKinds []string, context int, to io.Writer) bool {
oldIndex = reIndexForRelease(oldIndex)
newIndex = reIndexForRelease(newIndex)
return DiffManifests(oldIndex, newIndex, suppressedKinds, context, to)
return Manifests(oldIndex, newIndex, suppressedKinds, context, to)
}

func diffMappingResults(oldContent *manifest.MappingResult, newContent *manifest.MappingResult) []difflib.DiffRecord {
Expand Down
14 changes: 7 additions & 7 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ func assertDiff(t *testing.T, before, after string, context int, expected string
}
}

func TestDiffManifests(t *testing.T) {
func TestManifests(t *testing.T) {
specBeta := map[string]*manifest.MappingResult{
"default, nginx, Deployment (apps)": &manifest.MappingResult{
"default, nginx, Deployment (apps)": {

Name: "default, nginx, Deployment (apps)",
Kind: "Deployment",
Expand All @@ -142,7 +142,7 @@ metadata:
}}

specRelease := map[string]*manifest.MappingResult{
"default, nginx, Deployment (apps)": &manifest.MappingResult{
"default, nginx, Deployment (apps)": {

Name: "default, nginx, Deployment (apps)",
Kind: "Deployment",
Expand All @@ -158,8 +158,8 @@ metadata:

var buf1 bytes.Buffer

if changesSeen := DiffManifests(specBeta, specRelease, []string{}, 10, &buf1); !changesSeen {
t.Error("Unexpected return value from DiffManifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
if changesSeen := Manifests(specBeta, specRelease, []string{}, 10, &buf1); !changesSeen {
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
}

require.Equal(t, `default, nginx, Deployment (apps) has changed:
Expand All @@ -176,8 +176,8 @@ metadata:
t.Run("OnNoChange", func(t *testing.T) {
var buf2 bytes.Buffer

if changesSeen := DiffManifests(specRelease, specRelease, []string{}, 10, &buf2); changesSeen {
t.Error("Unexpected return value from DiffManifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`")
if changesSeen := Manifests(specRelease, specRelease, []string{}, 10, &buf2); changesSeen {
t.Error("Unexpected return value from Manifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`")
}

require.Equal(t, ``, buf2.String())
Expand Down
9 changes: 6 additions & 3 deletions manifest/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (

var yamlSeperator = []byte("\n---\n")

// MappingResult to store result of diff
type MappingResult struct {
Name string
Kind string
Content string
}

type metadata struct {
ApiVersion string `yaml:"apiVersion"`
APIVersion string `yaml:"apiVersion"`
Kind string
Metadata struct {
Namespace string
Expand All @@ -29,7 +30,7 @@ type metadata struct {
}

func (m metadata) String() string {
apiBase := m.ApiVersion
apiBase := m.APIVersion
sp := strings.Split(apiBase, "/")
if len(sp) > 1 {
apiBase = strings.Join(sp[:len(sp)-1], "/")
Expand Down Expand Up @@ -61,6 +62,7 @@ func splitSpec(token string) (string, string) {
return "", ""
}

// ParseRelease parses release objects into MappingResult
func ParseRelease(release *release.Release, includeTests bool) map[string]*MappingResult {
manifest := release.Manifest
for _, hook := range release.Hooks {
Expand All @@ -75,12 +77,13 @@ func ParseRelease(release *release.Release, includeTests bool) map[string]*Mappi
return Parse(manifest, release.Namespace)
}

// Parse parses manifest strings into MappingResult
func Parse(manifest string, defaultNamespace string) map[string]*MappingResult {
scanner := bufio.NewScanner(strings.NewReader(manifest))
scanner.Split(scanYamlSpecs)
//Allow for tokens (specs) up to 1M in size
scanner.Buffer(make([]byte, bufio.MaxScanTokenSize), 1048576)
//Discard the first result, we only care about everything after the first seperator
//Discard the first result, we only care about everything after the first separator
scanner.Scan()

result := make(map[string]*MappingResult)
Expand Down
2 changes: 1 addition & 1 deletion manifest/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func foundObjects(result map[string]*MappingResult) []string {
objs := make([]string, 0, len(result))
for k, _ := range result {
for k := range result {
objs = append(objs, k)
}
sort.Strings(objs)
Expand Down
16 changes: 16 additions & 0 deletions scripts/update-gofmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
# script credits : https://github.com/infracloudio/botkube

set -o errexit
set -o nounset
set -o pipefail

find_files() {
find . -not \( \
\( \
-wholename '*/vendor/*' \
\) -prune \
\) -name '*.go'
}

find_files | xargs gofmt -w -s
22 changes: 22 additions & 0 deletions scripts/verify-gofmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
# script credits : https://github.com/infracloudio/botkube

set -o errexit
set -o nounset
set -o pipefail

find_files() {
find . -not \( \
\( \
-wholename '*/vendor/*' \
\) -prune \
\) -name '*.go'
}

bad_files=$(find_files | xargs gofmt -d -s 2>&1)
if [[ -n "${bad_files}" ]]; then
echo "${bad_files}" >&2
echo >&2
echo "Run ./hack/update-gofmt.sh" >&2
exit 1
fi
20 changes: 20 additions & 0 deletions scripts/verify-golint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
# script credits : https://github.com/infracloudio/botkube

set -o errexit
set -o nounset
set -o pipefail

find_files() {
find . -not \( \
\( \
-wholename '*/vendor/*' \
\) -prune \
\) -name '*.go'
}

bad_files=$(find_files | xargs -I@ bash -c "golint @")
if [[ -n "${bad_files}" ]]; then
echo "${bad_files}"
exit 1
fi
7 changes: 7 additions & 0 deletions scripts/verify-govet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
# script credits : https://github.com/infracloudio/botkube

set -x

go vet ./pkg/...
go vet ./cmd/...