Files
kustomize/kustomize/commands/edit/all.go
Mauren Berti 30893b0184 feat: edit set configmap (#5391)
* feat: add new command 'edit set configmap'

* Add a new command 'edit set configmap' to allow editing the values of an
  already-existing configmap in a kustomization file.
* Add tests to validate the new feature.

* fix: add tests, minor refactoring to use constants

* Include tests to validade the new function ValidateSet, included to do
  necessary validations when running the 'kustomize edit set configmap' command.
* Minor refactorings to use the existing constants in the 'edit set configmap'
  command.
* Add dashes before each item in the comment explaining how ExpandFileSource()
  works so IDEs don't try to reformat the list and remove the indentation in it.
* Because this change mutates the list of literal sources, ensure that both add
  and set save the resulting list in a predictable order to make it easier to
  check when new items are added/removed and aid in testing.
* Since literal sources are the only bit that's important in this test, verify
  that the literal sources in the actual result is equal to what we expected it
  to be.

* fix: change format to print resource name

Use '%q' formatter instead of '%s' to print resource name

Co-authored-by: Varsha <varshaprasad96@gmail.com>

* fix: add changes from code review

* Unexport constant that is used only in the scope of a single function.
* Add extra validation to ensure format is correct with one single '=' per key-value
  pair.
* Add extra set of tests to validate format.
* Update test case to match new printed format in the error message.

* fix: rollback sort for edit add/set configmap

* chore: rename test package and unexport functions

Rename the test package from set_test back to set and unexport functions that do
not need to be exported anymore for testing purposes.

* feat: handle empty and default namespace as equal

Handle the empty and the default namespaces as equal. Add tests to validate this
scenario.

---------

Co-authored-by: Varsha <varshaprasad96@gmail.com>
2023-11-17 19:14:53 +01:00

59 lines
1.5 KiB
Go

// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package edit
import (
"io"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/kv"
ldrhelper "sigs.k8s.io/kustomize/api/pkg/loader"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/kustomize/v5/commands/edit/add"
"sigs.k8s.io/kustomize/kustomize/v5/commands/edit/fix"
"sigs.k8s.io/kustomize/kustomize/v5/commands/edit/listbuiltin"
"sigs.k8s.io/kustomize/kustomize/v5/commands/edit/remove"
"sigs.k8s.io/kustomize/kustomize/v5/commands/edit/set"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
// NewCmdEdit returns an instance of 'edit' subcommand.
func NewCmdEdit(
fSys filesys.FileSystem, v ifc.Validator, rf *resource.Factory,
w io.Writer) *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>
# Sets the namesuffix field
kustomize edit set namesuffix <suffix-value>
`,
Args: cobra.MinimumNArgs(1),
}
c.AddCommand(
add.NewCmdAdd(
fSys,
kv.NewLoader(ldrhelper.NewFileLoaderAtCwd(fSys), v),
rf),
set.NewCmdSet(
fSys,
kv.NewLoader(ldrhelper.NewFileLoaderAtCwd(fSys), v),
v,
rf),
fix.NewCmdFix(fSys, w),
remove.NewCmdRemove(fSys, v),
listbuiltin.NewCmdListBuiltinPlugin(),
)
return c
}