Skip to content

Commit

Permalink
Fix go lint error and add golint checks to a pre-commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Atamanenko committed Jun 10, 2018
1 parent 2b05d39 commit c994130
Show file tree
Hide file tree
Showing 18 changed files with 58 additions and 24 deletions.
5 changes: 5 additions & 0 deletions bin/pre-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ function testGoImports {
diff -u <(echo -n) <(go_dirs | xargs -0 goimports -l)
}

function testGoLint {
diff -u <(echo -n) <(go_dirs | xargs -0 golint --min_confidence 0.85 )
}

function testGoVet {
go vet -all ./...
}
Expand All @@ -49,6 +53,7 @@ function testExamples {

runTest testGoFmt
runTest testGoImports
runTest testGoLint
runTest testGoVet
runTest testGoTest
runTest testExamples
Expand Down
3 changes: 2 additions & 1 deletion pkg/app/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/kubernetes-sigs/kustomize/pkg/types"
)

// Application interface exposes methods to get resources of the application.
type Application interface {
// Resources computes and returns the resources for the app.
Resources() (resmap.ResMap, error)
Expand All @@ -57,7 +58,7 @@ type applicationImpl struct {
loader loader.Loader
}

// NewApp parses the kustomization file at the path using the loader.
// New parses the kustomization file at the path using the loader and returns application.
func New(loader loader.Loader) (Application, error) {
content, err := loader.Load(constants.KustomizationFileName)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/configmapandsecret/util/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation"
)

// handleConfigMapFromLiteralSources adds the specified literal source
// HandleConfigMapFromLiteralSources adds the specified literal source
// information into the provided configMap.
func HandleConfigMapFromLiteralSources(configMap *v1.ConfigMap, literalSources []string) error {
for _, literalSource := range literalSources {
Expand All @@ -43,7 +43,7 @@ func HandleConfigMapFromLiteralSources(configMap *v1.ConfigMap, literalSources [
return nil
}

// handleConfigMapFromFileSources adds the specified file source information
// HandleConfigMapFromFileSources adds the specified file source information
// into the provided configMap
func HandleConfigMapFromFileSources(configMap *v1.ConfigMap, fileSources []string) error {
for _, fileSource := range fileSources {
Expand Down Expand Up @@ -88,7 +88,7 @@ func HandleConfigMapFromFileSources(configMap *v1.ConfigMap, fileSources []strin
return nil
}

// handleConfigMapFromEnvFileSource adds the specified env file source information
// HandleConfigMapFromEnvFileSource adds the specified env file source information
// into the provided configMap
func HandleConfigMapFromEnvFileSource(configMap *v1.ConfigMap, envFileSource string) error {
info, err := os.Stat(envFileSource)
Expand Down
1 change: 1 addition & 0 deletions pkg/configmapandsecret/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func ParseRFC3339(s string, nowFn func() metav1.Time) (metav1.Time, error) {
return metav1.Time{Time: t}, nil
}

// HashObject encodes object using given codec and returns MD5 sum of the result.
func HashObject(obj runtime.Object, codec runtime.Codec) (string, error) {
data, err := runtime.Encode(codec, obj)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/internal/error/configmaperror.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package error

import "fmt"

// ConfigmapError represents error with a configmap.
type ConfigmapError struct {
Path string
ErrorMsg string
Expand Down
6 changes: 5 additions & 1 deletion pkg/internal/error/kustomizationerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"fmt"
)

// First pass to encapsulate fields for more informative error messages.
// KustomizationError represents an error with a kustomization.
type KustomizationError struct {
KustomizationPath string
ErrorMsg string
Expand All @@ -30,6 +30,7 @@ func (ke KustomizationError) Error() string {
return fmt.Sprintf("Kustomization File [%s]: %s\n", ke.KustomizationPath, ke.ErrorMsg)
}

// KustomizationErrors collects all errors.
type KustomizationErrors struct {
kErrors []error
}
Expand All @@ -42,14 +43,17 @@ func (ke *KustomizationErrors) Error() string {
return errormsg
}

// Append adds error to a collection of errors.
func (ke *KustomizationErrors) Append(e error) {
ke.kErrors = append(ke.kErrors, e)
}

// Get returns all collected errors.
func (ke *KustomizationErrors) Get() []error {
return ke.kErrors
}

// BatchAppend adds all errors from another KustomizationErrors
func (ke *KustomizationErrors) BatchAppend(e KustomizationErrors) {
for _, err := range e.Get() {
ke.kErrors = append(ke.kErrors, err)
Expand Down
1 change: 1 addition & 0 deletions pkg/internal/error/patcherror.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
)

// PatchError represents error during Patch.
type PatchError struct {
KustomizationPath string
PatchFilepath string
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/error/resourceerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package error

import "fmt"

// First pass to encapsulate fields for more informative error messages.
// ResourceError represents error in a resource.
type ResourceError struct {
KustomizationPath string
ResourceFilepath string
Expand Down
4 changes: 3 additions & 1 deletion pkg/internal/error/secreterror.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package error

import "fmt"

// SecretError represents error with a secret.
type SecretError struct {
KustomizationPath string
ErrorMsg string
// ErrorMsg is an error message
ErrorMsg string
}

func (e SecretError) Error() string {
Expand Down
7 changes: 5 additions & 2 deletions pkg/internal/loadertest/fakeloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,27 @@ func NewFakeLoader(initialDir string) FakeLoader {
return FakeLoader{fs: fakefs, delegate: loader}
}

// Adds a fake file to the file system.
// AddFile adds a fake file to the file system.
func (f FakeLoader) AddFile(fullFilePath string, content []byte) error {
return f.fs.WriteFile(fullFilePath, content)
}

// Adds a fake directory to the file system.
// AddDirectory adds a fake directory to the file system.
func (f FakeLoader) AddDirectory(fullDirPath string, mode os.FileMode) error {
return f.fs.Mkdir(fullDirPath, mode)
}

// Root returns root.
func (f FakeLoader) Root() string {
return f.delegate.Root()
}

// New creates a new loader from a new root.
func (f FakeLoader) New(newRoot string) (loader.Loader, error) {
return f.delegate.New(newRoot)
}

// Load performs load from a given location.
func (f FakeLoader) Load(location string) ([]byte, error) {
return f.delegate.Load(location)
}
2 changes: 1 addition & 1 deletion pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type loaderImpl struct {
schemes []SchemeLoader
}

// Interface for different types of loaders (e.g. fileLoader, httpLoader, etc.)
// SchemeLoader is the interface for different types of loaders (e.g. fileLoader, httpLoader, etc.)
type SchemeLoader interface {
// Does this location correspond to this scheme.
IsScheme(root string, location string) bool
Expand Down
9 changes: 5 additions & 4 deletions pkg/resmap/resmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,20 @@ func (m ResMap) EncodeAsYaml() ([]byte, error) {
return buf.Bytes(), nil
}

func (m1 ResMap) ErrorIfNotEqual(m2 ResMap) error {
if len(m1) != len(m2) {
// ErrorIfNotEqual returns error if maps are not equal.
func (m ResMap) ErrorIfNotEqual(m2 ResMap) error {
if len(m) != len(m2) {
keySet1 := []resource.ResId{}
keySet2 := []resource.ResId{}
for id := range m1 {
for id := range m {
keySet1 = append(keySet1, id)
}
for id := range m2 {
keySet2 = append(keySet2, id)
}
return fmt.Errorf("maps has different number of entries: %#v doesn't equals %#v", keySet1, keySet2)
}
for id, obj1 := range m1 {
for id, obj1 := range m {
obj2, found := m2[id]
if !found {
return fmt.Errorf("%#v doesn't exist in %#v", id, m2)
Expand Down
8 changes: 4 additions & 4 deletions pkg/resource/generationbehavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ package resource
type GenerationBehavior int

const (
// Unspecified behavior typically treated as a Create.
// BehaviorUnspecified is an Unspecified behavior; typically treated as a Create.
BehaviorUnspecified GenerationBehavior = iota
// Make a new resource.
// BehaviorCreate makes a new resource.
BehaviorCreate
// Replace a resource.
// BehaviorReplace replaces a resource.
BehaviorReplace
// Attempt to merge a new resource with an existing resource.
// BehaviorMerge attempts to merge a new resource with an existing resource.
BehaviorMerge
)

Expand Down
3 changes: 3 additions & 0 deletions pkg/resource/resid.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type ResId struct {
name string
}

// NewResId creates new resource identifier
func NewResId(g schema.GroupVersionKind, n string) ResId {
return ResId{gvk: g, name: n}
}
Expand All @@ -41,10 +42,12 @@ func (n ResId) String() string {
return strings.Join([]string{n.gvk.Group, n.gvk.Version, n.gvk.Kind, n.name}, "_") + ".yaml"
}

// Gvk returns Group/Version/Kind of the resource.
func (n ResId) Gvk() schema.GroupVersionKind {
return n.gvk
}

// Name returns resource name.
func (n ResId) Name() string {
return n.name
}
3 changes: 3 additions & 0 deletions pkg/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ func (r *Resource) Id() ResId {
return NewResId(r.GroupVersionKind(), r.GetName())
}

// Merge performs merge with other resource.
func (r *Resource) Merge(other *Resource) {
r.Replace(other)
mergeConfigmap(r.Object, other.Object, r.Object)
}

// Replace performs replace with other resource.
func (r *Resource) Replace(other *Resource) {
r.SetLabels(mergeStringMaps(other.GetLabels(), r.GetLabels()))
r.SetAnnotations(mergeStringMaps(other.GetAnnotations(), r.GetAnnotations()))
Expand Down Expand Up @@ -103,6 +105,7 @@ func mergeStringMaps(maps ...map[string]string) map[string]string {
return result
}

// GetFieldValue returns value at the given fieldpath.
func (r *Resource) GetFieldValue(fieldPath string) (string, error) {
return getFieldValue(r.UnstructuredContent(), strings.Split(fieldPath, "."))
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/types/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type Kustomization struct {
Vars []Var `json:"vars,omitempty" yaml:"vars,omitempty"`
}

// ConfigMapArg contains the metadata of how to generate a configmap.
// ConfigMapArgs contains the metadata of how to generate a configmap.
type ConfigMapArgs struct {
// Name of the configmap.
// The full name should be Kustomization.NamePrefix + Configmap.Name +
Expand All @@ -84,7 +84,7 @@ type ConfigMapArgs struct {
DataSources `json:",inline,omitempty" yaml:",inline,omitempty"`
}

// SecretGenerator contains the metadata of how to generate a secret.
// SecretArgs contains the metadata of how to generate a secret.
type SecretArgs struct {
// Name of the secret.
// The full name should be Kustomization.NamePrefix + SecretGenerator.Name +
Expand Down
1 change: 1 addition & 0 deletions pkg/types/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Var struct {
FieldRef corev1.ObjectFieldSelector `json:"fieldref,omitempty" yaml:"objref,omitempty"`
}

// Defaulting sets reference to field used by default.
func (v *Var) Defaulting() {
if (corev1.ObjectFieldSelector{}) == v.FieldRef {
v.FieldRef = corev1.ObjectFieldSelector{FieldPath: "metadata.name"}
Expand Down
16 changes: 12 additions & 4 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ var (
buildDate = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
)

// Version represens kustomize version.
type Version struct {
// KustomizeVersion is a kustomize binary version.
KustomizeVersion string `json:"kustomizeVersion"`
GitCommit string `json:"gitCommit"`
BuildDate string `json:"buildDate"`
GoOs string `json:"goOs"`
GoArch string `json:"goArch"`
// GitCommit is a git commit
GitCommit string `json:"gitCommit"`
// BuildDate is a build date of the binary.
BuildDate string `json:"buildDate"`
// GoOs holds OS name.
GoOs string `json:"goOs"`
// GoArch holds architecture name.
GoArch string `json:"goArch"`
}

// GetVersion returns version.
func GetVersion() Version {
return Version{
kustomizeVersion,
Expand All @@ -50,6 +57,7 @@ func GetVersion() Version {
}
}

// Print prints version.
func (v Version) Print(w io.Writer) {
fmt.Fprintf(w, "Version: %+v\n", v)
}
Expand Down

0 comments on commit c994130

Please sign in to comment.