func getTransport(c *cli.Context) (*http.Transport, error) { cafile := c.GlobalString("ca-file") certfile := c.GlobalString("cert-file") keyfile := c.GlobalString("key-file") // Use an environment variable if nothing was supplied on the // command line if cafile == "" { cafile = os.Getenv("ETCDCTL_CA_FILE") } if certfile == "" { certfile = os.Getenv("ETCDCTL_CERT_FILE") } if keyfile == "" { keyfile = os.Getenv("ETCDCTL_KEY_FILE") } tls := transport.TLSInfo{ CAFile: cafile, CertFile: certfile, KeyFile: keyfile, } dialTimeout := defaultDialTimeout totalTimeout := c.GlobalDuration("total-timeout") if totalTimeout != 0 && totalTimeout < dialTimeout { dialTimeout = totalTimeout } return transport.NewTransport(tls, dialTimeout) }
func newClient(c *cli.Context) (client.Client, error) { eps, err := getEndpoints(c) if err != nil { return nil, err } tr, err := getTransport(c) if err != nil { return nil, err } cfg := client.Config{ Transport: tr, Endpoints: eps, HeaderTimeoutPerRequest: c.GlobalDuration("timeout"), } uFlag := c.GlobalString("username") if uFlag == "" { uFlag = os.Getenv("ETCDCTL_USERNAME") } if uFlag != "" { username, password, err := getUsernamePasswordFromFlag(uFlag) if err != nil { return nil, err } cfg.Username = username cfg.Password = password } return client.New(cfg) }
func etcdConfigFromFlags(c *cli.Context) etcdclient.Config { eFlag := c.GlobalString("endpoint") uFlag := c.GlobalString("username") pFlag := c.GlobalString("password") tFlag := c.GlobalDuration("timeout") cfg := etcdclient.Config{ Endpoints: strings.Split(eFlag, ","), Transport: etcdclient.DefaultTransport, HeaderTimeoutPerRequest: tFlag, } if uFlag != "" && pFlag != "" { cfg.Username = uFlag cfg.Password = pFlag } return cfg }
func contextWithTotalTimeout(c *cli.Context) (context.Context, context.CancelFunc) { return context.WithTimeout(context.Background(), c.GlobalDuration("total-timeout")) }