| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- package pkg
- import (
- "flag"
- "fmt"
- "log"
- "net/url"
- "os"
- "strings"
- "github.com/coreos/etcd/pkg/flags"
- "github.com/coreos/etcd/pkg/transport"
- )
- type DeprecatedFlag struct {
- Name string
- }
- // IsBoolFlag is defined to allow the flag to be defined without an argument
- func (df *DeprecatedFlag) IsBoolFlag() bool {
- return true
- }
- func (df *DeprecatedFlag) Set(s string) error {
- log.Printf("WARNING: flag \"-%s\" is no longer supported.", df.Name)
- return nil
- }
- func (df *DeprecatedFlag) String() string {
- return ""
- }
- func UsageWithIgnoredFlagsFunc(fs *flag.FlagSet, ignore []string) func() {
- iMap := make(map[string]struct{}, len(ignore))
- for _, name := range ignore {
- iMap[name] = struct{}{}
- }
- return func() {
- fs.VisitAll(func(f *flag.Flag) {
- if _, ok := iMap[f.Name]; ok {
- return
- }
- format := " -%s=%s: %s\n"
- fmt.Fprintf(os.Stderr, format, f.Name, f.DefValue, f.Usage)
- })
- }
- }
- // SetFlagsFromEnv parses all registered flags in the given flagset,
- // and if they are not already set it attempts to set their values from
- // environment variables. Environment variables take the name of the flag but
- // are UPPERCASE, have the prefix "ETCD_", and any dashes are replaced by
- // underscores - for example: some-flag => ETCD_SOME_FLAG
- func SetFlagsFromEnv(fs *flag.FlagSet) {
- alreadySet := make(map[string]bool)
- fs.Visit(func(f *flag.Flag) {
- alreadySet[f.Name] = true
- })
- fs.VisitAll(func(f *flag.Flag) {
- if !alreadySet[f.Name] {
- key := "ETCD_" + strings.ToUpper(strings.Replace(f.Name, "-", "_", -1))
- val := os.Getenv(key)
- if val != "" {
- fs.Set(f.Name, val)
- }
- }
- })
- }
- // URLsFromFlags decides what URLs should be using two different flags
- // as datasources. The first flag's Value must be of type URLs, while
- // the second must be of type IPAddressPort. If both of these flags
- // are set, an error will be returned. If only the first flag is set,
- // the underlying url.URL objects will be returned unmodified. If the
- // second flag happens to be set, the underlying IPAddressPort will be
- // converted to a url.URL and returned. The Scheme of the returned
- // url.URL will be http unless the provided TLSInfo object is non-empty.
- // If neither of the flags have been explicitly set, the default value
- // of the first flag will be returned unmodified.
- func URLsFromFlags(fs *flag.FlagSet, urlsFlagName string, addrFlagName string, tlsInfo transport.TLSInfo) ([]url.URL, error) {
- visited := make(map[string]struct{})
- fs.Visit(func(f *flag.Flag) {
- visited[f.Name] = struct{}{}
- })
- _, urlsFlagIsSet := visited[urlsFlagName]
- _, addrFlagIsSet := visited[addrFlagName]
- if addrFlagIsSet {
- if urlsFlagIsSet {
- return nil, fmt.Errorf("Set only one of flags -%s and -%s", urlsFlagName, addrFlagName)
- }
- addr := *fs.Lookup(addrFlagName).Value.(*flags.IPAddressPort)
- addrURL := url.URL{Scheme: "http", Host: addr.String()}
- if !tlsInfo.Empty() {
- addrURL.Scheme = "https"
- }
- return []url.URL{addrURL}, nil
- }
- return []url.URL(*fs.Lookup(urlsFlagName).Value.(*flags.URLsValue)), nil
- }
|