flag.go 3.9 KB

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