Improve command package isolation.

This commit is contained in:
Jeffrey Regan
2018-10-03 15:04:57 -07:00
parent f5fee4decf
commit bb9fafa6cc
104 changed files with 301 additions and 212 deletions

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package build
import (
"errors"
@@ -57,8 +57,8 @@ Use different transformer configurations by passing files to kustomize
build somedir -t some-transformer-configfile,another-transformer-configfile
`
// newCmdBuild creates a new build command.
func newCmdBuild(
// NewCmdBuild creates a new build command.
func NewCmdBuild(
out io.Writer, fs fs.FileSystem,
decoder ifc.Decoder) *cobra.Command {
var o buildOptions

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package build
import (
"bytes"
@@ -22,12 +22,12 @@ import (
"os"
"path/filepath"
"reflect"
"sigs.k8s.io/kustomize/internal/k8sdeps"
"strings"
"testing"
"github.com/ghodss/yaml"
"sigs.k8s.io/kustomize/internal/k8sdeps"
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
)
@@ -99,7 +99,7 @@ func TestBuild(t *testing.T) {
return nil
})
// sanity check that we found the right folder
if !stringInSlice("simple", testcases) {
if !kustfile.StringInSlice("simple", testcases) {
t.Fatalf("Error locating testcases")
}

View File

@@ -1,5 +1,5 @@
description: simple
args: []
filename: ../examplelayout/simple/instances/exampleinstance/
filename: ../../examplelayout/simple/instances/exampleinstance/
expectedStdout: testdata/testcase-simple/expected.yaml
expectedDiff: testdata/testcase-simple/expected.diff

View File

@@ -22,6 +22,9 @@ import (
"os"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/build"
"sigs.k8s.io/kustomize/pkg/commands/edit"
"sigs.k8s.io/kustomize/pkg/commands/misc"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
)
@@ -44,10 +47,10 @@ See https://sigs.k8s.io/kustomize
c.AddCommand(
// TODO: Make consistent API for newCmd* functions.
newCmdBuild(stdOut, fsys, decoder),
newCmdEdit(fsys, validator),
newCmdConfig(fsys),
newCmdVersion(stdOut),
build.NewCmdBuild(stdOut, fsys, decoder),
edit.NewCmdEdit(fsys, validator),
misc.NewCmdConfig(fsys),
misc.NewCmdVersion(stdOut),
)
c.PersistentFlags().AddGoFlagSet(flag.CommandLine)
@@ -56,85 +59,3 @@ See https://sigs.k8s.io/kustomize
flag.CommandLine.Parse([]string{})
return c
}
// newCmdEdit returns an instance of 'edit' subcommand.
func newCmdEdit(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
c := &cobra.Command{
Use: "edit",
Short: "Edits a kustomization file",
Long: "",
Example: `
# Adds a configmap to the kustomization file
kustomize edit add configmap NAME --from-literal=k=v
# Sets the nameprefix field
kustomize edit set nameprefix <prefix-value>
`,
Args: cobra.MinimumNArgs(1),
}
c.AddCommand(
newCmdAdd(fsys, v),
newCmdSet(fsys, v),
)
return c
}
// newAddCommand returns an instance of 'add' subcommand.
func newCmdAdd(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
c := &cobra.Command{
Use: "add",
Short: "Adds configmap/resource/patch/base to the kustomization file.",
Long: "",
Example: `
# Adds a configmap to the kustomization file
kustomize edit add configmap NAME --from-literal=k=v
# Adds a resource to the kustomization
kustomize edit add resource <filepath>
# Adds a patch to the kustomization
kustomize edit add patch <filepath>
# Adds one or more base directories to the kustomization
kustomize edit add base <filepath>
kustomize edit add base <filepath1>,<filepath2>,<filepath3>
# Adds one or more commonLabels to the kustomization
kustomize edit add label {labelKey1:labelValue1},{labelKey2:labelValue2}
# Adds one or more commonAnnotations to the kustomization
kustomize edit add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}
`,
Args: cobra.MinimumNArgs(1),
}
c.AddCommand(
newCmdAddResource(fsys),
newCmdAddPatch(fsys),
newCmdAddConfigMap(fsys),
newCmdAddBase(fsys),
newCmdAddLabel(fsys, v.MakeLabelValidator()),
newCmdAddAnnotation(fsys, v.MakeAnnotationValidator()),
)
return c
}
// newSetCommand returns an instance of 'set' subcommand.
func newCmdSet(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
c := &cobra.Command{
Use: "set",
Short: "Sets the value of different fields in kustomization file.",
Long: "",
Example: `
# Sets the nameprefix field
kustomize edit set nameprefix <prefix-value>
`,
Args: cobra.MinimumNArgs(1),
}
c.AddCommand(
newCmdSetNamePrefix(fsys),
newCmdSetNamespace(fsys, v),
newCmdSetImageTag(fsys),
)
return c
}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package add
import (
"errors"
@@ -22,7 +22,7 @@ import (
"strings"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
)
@@ -71,12 +71,12 @@ func (o *addBaseOptions) Complete(cmd *cobra.Command, args []string) error {
// RunAddBase runs addBase command (do real work).
func (o *addBaseOptions) RunAddBase(fsys fs.FileSystem) error {
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys)
if err != nil {
return err
}
m, err := mf.read()
m, err := mf.Read()
if err != nil {
return err
}
@@ -87,12 +87,12 @@ func (o *addBaseOptions) RunAddBase(fsys fs.FileSystem) error {
if !fsys.Exists(path) {
return errors.New(path + " does not exist")
}
if stringInSlice(path, m.Bases) {
if kustfile.StringInSlice(path, m.Bases) {
return fmt.Errorf("base %s already in kustomization file", path)
}
m.Bases = append(m.Bases, path)
}
return mf.write(m)
return mf.Write(m)
}

View File

@@ -14,13 +14,13 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package add
import (
"strings"
"testing"
"strings"
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
)
@@ -35,7 +35,7 @@ func TestAddBaseHappyPath(t *testing.T) {
for _, base := range bases {
fakeFS.Mkdir(base)
}
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
cmd := newCmdAddBase(fakeFS)
args := []string{baseDirectoryPaths}
@@ -62,7 +62,7 @@ func TestAddBaseAlreadyThere(t *testing.T) {
for _, base := range bases {
fakeFS.Mkdir(base)
}
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
cmd := newCmdAddBase(fakeFS)
args := []string{baseDirectoryPaths}
@@ -79,7 +79,7 @@ func TestAddBaseAlreadyThere(t *testing.T) {
for _, base := range bases {
msg := "base " + base + " already in kustomization file"
expectedErrors = append(expectedErrors, msg)
if !stringInSlice(msg, expectedErrors) {
if !kustfile.StringInSlice(msg, expectedErrors) {
t.Errorf("unexpected error %v", err)
}
}

View File

@@ -14,13 +14,14 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package add
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/types"
@@ -91,11 +92,11 @@ func (o *addMetadataOptions) runE(
if err != nil {
return err
}
kf, err := newKustomizationFile(constants.KustomizationFileName, fSys)
kf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fSys)
if err != nil {
return err
}
m, err := kf.read()
m, err := kf.Read()
if err != nil {
return err
}
@@ -103,7 +104,7 @@ func (o *addMetadataOptions) runE(
if err != nil {
return err
}
return kf.write(m)
return kf.Write(m)
}
// validateAndParse validates `add` commands and parses them into o.metadata

View File

@@ -14,11 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package add
import (
"testing"
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/types"
@@ -27,12 +28,12 @@ import (
func makeKustomization(t *testing.T) *types.Kustomization {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
kf, err := newKustomizationFile(constants.KustomizationFileName, fakeFS)
fakeFS.WriteTestKustomization()
kf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fakeFS)
if err != nil {
t.Errorf("unexpected new error %v", err)
}
m, err := kf.read()
m, err := kf.Read()
if err != nil {
t.Errorf("unexpected read error %v", err)
}
@@ -92,7 +93,7 @@ func TestAddAnnotationInvalidFormat(t *testing.T) {
func TestAddAnnotationManyArgs(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
v := validators.MakeHappyMapValidator(t)
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
args := []string{"k1:v1,k2:v2,k3:v3,k4:v5"}
@@ -135,7 +136,7 @@ func TestAddAnnotationTooManyColons(t *testing.T) {
func TestAddAnnotationNoValue(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
v := validators.MakeHappyMapValidator(t)
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
args := []string{"no:,value"}
@@ -148,7 +149,7 @@ func TestAddAnnotationNoValue(t *testing.T) {
func TestAddAnnotationMultipleArgs(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
v := validators.MakeHappyMapValidator(t)
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
args := []string{"this:annotation", "has:spaces"}
@@ -245,7 +246,7 @@ func TestAddLabelTooManyColons(t *testing.T) {
func TestAddLabelNoValue(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
v := validators.MakeHappyMapValidator(t)
cmd := newCmdAddLabel(fakeFS, v.Validator)
args := []string{"no,value:"}
@@ -258,7 +259,7 @@ func TestAddLabelNoValue(t *testing.T) {
func TestAddLabelMultipleArgs(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
v := validators.MakeHappyMapValidator(t)
cmd := newCmdAddLabel(fakeFS, v.Validator)
args := []string{"this:input", "has:spaces"}

View File

@@ -14,14 +14,14 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package add
import (
"errors"
"log"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/patch"
@@ -79,23 +79,23 @@ func (o *addPatchOptions) RunAddPatch(fsys fs.FileSystem) error {
return nil
}
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys)
if err != nil {
return err
}
m, err := mf.read()
m, err := mf.Read()
if err != nil {
return err
}
for _, p := range patches {
if patch.Exist(m.PatchesStrategicMerge, p) || stringInSlice(p, m.Patches) {
if patch.Exist(m.PatchesStrategicMerge, p) || kustfile.StringInSlice(p, m.Patches) {
log.Printf("patch %s already in kustomization file", p)
continue
}
m.PatchesStrategicMerge = patch.Append(m.PatchesStrategicMerge, p)
}
return mf.write(m)
return mf.Write(m)
}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package add
import (
"testing"
@@ -37,7 +37,7 @@ func TestAddPatchHappyPath(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(patchFileName, []byte(patchFileContent))
fakeFS.WriteFile(patchFileName+"another", []byte(patchFileContent))
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
cmd := newCmdAddPatch(fakeFS)
args := []string{patchFileName + "*"}
@@ -60,7 +60,7 @@ func TestAddPatchHappyPath(t *testing.T) {
func TestAddPatchAlreadyThere(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(patchFileName, []byte(patchFileContent))
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
cmd := newCmdAddPatch(fakeFS)
args := []string{patchFileName}

View File

@@ -14,14 +14,14 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package add
import (
"errors"
"log"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
)
@@ -78,23 +78,23 @@ func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error {
return nil
}
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys)
if err != nil {
return err
}
m, err := mf.read()
m, err := mf.Read()
if err != nil {
return err
}
for _, resource := range resources {
if stringInSlice(resource, m.Resources) {
if kustfile.StringInSlice(resource, m.Resources) {
log.Printf("resource %s already in kustomization file", resource)
continue
}
m.Resources = append(m.Resources, resource)
}
return mf.write(m)
return mf.Write(m)
}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package add
import (
"strings"
@@ -29,22 +29,6 @@ const (
resourceFileContent = `
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
`
kustomizationContent = `namePrefix: some-prefix
# Labels to add to all objects and selectors.
# These labels would also be used to form the selector for apply --prune
# Named differently than labels to avoid confusion with metadata for this object
commonLabels:
app: helloworld
commonAnnotations:
note: This is an example annotation
resources: []
#- service.yaml
#- ../some-dir/
# There could also be configmaps in Base, which would make these overlays
configMapGenerator: []
# There could be secrets in Base, if just using a fork/rebase workflow
secretGenerator: []
`
)
@@ -52,7 +36,7 @@ func TestAddResourceHappyPath(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
fakeFS.WriteFile(resourceFileName+"another", []byte(resourceFileContent))
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
cmd := newCmdAddResource(fakeFS)
args := []string{resourceFileName + "*"}
@@ -75,7 +59,7 @@ func TestAddResourceHappyPath(t *testing.T) {
func TestAddResourceAlreadyThere(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(resourceFileName, []byte(resourceFileContent))
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
cmd := newCmdAddResource(fakeFS)
args := []string{resourceFileName}

View File

@@ -0,0 +1,62 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package add
import (
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
)
// NewCmdAdd returns an instance of 'add' subcommand.
func NewCmdAdd(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
c := &cobra.Command{
Use: "add",
Short: "Adds configmap/resource/patch/base to the kustomization file.",
Long: "",
Example: `
# Adds a configmap to the kustomization file
kustomize edit add configmap NAME --from-literal=k=v
# Adds a resource to the kustomization
kustomize edit add resource <filepath>
# Adds a patch to the kustomization
kustomize edit add patch <filepath>
# Adds one or more base directories to the kustomization
kustomize edit add base <filepath>
kustomize edit add base <filepath1>,<filepath2>,<filepath3>
# Adds one or more commonLabels to the kustomization
kustomize edit add label {labelKey1:labelValue1},{labelKey2:labelValue2}
# Adds one or more commonAnnotations to the kustomization
kustomize edit add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}
`,
Args: cobra.MinimumNArgs(1),
}
c.AddCommand(
newCmdAddResource(fsys),
newCmdAddPatch(fsys),
newCmdAddConfigMap(fsys),
newCmdAddBase(fsys),
newCmdAddLabel(fsys, v.MakeLabelValidator()),
newCmdAddAnnotation(fsys, v.MakeAnnotationValidator()),
)
return c
}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package add
import (
"fmt"

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package add
import (
"reflect"

View File

@@ -14,13 +14,13 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package add
import (
"fmt"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/configmapandsecret"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
@@ -28,6 +28,7 @@ import (
"sigs.k8s.io/kustomize/pkg/types"
)
// newCmdAddConfigMap returns a new command.
func newCmdAddConfigMap(fSys fs.FileSystem) *cobra.Command {
var flagsAndArgs cMapFlagsAndArgs
cmd := &cobra.Command{
@@ -56,12 +57,12 @@ func newCmdAddConfigMap(fSys fs.FileSystem) *cobra.Command {
}
// Load the kustomization file.
mf, err := newKustomizationFile(constants.KustomizationFileName, fSys)
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fSys)
if err != nil {
return err
}
kustomization, err := mf.read()
kustomization, err := mf.Read()
if err != nil {
return err
}
@@ -76,7 +77,7 @@ func newCmdAddConfigMap(fSys fs.FileSystem) *cobra.Command {
}
// Write out the kustomization file with added configmap.
return mf.write(kustomization)
return mf.Write(kustomization)
},
}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package add
import (
"testing"

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package add
import (
"log"

47
pkg/commands/edit/all.go Normal file
View File

@@ -0,0 +1,47 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package edit
import (
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/edit/add"
"sigs.k8s.io/kustomize/pkg/commands/edit/set"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
)
// NewCmdEdit returns an instance of 'edit' subcommand.
func NewCmdEdit(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
c := &cobra.Command{
Use: "edit",
Short: "Edits a kustomization file",
Long: "",
Example: `
# Adds a configmap to the kustomization file
kustomize edit add configmap NAME --from-literal=k=v
# Sets the nameprefix field
kustomize edit set nameprefix <prefix-value>
`,
Args: cobra.MinimumNArgs(1),
}
c.AddCommand(
add.NewCmdAdd(fsys, v),
set.NewCmdSet(fsys, v),
)
return c
}

View File

@@ -0,0 +1,44 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package set
import (
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
)
// NewCmdSet returns an instance of 'set' subcommand.
func NewCmdSet(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
c := &cobra.Command{
Use: "set",
Short: "Sets the value of different fields in kustomization file.",
Long: "",
Example: `
# Sets the nameprefix field
kustomize edit set nameprefix <prefix-value>
`,
Args: cobra.MinimumNArgs(1),
}
c.AddCommand(
newCmdSetNamePrefix(fsys),
newCmdSetNamespace(fsys, v),
newCmdSetImageTag(fsys),
)
return c
}

View File

@@ -14,13 +14,13 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package set
import (
"errors"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
)
@@ -74,14 +74,14 @@ func (o *setNamePrefixOptions) Complete(cmd *cobra.Command, args []string) error
// RunSetNamePrefix runs setNamePrefix command (does real work).
func (o *setNamePrefixOptions) RunSetNamePrefix(fsys fs.FileSystem) error {
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys)
if err != nil {
return err
}
m, err := mf.read()
m, err := mf.Read()
if err != nil {
return err
}
m.NamePrefix = o.prefix
return mf.write(m)
return mf.Write(m)
}

View File

@@ -14,12 +14,11 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package set
import (
"testing"
"strings"
"testing"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
@@ -31,7 +30,7 @@ const (
func TestSetNamePrefixHappyPath(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
cmd := newCmdSetNamePrefix(fakeFS)
args := []string{goodPrefixValue}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package set
import (
"errors"
@@ -23,7 +23,7 @@ import (
"strings"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/types"
@@ -100,11 +100,11 @@ func (o *setImageTagOptions) Validate(args []string) error {
// RunSetImageTags runs setImageTags command (does real work).
func (o *setImageTagOptions) RunSetImageTags(fsys fs.FileSystem) error {
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys)
if err != nil {
return err
}
m, err := mf.read()
m, err := mf.Read()
if err != nil {
return err
}
@@ -127,5 +127,5 @@ func (o *setImageTagOptions) RunSetImageTags(fsys fs.FileSystem) error {
m.ImageTags = imageTags
return mf.write(m)
return mf.Write(m)
}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package set
import (
"strings"
@@ -26,7 +26,7 @@ import (
func TestSetImageTagsHappyPath(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
cmd := newCmdSetImageTag(fakeFS)
args := []string{"image1:tag1", "image2:tag2", "localhost:5000/operator:1.0.0",
@@ -57,7 +57,7 @@ imageTags:
func TestSetImageTagsOverride(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
cmd := newCmdSetImageTag(fakeFS)
args := []string{"image1:tag1", "image2:tag1"}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package set
import (
"errors"
@@ -22,6 +22,7 @@ import (
"strings"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
@@ -72,14 +73,14 @@ func (o *setNamespaceOptions) Validate(args []string) error {
// RunSetNamespace runs setNamespace command (does real work).
func (o *setNamespaceOptions) RunSetNamespace(fsys fs.FileSystem) error {
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys)
if err != nil {
return err
}
m, err := mf.read()
m, err := mf.Read()
if err != nil {
return err
}
m.Namespace = o.namespace
return mf.write(m)
return mf.Write(m)
}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package set
import (
"fmt"
@@ -32,7 +32,7 @@ const (
func TestSetNamespaceHappyPath(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
cmd := newCmdSetNamespace(fakeFS, validators.MakeFakeValidator())
args := []string{goodNamespaceValue}
@@ -52,7 +52,7 @@ func TestSetNamespaceHappyPath(t *testing.T) {
func TestSetNamespaceOverride(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
fakeFS.WriteTestKustomization()
cmd := newCmdSetNamespace(fakeFS, validators.MakeFakeValidator())
args := []string{goodNamespaceValue}
@@ -90,7 +90,6 @@ func TestSetNamespaceNoArgs(t *testing.T) {
func TestSetNamespaceInvalid(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
cmd := newCmdSetNamespace(fakeFS, validators.MakeFakeValidator())
args := []string{"/badnamespace/"}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package kustfile
import (
"bytes"
@@ -77,7 +77,8 @@ type kustomizationFile struct {
originalFields []*commentedField
}
func newKustomizationFile(mPath string, fsys fs.FileSystem) (*kustomizationFile, error) { // nolint
// NewKustomizationFile returns a new instance.
func NewKustomizationFile(mPath string, fsys fs.FileSystem) (*kustomizationFile, error) { // nolint
mf := &kustomizationFile{path: mPath, fsys: fsys}
err := mf.validate()
if err != nil {
@@ -104,7 +105,7 @@ func (mf *kustomizationFile) validate() error {
return nil
}
func (mf *kustomizationFile) read() (*types.Kustomization, error) {
func (mf *kustomizationFile) Read() (*types.Kustomization, error) {
data, err := mf.fsys.ReadFile(mf.path)
if err != nil {
return nil, err
@@ -121,7 +122,7 @@ func (mf *kustomizationFile) read() (*types.Kustomization, error) {
return &kustomization, err
}
func (mf *kustomizationFile) write(kustomization *types.Kustomization) error {
func (mf *kustomizationFile) Write(kustomization *types.Kustomization) error {
if kustomization == nil {
return errors.New("util: kustomization file arg is nil")
}
@@ -132,7 +133,8 @@ func (mf *kustomizationFile) write(kustomization *types.Kustomization) error {
return mf.fsys.WriteFile(mf.path, data)
}
func stringInSlice(str string, list []string) bool {
// StringInSlice returns true if the string is in the slice.
func StringInSlice(str string, list []string) bool {
for _, v := range list {
if v == str {
return true

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
package kustfile
import (
"reflect"
@@ -33,16 +33,16 @@ func TestWriteAndRead(t *testing.T) {
fsys := fs.MakeFakeFS()
fsys.Create(constants.KustomizationFileName)
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
mf, err := NewKustomizationFile(constants.KustomizationFileName, fsys)
if err != nil {
t.Fatalf("Unexpected Error: %v", err)
}
if err := mf.write(kustomization); err != nil {
if err := mf.Write(kustomization); err != nil {
t.Fatalf("Couldn't write kustomization file: %v\n", err)
}
content, err := mf.read()
content, err := mf.Read()
if err != nil {
t.Fatalf("Couldn't read kustomization file: %v\n", err)
}
@@ -53,7 +53,7 @@ func TestWriteAndRead(t *testing.T) {
func TestEmptyFile(t *testing.T) {
fsys := fs.MakeFakeFS()
_, err := newKustomizationFile("", fsys)
_, err := NewKustomizationFile("", fsys)
if err == nil {
t.Fatalf("Create kustomizationFile from empty filename should fail")
}
@@ -64,7 +64,7 @@ func TestNewNotExist(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.Mkdir(".")
fakeFS.Create(badSuffix)
_, err := newKustomizationFile(constants.KustomizationFileName, fakeFS)
_, err := NewKustomizationFile(constants.KustomizationFileName, fakeFS)
if err == nil {
t.Fatalf("expect an error")
}
@@ -72,14 +72,14 @@ func TestNewNotExist(t *testing.T) {
if !strings.Contains(err.Error(), contained) {
t.Fatalf("expect an error contains %q, but got %v", contained, err)
}
_, err = newKustomizationFile(constants.KustomizationFileName, fakeFS)
_, err = NewKustomizationFile(constants.KustomizationFileName, fakeFS)
if err == nil {
t.Fatalf("expect an error")
}
if !strings.Contains(err.Error(), contained) {
t.Fatalf("expect an error contains %q, but got %v", contained, err)
}
_, err = newKustomizationFile(badSuffix, fakeFS)
_, err = NewKustomizationFile(badSuffix, fakeFS)
if err == nil {
t.Fatalf("expect an error")
}
@@ -116,15 +116,15 @@ patchesStrategicMerge:
fsys := fs.MakeFakeFS()
fsys.Create(constants.KustomizationFileName)
fsys.WriteFile(constants.KustomizationFileName, kustomizationContentWithComments)
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
mf, err := NewKustomizationFile(constants.KustomizationFileName, fsys)
if err != nil {
t.Fatalf("Unexpected Error: %v", err)
}
kustomization, err := mf.read()
kustomization, err := mf.Read()
if err != nil {
t.Fatalf("Unexpected Error: %v", err)
}
if err = mf.write(kustomization); err != nil {
if err = mf.Write(kustomization); err != nil {
t.Fatalf("Unexpected Error: %v", err)
}
bytes, _ := fsys.ReadFile(mf.path)
@@ -207,16 +207,16 @@ patchesStrategicMerge:
fsys := fs.MakeFakeFS()
fsys.Create(constants.KustomizationFileName)
fsys.WriteFile(constants.KustomizationFileName, kustomizationContentWithComments)
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
mf, err := NewKustomizationFile(constants.KustomizationFileName, fsys)
if err != nil {
t.Fatalf("Unexpected Error: %v", err)
}
kustomization, err := mf.read()
kustomization, err := mf.Read()
if err != nil {
t.Fatalf("Unexpected Error: %v", err)
}
if err = mf.write(kustomization); err != nil {
if err = mf.Write(kustomization); err != nil {
t.Fatalf("Unexpected Error: %v", err)
}
bytes, _ := fsys.ReadFile(mf.path)

Some files were not shown because too many files have changed in this diff Show More