flags.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Copyright 2018 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 flag
  14. import (
  15. "fmt"
  16. "net"
  17. "strconv"
  18. "github.com/spf13/pflag"
  19. "k8s.io/klog"
  20. utilnet "k8s.io/apimachinery/pkg/util/net"
  21. )
  22. // PrintFlags logs the flags in the flagset
  23. func PrintFlags(flags *pflag.FlagSet) {
  24. flags.VisitAll(func(flag *pflag.Flag) {
  25. klog.V(1).Infof("FLAG: --%s=%q", flag.Name, flag.Value)
  26. })
  27. }
  28. // TODO(mikedanese): remove these flag wrapper types when we remove command line flags
  29. var (
  30. _ pflag.Value = &IPVar{}
  31. _ pflag.Value = &IPPortVar{}
  32. _ pflag.Value = &PortRangeVar{}
  33. )
  34. // IPVar is used for validating a command line option that represents an IP. It implements the pflag.Value interface
  35. type IPVar struct {
  36. Val *string
  37. }
  38. // Set sets the flag value
  39. func (v IPVar) Set(s string) error {
  40. if len(s) == 0 {
  41. v.Val = nil
  42. return nil
  43. }
  44. if net.ParseIP(s) == nil {
  45. return fmt.Errorf("%q is not a valid IP address", s)
  46. }
  47. if v.Val == nil {
  48. // it's okay to panic here since this is programmer error
  49. panic("the string pointer passed into IPVar should not be nil")
  50. }
  51. *v.Val = s
  52. return nil
  53. }
  54. // String returns the flag value
  55. func (v IPVar) String() string {
  56. if v.Val == nil {
  57. return ""
  58. }
  59. return *v.Val
  60. }
  61. // Type gets the flag type
  62. func (v IPVar) Type() string {
  63. return "ip"
  64. }
  65. // IPPortVar is used for validating a command line option that represents an IP and a port. It implements the pflag.Value interface
  66. type IPPortVar struct {
  67. Val *string
  68. }
  69. // Set sets the flag value
  70. func (v IPPortVar) Set(s string) error {
  71. if len(s) == 0 {
  72. v.Val = nil
  73. return nil
  74. }
  75. if v.Val == nil {
  76. // it's okay to panic here since this is programmer error
  77. panic("the string pointer passed into IPPortVar should not be nil")
  78. }
  79. // Both IP and IP:port are valid.
  80. // Attempt to parse into IP first.
  81. if net.ParseIP(s) != nil {
  82. *v.Val = s
  83. return nil
  84. }
  85. // Can not parse into IP, now assume IP:port.
  86. host, port, err := net.SplitHostPort(s)
  87. if err != nil {
  88. return fmt.Errorf("%q is not in a valid format (ip or ip:port): %v", s, err)
  89. }
  90. if net.ParseIP(host) == nil {
  91. return fmt.Errorf("%q is not a valid IP address", host)
  92. }
  93. if _, err := strconv.Atoi(port); err != nil {
  94. return fmt.Errorf("%q is not a valid number", port)
  95. }
  96. *v.Val = s
  97. return nil
  98. }
  99. // String returns the flag value
  100. func (v IPPortVar) String() string {
  101. if v.Val == nil {
  102. return ""
  103. }
  104. return *v.Val
  105. }
  106. // Type gets the flag type
  107. func (v IPPortVar) Type() string {
  108. return "ipport"
  109. }
  110. // PortRangeVar is used for validating a command line option that represents a port range. It implements the pflag.Value interface
  111. type PortRangeVar struct {
  112. Val *string
  113. }
  114. // Set sets the flag value
  115. func (v PortRangeVar) Set(s string) error {
  116. if _, err := utilnet.ParsePortRange(s); err != nil {
  117. return fmt.Errorf("%q is not a valid port range: %v", s, err)
  118. }
  119. if v.Val == nil {
  120. // it's okay to panic here since this is programmer error
  121. panic("the string pointer passed into PortRangeVar should not be nil")
  122. }
  123. *v.Val = s
  124. return nil
  125. }
  126. // String returns the flag value
  127. func (v PortRangeVar) String() string {
  128. if v.Val == nil {
  129. return ""
  130. }
  131. return *v.Val
  132. }
  133. // Type gets the flag type
  134. func (v PortRangeVar) Type() string {
  135. return "port-range"
  136. }