flag.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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 flags
  14. import (
  15. "flag"
  16. "fmt"
  17. "log"
  18. "net/url"
  19. "os"
  20. "strings"
  21. "github.com/coreos/etcd/pkg/transport"
  22. )
  23. // DeprecatedFlag encapsulates a flag that may have been previously valid but
  24. // is now deprecated. If a DeprecatedFlag is set, an error occurs.
  25. type DeprecatedFlag struct {
  26. Name string
  27. }
  28. func (f *DeprecatedFlag) Set(_ string) error {
  29. return fmt.Errorf(`flag "-%s" is no longer supported.`, f.Name)
  30. }
  31. func (f *DeprecatedFlag) String() string {
  32. return ""
  33. }
  34. // IgnoredFlag encapsulates a flag that may have been previously valid but is
  35. // now ignored. If an IgnoredFlag is set, a warning is printed and
  36. // operation continues.
  37. type IgnoredFlag struct {
  38. Name string
  39. }
  40. // IsBoolFlag is defined to allow the flag to be defined without an argument
  41. func (f *IgnoredFlag) IsBoolFlag() bool {
  42. return true
  43. }
  44. func (f *IgnoredFlag) Set(s string) error {
  45. log.Printf(`WARNING: flag "-%s" is no longer supported - ignoring.`, f.Name)
  46. return nil
  47. }
  48. func (f *IgnoredFlag) String() string {
  49. return ""
  50. }
  51. func UsageWithIgnoredFlagsFunc(fs *flag.FlagSet, ignore []string) func() {
  52. iMap := make(map[string]struct{}, len(ignore))
  53. for _, name := range ignore {
  54. iMap[name] = struct{}{}
  55. }
  56. return func() {
  57. fs.VisitAll(func(f *flag.Flag) {
  58. if _, ok := iMap[f.Name]; ok {
  59. return
  60. }
  61. format := " -%s=%s: %s\n"
  62. fmt.Fprintf(os.Stderr, format, f.Name, f.DefValue, f.Usage)
  63. })
  64. }
  65. }
  66. // SetFlagsFromEnv parses all registered flags in the given flagset,
  67. // and if they are not already set it attempts to set their values from
  68. // environment variables. Environment variables take the name of the flag but
  69. // are UPPERCASE, have the prefix "ETCD_", and any dashes are replaced by
  70. // underscores - for example: some-flag => ETCD_SOME_FLAG
  71. func SetFlagsFromEnv(fs *flag.FlagSet) error {
  72. var err error
  73. alreadySet := make(map[string]bool)
  74. fs.Visit(func(f *flag.Flag) {
  75. alreadySet[f.Name] = true
  76. })
  77. fs.VisitAll(func(f *flag.Flag) {
  78. if !alreadySet[f.Name] {
  79. key := "ETCD_" + strings.ToUpper(strings.Replace(f.Name, "-", "_", -1))
  80. val := os.Getenv(key)
  81. if val != "" {
  82. if serr := fs.Set(f.Name, val); serr != nil {
  83. err = fmt.Errorf("invalid value %q for %s: %v", val, key, serr)
  84. }
  85. }
  86. }
  87. })
  88. return err
  89. }
  90. // URLsFromFlags decides what URLs should be using two different flags
  91. // as datasources. The first flag's Value must be of type URLs, while
  92. // the second must be of type IPAddressPort. If both of these flags
  93. // are set, an error will be returned. If only the first flag is set,
  94. // the underlying url.URL objects will be returned unmodified. If the
  95. // second flag happens to be set, the underlying IPAddressPort will be
  96. // converted to a url.URL and returned. The Scheme of the returned
  97. // url.URL will be http unless the provided TLSInfo object is non-empty.
  98. // If neither of the flags have been explicitly set, the default value
  99. // of the first flag will be returned unmodified.
  100. func URLsFromFlags(fs *flag.FlagSet, urlsFlagName string, addrFlagName string, tlsInfo transport.TLSInfo) ([]url.URL, error) {
  101. visited := make(map[string]struct{})
  102. fs.Visit(func(f *flag.Flag) {
  103. visited[f.Name] = struct{}{}
  104. })
  105. _, urlsFlagIsSet := visited[urlsFlagName]
  106. _, addrFlagIsSet := visited[addrFlagName]
  107. if addrFlagIsSet {
  108. if urlsFlagIsSet {
  109. return nil, fmt.Errorf("Set only one of flags -%s and -%s", urlsFlagName, addrFlagName)
  110. }
  111. addr := *fs.Lookup(addrFlagName).Value.(*IPAddressPort)
  112. addrURL := url.URL{Scheme: "http", Host: addr.String()}
  113. if !tlsInfo.Empty() {
  114. addrURL.Scheme = "https"
  115. }
  116. return []url.URL{addrURL}, nil
  117. }
  118. return []url.URL(*fs.Lookup(urlsFlagName).Value.(*URLsValue)), nil
  119. }