flag.go 3.0 KB

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