Merge pull request #113 from uthark/unparam

Enable unparam linter and fix found issues
This commit is contained in:
Jingfang Liu
2018-06-18 11:55:57 -07:00
committed by GitHub
18 changed files with 57 additions and 64 deletions

View File

@@ -49,6 +49,7 @@ function testGoMetalinter {
--enable=goimports \ --enable=goimports \
--enable=varcheck \ --enable=varcheck \
--enable=goconst \ --enable=goconst \
--enable=unparam \
--enable=ineffassign \ --enable=ineffassign \
--enable=nakedret \ --enable=nakedret \
--enable=interfacer \ --enable=interfacer \

View File

@@ -19,7 +19,6 @@ package commands
import ( import (
"errors" "errors"
"fmt" "fmt"
"io"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -32,7 +31,7 @@ type addPatchOptions struct {
} }
// newCmdAddPatch adds the name of a file containing a patch to the kustomization file. // newCmdAddPatch adds the name of a file containing a patch to the kustomization file.
func newCmdAddPatch(out, errOut io.Writer, fsys fs.FileSystem) *cobra.Command { func newCmdAddPatch(fsys fs.FileSystem) *cobra.Command {
var o addPatchOptions var o addPatchOptions
cmd := &cobra.Command{ cmd := &cobra.Command{
@@ -49,7 +48,7 @@ func newCmdAddPatch(out, errOut io.Writer, fsys fs.FileSystem) *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
return o.RunAddPatch(out, errOut, fsys) return o.RunAddPatch(fsys)
}, },
} }
return cmd return cmd
@@ -70,7 +69,7 @@ func (o *addPatchOptions) Complete(cmd *cobra.Command, args []string) error {
} }
// RunAddPatch runs addPatch command (do real work). // RunAddPatch runs addPatch command (do real work).
func (o *addPatchOptions) RunAddPatch(out, errOut io.Writer, fsys fs.FileSystem) error { func (o *addPatchOptions) RunAddPatch(fsys fs.FileSystem) error {
_, err := fsys.Stat(o.patchFilePath) _, err := fsys.Stat(o.patchFilePath)
if err != nil { if err != nil {
return err return err

View File

@@ -17,8 +17,6 @@ limitations under the License.
package commands package commands
import ( import (
"bytes"
"os"
"testing" "testing"
"strings" "strings"
@@ -36,12 +34,11 @@ sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
) )
func TestAddPatchHappyPath(t *testing.T) { func TestAddPatchHappyPath(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
fakeFS := fs.MakeFakeFS() fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(patchFileName, []byte(patchFileContent)) fakeFS.WriteFile(patchFileName, []byte(patchFileContent))
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent)) fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
cmd := newCmdAddPatch(buf, os.Stderr, fakeFS) cmd := newCmdAddPatch(fakeFS)
args := []string{patchFileName} args := []string{patchFileName}
err := cmd.RunE(cmd, args) err := cmd.RunE(cmd, args)
if err != nil { if err != nil {
@@ -57,12 +54,11 @@ func TestAddPatchHappyPath(t *testing.T) {
} }
func TestAddPatchAlreadyThere(t *testing.T) { func TestAddPatchAlreadyThere(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
fakeFS := fs.MakeFakeFS() fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(patchFileName, []byte(patchFileContent)) fakeFS.WriteFile(patchFileName, []byte(patchFileContent))
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent)) fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
cmd := newCmdAddPatch(buf, os.Stderr, fakeFS) cmd := newCmdAddPatch(fakeFS)
args := []string{patchFileName} args := []string{patchFileName}
err := cmd.RunE(cmd, args) err := cmd.RunE(cmd, args)
if err != nil { if err != nil {
@@ -80,10 +76,9 @@ func TestAddPatchAlreadyThere(t *testing.T) {
} }
func TestAddPatchNoArgs(t *testing.T) { func TestAddPatchNoArgs(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
fakeFS := fs.MakeFakeFS() fakeFS := fs.MakeFakeFS()
cmd := newCmdAddPatch(buf, os.Stderr, fakeFS) cmd := newCmdAddPatch(fakeFS)
err := cmd.Execute() err := cmd.Execute()
if err == nil { if err == nil {
t.Errorf("expected error: %v", err) t.Errorf("expected error: %v", err)

View File

@@ -19,7 +19,6 @@ package commands
import ( import (
"errors" "errors"
"fmt" "fmt"
"io"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -32,7 +31,7 @@ type addResourceOptions struct {
} }
// newCmdAddResource adds the name of a file containing a resource to the kustomization file. // newCmdAddResource adds the name of a file containing a resource to the kustomization file.
func newCmdAddResource(out, errOut io.Writer, fsys fs.FileSystem) *cobra.Command { func newCmdAddResource(fsys fs.FileSystem) *cobra.Command {
var o addResourceOptions var o addResourceOptions
cmd := &cobra.Command{ cmd := &cobra.Command{
@@ -49,7 +48,7 @@ func newCmdAddResource(out, errOut io.Writer, fsys fs.FileSystem) *cobra.Command
if err != nil { if err != nil {
return err return err
} }
return o.RunAddResource(out, errOut, fsys) return o.RunAddResource(fsys)
}, },
} }
return cmd return cmd
@@ -70,7 +69,7 @@ func (o *addResourceOptions) Complete(cmd *cobra.Command, args []string) error {
} }
// RunAddResource runs addResource command (do real work). // RunAddResource runs addResource command (do real work).
func (o *addResourceOptions) RunAddResource(out, errOut io.Writer, fsys fs.FileSystem) error { func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error {
_, err := fsys.Stat(o.resourceFilePath) _, err := fsys.Stat(o.resourceFilePath)
if err != nil { if err != nil {
return err return err

View File

@@ -17,8 +17,6 @@ limitations under the License.
package commands package commands
import ( import (
"bytes"
"os"
"testing" "testing"
"strings" "strings"
@@ -52,12 +50,11 @@ secretGenerator: []
) )
func TestAddResourceHappyPath(t *testing.T) { func TestAddResourceHappyPath(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
fakeFS := fs.MakeFakeFS() fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent)) fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent)) fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
cmd := newCmdAddResource(buf, os.Stderr, fakeFS) cmd := newCmdAddResource(fakeFS)
args := []string{resourceFileName} args := []string{resourceFileName}
err := cmd.RunE(cmd, args) err := cmd.RunE(cmd, args)
if err != nil { if err != nil {
@@ -73,12 +70,11 @@ func TestAddResourceHappyPath(t *testing.T) {
} }
func TestAddResourceAlreadyThere(t *testing.T) { func TestAddResourceAlreadyThere(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
fakeFS := fs.MakeFakeFS() fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent)) fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent)) fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
cmd := newCmdAddResource(buf, os.Stderr, fakeFS) cmd := newCmdAddResource(fakeFS)
args := []string{resourceFileName} args := []string{resourceFileName}
err := cmd.RunE(cmd, args) err := cmd.RunE(cmd, args)
if err != nil { if err != nil {
@@ -96,10 +92,9 @@ func TestAddResourceAlreadyThere(t *testing.T) {
} }
func TestAddResourceNoArgs(t *testing.T) { func TestAddResourceNoArgs(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
fakeFS := fs.MakeFakeFS() fakeFS := fs.MakeFakeFS()
cmd := newCmdAddResource(buf, os.Stderr, fakeFS) cmd := newCmdAddResource(fakeFS)
err := cmd.Execute() err := cmd.Execute()
if err == nil { if err == nil {
t.Errorf("expected error: %v", err) t.Errorf("expected error: %v", err)

View File

@@ -35,7 +35,7 @@ type buildOptions struct {
} }
// newCmdBuild creates a new build command. // newCmdBuild creates a new build command.
func newCmdBuild(out, errOut io.Writer, fs fs.FileSystem) *cobra.Command { func newCmdBuild(out io.Writer, fs fs.FileSystem) *cobra.Command {
var o buildOptions var o buildOptions
cmd := &cobra.Command{ cmd := &cobra.Command{
@@ -48,7 +48,7 @@ func newCmdBuild(out, errOut io.Writer, fs fs.FileSystem) *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
return o.RunBuild(out, errOut, fs) return o.RunBuild(out, fs)
}, },
} }
return cmd return cmd
@@ -68,7 +68,7 @@ func (o *buildOptions) Validate(args []string) error {
} }
// RunBuild runs build command. // RunBuild runs build command.
func (o *buildOptions) RunBuild(out, errOut io.Writer, fs fs.FileSystem) error { func (o *buildOptions) RunBuild(out io.Writer, fs fs.FileSystem) error {
l := loader.Init([]loader.SchemeLoader{loader.NewFileLoader(fs)}) l := loader.Init([]loader.SchemeLoader{loader.NewFileLoader(fs)})
absPath, err := filepath.Abs(o.kustomizationPath) absPath, err := filepath.Abs(o.kustomizationPath)

View File

@@ -125,7 +125,7 @@ func runBuildTestCase(t *testing.T, testcaseName string, updateKustomizeExpected
kustomizationPath: testcase.Filename, kustomizationPath: testcase.Filename,
} }
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
err = ops.RunBuild(buf, os.Stderr, fs) err = ops.RunBuild(buf, fs)
switch { switch {
case err != nil && len(testcase.ExpectedError) == 0: case err != nil && len(testcase.ExpectedError) == 0:
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)

View File

@@ -19,7 +19,6 @@ package commands
import ( import (
"flag" "flag"
"io"
"os" "os"
"github.com/kubernetes-sigs/kustomize/pkg/fs" "github.com/kubernetes-sigs/kustomize/pkg/fs"
@@ -42,9 +41,10 @@ See https://github.com/kubernetes-sigs/kustomize
} }
c.AddCommand( c.AddCommand(
newCmdBuild(stdOut, stdErr, fsys), // TODO: Make consistent API for newCmd* functions.
newCmdBuild(stdOut, fsys),
newCmdDiff(stdOut, stdErr, fsys), newCmdDiff(stdOut, stdErr, fsys),
newCmdEdit(stdOut, stdErr, fsys), newCmdEdit(fsys),
newCmdVersion(stdOut), newCmdVersion(stdOut),
) )
c.PersistentFlags().AddGoFlagSet(flag.CommandLine) c.PersistentFlags().AddGoFlagSet(flag.CommandLine)
@@ -56,7 +56,7 @@ See https://github.com/kubernetes-sigs/kustomize
} }
// newCmdEdit returns an instance of 'edit' subcommand. // newCmdEdit returns an instance of 'edit' subcommand.
func newCmdEdit(stdOut, stdErr io.Writer, fsys fs.FileSystem) *cobra.Command { func newCmdEdit(fsys fs.FileSystem) *cobra.Command {
c := &cobra.Command{ c := &cobra.Command{
Use: "edit", Use: "edit",
Short: "Edits a kustomization file", Short: "Edits a kustomization file",
@@ -71,14 +71,14 @@ func newCmdEdit(stdOut, stdErr io.Writer, fsys fs.FileSystem) *cobra.Command {
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
} }
c.AddCommand( c.AddCommand(
newCmdAdd(stdOut, stdErr, fsys), newCmdAdd(fsys),
newCmdSet(stdOut, stdErr, fsys), newCmdSet(fsys),
) )
return c return c
} }
// newAddCommand returns an instance of 'add' subcommand. // newAddCommand returns an instance of 'add' subcommand.
func newCmdAdd(stdOut, stdErr io.Writer, fsys fs.FileSystem) *cobra.Command { func newCmdAdd(fsys fs.FileSystem) *cobra.Command {
c := &cobra.Command{ c := &cobra.Command{
Use: "add", Use: "add",
Short: "Adds configmap/resource/patch to the kustomization file.", Short: "Adds configmap/resource/patch to the kustomization file.",
@@ -96,15 +96,15 @@ func newCmdAdd(stdOut, stdErr io.Writer, fsys fs.FileSystem) *cobra.Command {
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
} }
c.AddCommand( c.AddCommand(
newCmdAddResource(stdOut, stdErr, fsys), newCmdAddResource(fsys),
newCmdAddPatch(stdOut, stdErr, fsys), newCmdAddPatch(fsys),
newCmdAddConfigMap(stdErr, fsys), newCmdAddConfigMap(fsys),
) )
return c return c
} }
// newSetCommand returns an instance of 'set' subcommand. // newSetCommand returns an instance of 'set' subcommand.
func newCmdSet(stdOut, stdErr io.Writer, fsys fs.FileSystem) *cobra.Command { func newCmdSet(fsys fs.FileSystem) *cobra.Command {
c := &cobra.Command{ c := &cobra.Command{
Use: "set", Use: "set",
Short: "Sets the value of different fields in kustomization file.", Short: "Sets the value of different fields in kustomization file.",
@@ -117,7 +117,7 @@ func newCmdSet(stdOut, stdErr io.Writer, fsys fs.FileSystem) *cobra.Command {
} }
c.AddCommand( c.AddCommand(
newCmdSetNamePrefix(stdOut, stdErr, fsys), newCmdSetNamePrefix(fsys),
) )
return c return c
} }

View File

@@ -18,7 +18,6 @@ package commands
import ( import (
"fmt" "fmt"
"io"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -28,7 +27,7 @@ import (
"github.com/kubernetes-sigs/kustomize/pkg/types" "github.com/kubernetes-sigs/kustomize/pkg/types"
) )
func newCmdAddConfigMap(errOut io.Writer, fsys fs.FileSystem) *cobra.Command { func newCmdAddConfigMap(fsys fs.FileSystem) *cobra.Command {
var config dataConfig var config dataConfig
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1]", Use: "configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1]",

View File

@@ -24,7 +24,7 @@ import (
) )
func TestNewAddConfigMapIsNotNil(t *testing.T) { func TestNewAddConfigMapIsNotNil(t *testing.T) {
if newCmdAddConfigMap(nil, fs.MakeFakeFS()) == nil { if newCmdAddConfigMap(fs.MakeFakeFS()) == nil {
t.Fatal("newCmdAddConfigMap shouldn't be nil") t.Fatal("newCmdAddConfigMap shouldn't be nil")
} }
} }

View File

@@ -42,7 +42,7 @@ func newCmdDiff(out, errOut io.Writer, fs fs.FileSystem) *cobra.Command {
Use: "diff [path]", Use: "diff [path]",
Short: "diff between customized resources and uncustomized resources", Short: "diff between customized resources and uncustomized resources",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
err := o.Validate(cmd, args) err := o.Validate(args)
if err != nil { if err != nil {
return err return err
} }
@@ -53,7 +53,7 @@ func newCmdDiff(out, errOut io.Writer, fs fs.FileSystem) *cobra.Command {
} }
// Validate validates diff command. // Validate validates diff command.
func (o *diffOptions) Validate(cmd *cobra.Command, args []string) error { func (o *diffOptions) Validate(args []string) error {
if len(args) > 1 { if len(args) > 1 {
return errors.New("specify one path to " + constants.KustomizationFileName) return errors.New("specify one path to " + constants.KustomizationFileName)
} }

View File

@@ -35,7 +35,7 @@ type kustomizationFile struct {
fsys fs.FileSystem fsys fs.FileSystem
} }
func newKustomizationFile(mPath string, fsys fs.FileSystem) (*kustomizationFile, error) { func newKustomizationFile(mPath string, fsys fs.FileSystem) (*kustomizationFile, error) { // nolint
mf := &kustomizationFile{path: mPath, fsys: fsys} mf := &kustomizationFile{path: mPath, fsys: fsys}
err := mf.validate() err := mf.validate()
if err != nil { if err != nil {

View File

@@ -18,7 +18,6 @@ package commands
import ( import (
"errors" "errors"
"io"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -31,7 +30,7 @@ type setNamePrefixOptions struct {
} }
// newCmdSetNamePrefix sets the value of the namePrefix field in the kustomization. // newCmdSetNamePrefix sets the value of the namePrefix field in the kustomization.
func newCmdSetNamePrefix(out, errOut io.Writer, fsys fs.FileSystem) *cobra.Command { func newCmdSetNamePrefix(fsys fs.FileSystem) *cobra.Command {
var o setNamePrefixOptions var o setNamePrefixOptions
cmd := &cobra.Command{ cmd := &cobra.Command{
@@ -52,7 +51,7 @@ and overwrite the value with "acme-" if the field does exist.
if err != nil { if err != nil {
return err return err
} }
return o.RunSetNamePrefix(out, errOut, fsys) return o.RunSetNamePrefix(fsys)
}, },
} }
return cmd return cmd
@@ -74,7 +73,7 @@ func (o *setNamePrefixOptions) Complete(cmd *cobra.Command, args []string) error
} }
// RunSetNamePrefix runs setNamePrefix command (does real work). // RunSetNamePrefix runs setNamePrefix command (does real work).
func (o *setNamePrefixOptions) RunSetNamePrefix(out, errOut io.Writer, fsys fs.FileSystem) error { func (o *setNamePrefixOptions) RunSetNamePrefix(fsys fs.FileSystem) error {
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys) mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
if err != nil { if err != nil {
return err return err

View File

@@ -17,8 +17,6 @@ limitations under the License.
package commands package commands
import ( import (
"bytes"
"os"
"testing" "testing"
"strings" "strings"
@@ -32,11 +30,10 @@ const (
) )
func TestSetNamePrefixHappyPath(t *testing.T) { func TestSetNamePrefixHappyPath(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
fakeFS := fs.MakeFakeFS() fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent)) fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
cmd := newCmdSetNamePrefix(buf, os.Stderr, fakeFS) cmd := newCmdSetNamePrefix(fakeFS)
args := []string{goodPrefixValue} args := []string{goodPrefixValue}
err := cmd.RunE(cmd, args) err := cmd.RunE(cmd, args)
if err != nil { if err != nil {
@@ -52,10 +49,9 @@ func TestSetNamePrefixHappyPath(t *testing.T) {
} }
func TestSetNamePrefixNoArgs(t *testing.T) { func TestSetNamePrefixNoArgs(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
fakeFS := fs.MakeFakeFS() fakeFS := fs.MakeFakeFS()
cmd := newCmdSetNamePrefix(buf, os.Stderr, fakeFS) cmd := newCmdSetNamePrefix(fakeFS)
err := cmd.Execute() err := cmd.Execute()
if err == nil { if err == nil {
t.Errorf("expected error: %v", err) t.Errorf("expected error: %v", err)

View File

@@ -234,10 +234,10 @@ func TestObjectConvertToUnstructured(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
actual, err := objectToUnstructured(tc.input) actual, err := objectToUnstructured(tc.input)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("%s: unexpected error: %v", tc.description, err)
} }
if !reflect.DeepEqual(actual, tc.expected) { if !reflect.DeepEqual(actual, tc.expected) {
t.Fatalf("%#v\ndoesn't match expected\n%#v\n", actual, tc.expected) t.Fatalf("%s: %#v\ndoesn't match expected\n%#v\n", tc.description, actual, tc.expected)
} }
} }
} }

View File

@@ -30,7 +30,7 @@ import (
) )
// ParseRFC3339 parses an RFC3339 date in either RFC3339Nano or RFC3339 format. // ParseRFC3339 parses an RFC3339 date in either RFC3339Nano or RFC3339 format.
func ParseRFC3339(s string, nowFn func() metav1.Time) (metav1.Time, error) { func ParseRFC3339(s string) (metav1.Time, error) {
if t, timeErr := time.Parse(time.RFC3339Nano, s); timeErr == nil { if t, timeErr := time.Parse(time.RFC3339Nano, s); timeErr == nil {
return metav1.Time{Time: t}, nil return metav1.Time{Time: t}, nil
} }

View File

@@ -26,18 +26,22 @@ import (
) )
// RunDiff runs system diff program to compare two Maps. // RunDiff runs system diff program to compare two Maps.
func RunDiff(raw, transformed resmap.ResMap, out, errOut io.Writer) error { func RunDiff(raw, transformed resmap.ResMap, out, errOut io.Writer) (err error) {
transformedDir, err := writeYamlToNewDir(transformed, "transformed") transformedDir, err := writeYamlToNewDir(transformed, "transformed")
if err != nil { if err != nil {
return err return err
} }
defer transformedDir.delete() defer func() {
err = transformedDir.delete()
}()
noopDir, err := writeYamlToNewDir(raw, "noop") noopDir, err := writeYamlToNewDir(raw, "noop")
if err != nil { if err != nil {
return err return err
} }
defer noopDir.delete() defer func() {
err = noopDir.delete()
}()
return newProgram(out, errOut).run(noopDir.name(), transformedDir.name()) return newProgram(out, errOut).run(noopDir.name(), transformedDir.name())
} }

View File

@@ -43,10 +43,16 @@ func (o *nameHashTransformer) Transform(m resmap.ResMap) error {
for id, res := range m { for id, res := range m {
switch { switch {
case selectByGVK(id.Gvk(), &schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}): case selectByGVK(id.Gvk(), &schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}):
appendHashForConfigMap(res) err := appendHashForConfigMap(res)
if err != nil {
return err
}
case selectByGVK(id.Gvk(), &schema.GroupVersionKind{Version: "v1", Kind: "Secret"}): case selectByGVK(id.Gvk(), &schema.GroupVersionKind{Version: "v1", Kind: "Secret"}):
appendHashForSecret(res) err := appendHashForSecret(res)
if err != nil {
return err
}
} }
} }
return nil return nil