flag.go 3.5 KB

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