unset.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package config
  14. import (
  15. "errors"
  16. "fmt"
  17. "io"
  18. "reflect"
  19. "github.com/renstrom/dedent"
  20. "github.com/spf13/cobra"
  21. "k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
  22. )
  23. type unsetOptions struct {
  24. configAccess clientcmd.ConfigAccess
  25. propertyName string
  26. }
  27. var unset_long = dedent.Dedent(`
  28. Unsets an individual value in a kubeconfig file
  29. PROPERTY_NAME is a dot delimited name where each token represents either an attribute name or a map key. Map keys may not contain dots.`)
  30. func NewCmdConfigUnset(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
  31. options := &unsetOptions{configAccess: configAccess}
  32. cmd := &cobra.Command{
  33. Use: "unset PROPERTY_NAME",
  34. Short: "Unsets an individual value in a kubeconfig file",
  35. Long: unset_long,
  36. Run: func(cmd *cobra.Command, args []string) {
  37. if !options.complete(cmd) {
  38. return
  39. }
  40. err := options.run()
  41. if err != nil {
  42. fmt.Fprintf(out, "%v\n", err)
  43. } else {
  44. fmt.Fprintf(out, "property %q unset.\n", options.propertyName)
  45. }
  46. },
  47. }
  48. return cmd
  49. }
  50. func (o unsetOptions) run() error {
  51. err := o.validate()
  52. if err != nil {
  53. return err
  54. }
  55. config, err := o.configAccess.GetStartingConfig()
  56. if err != nil {
  57. return err
  58. }
  59. steps, err := newNavigationSteps(o.propertyName)
  60. if err != nil {
  61. return err
  62. }
  63. err = modifyConfig(reflect.ValueOf(config), steps, "", true, true)
  64. if err != nil {
  65. return err
  66. }
  67. if err := clientcmd.ModifyConfig(o.configAccess, *config, false); err != nil {
  68. return err
  69. }
  70. return nil
  71. }
  72. func (o *unsetOptions) complete(cmd *cobra.Command) bool {
  73. endingArgs := cmd.Flags().Args()
  74. if len(endingArgs) != 1 {
  75. cmd.Help()
  76. return false
  77. }
  78. o.propertyName = endingArgs[0]
  79. return true
  80. }
  81. func (o unsetOptions) validate() error {
  82. if len(o.propertyName) == 0 {
  83. return errors.New("you must specify a property")
  84. }
  85. return nil
  86. }