本文整理汇总了Golang中net/url.URL.User方法的典型用法代码示例。如果您正苦于以下问题:Golang URL.User方法的具体用法?Golang URL.User怎么用?Golang URL.User使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/url.URL
的用法示例。
在下文中一共展示了URL.User方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SetURLUser
// SetURLUser set the user credentials in the given URL. If the username or
// password is not set in the URL then the default is used (if provided).
func SetURLUser(u *url.URL, defaultUser, defaultPass string) {
var user, pass string
var userIsSet, passIsSet bool
if u.User != nil {
user = u.User.Username()
if user != "" {
userIsSet = true
}
pass, passIsSet = u.User.Password()
}
if !userIsSet && defaultUser != "" {
userIsSet = true
user = defaultUser
}
if !passIsSet && defaultPass != "" {
passIsSet = true
pass = defaultPass
}
if userIsSet && passIsSet {
u.User = url.UserPassword(user, pass)
} else if userIsSet {
u.User = url.User(user)
}
}
示例2: processOverride
func processOverride(u *url.URL) {
envUsername := os.Getenv(envUserName)
envPassword := os.Getenv(envPassword)
// Override username if provided
if envUsername != "" {
var password string
var ok bool
if u.User != nil {
password, ok = u.User.Password()
}
if ok {
u.User = url.UserPassword(envUsername, password)
} else {
u.User = url.User(envUsername)
}
}
// Override password if provided
if envPassword != "" {
var username string
if u.User != nil {
username = u.User.Username()
}
u.User = url.UserPassword(username, envPassword)
}
}
示例3: SetAPIKey
// Set the API key used for all Get and Push API calls.
func SetAPIKey(apiKey string) {
pUrl := url.URL{}
pUrl.Scheme = "https"
pUrl.User = url.UserPassword(apiKey, "")
pUrl.Host = "api.pushbullet.com"
pUrl.Path = "/api/pushes"
pushUrl = pUrl.String()
gUrl := url.URL{}
gUrl.Scheme = "https"
gUrl.User = url.UserPassword(apiKey, "")
gUrl.Host = "api.pushbullet.com"
gUrl.Path = "/api/devices"
getUrl = gUrl.String()
}
示例4: ParseURL
// ParseURL is wrapper around url.Parse, where Scheme defaults to "https" and Path defaults to "/sdk"
func ParseURL(s string) (*url.URL, error) {
var err error
var u *url.URL
if s != "" {
// Default the scheme to https
if !schemeMatch.MatchString(s) {
s = "https://" + s
}
u, err = url.Parse(s)
if err != nil {
return nil, err
}
// Default the path to /sdk
if u.Path == "" {
u.Path = "/sdk"
}
if u.User == nil {
u.User = url.UserPassword("", "")
}
}
return u, nil
}
示例5: linkArgs
func linkArgs(s Service, container string) []string {
args := []string{}
prefix := strings.Replace(strings.ToUpper(s.Name), "-", "_", -1)
env := containerEnv(container)
scheme := coalesce(env["LINK_SCHEME"], "tcp")
host := containerHost(container, s.Networks)
port := containerPort(container)
path := env["LINK_PATH"]
username := env["LINK_USERNAME"]
password := env["LINK_PASSWORD"]
args = append(args, "--add-host", fmt.Sprintf("%s:%s", s.Name, host))
args = append(args, "-e", fmt.Sprintf("%s_SCHEME=%s", prefix, scheme))
args = append(args, "-e", fmt.Sprintf("%s_HOST=%s", prefix, host))
args = append(args, "-e", fmt.Sprintf("%s_PORT=%s", prefix, port))
args = append(args, "-e", fmt.Sprintf("%s_PATH=%s", prefix, path))
args = append(args, "-e", fmt.Sprintf("%s_USERNAME=%s", prefix, username))
args = append(args, "-e", fmt.Sprintf("%s_PASSWORD=%s", prefix, password))
u := url.URL{
Scheme: scheme,
Host: fmt.Sprintf("%s:%s", host, port),
Path: path,
}
if username != "" || password != "" {
u.User = url.UserPassword(username, password)
}
args = append(args, "-e", fmt.Sprintf("%s_URL=%s", prefix, u.String()))
return args
}
示例6: OutletURL
func (c *ShuttleConfig) OutletURL() string {
var err error
var oUrl *url.URL
if len(c.LogsURL) > 0 {
oUrl, err = url.Parse(c.LogsURL)
if err != nil {
log.Fatalf("Unable to parse logs-url")
}
}
if len(LogplexUrl) > 0 {
oUrl, err = url.Parse(LogplexUrl)
if err != nil {
log.Fatalf("Unable to parse $LOGPLEX_URL")
}
}
if oUrl == nil {
log.Fatalf("Must set -logs-url or $LOGPLEX_URL.")
}
if oUrl.User == nil {
oUrl.User = url.UserPassword("token", c.Appname)
}
return oUrl.String()
}
示例7: ResolvedLinkVars
// NOTE: this is the simpler approach:
// build up the ENV from the declared links
// assuming local dev is done on DOCKER_HOST
func (me *ManifestEntry) ResolvedLinkVars(m *Manifest, cache bool) (map[string]string, error) {
linkVars := make(map[string]string)
if m == nil {
return linkVars, nil
}
for _, link := range me.Links {
linkEntry, ok := (*m)[link]
if !ok {
return nil, fmt.Errorf("no such link: %s", link)
}
linkEntryEnv, err := getLinkEntryEnv(linkEntry, cache)
if err != nil {
return linkVars, err
}
// get url parts from various places
scheme := linkEntryEnv["LINK_SCHEME"]
if scheme == "" {
scheme = "tcp"
}
host, err := getDockerGateway()
if err != nil {
return linkVars, err
}
// we don't create a balancer without a port,
// so we don't create a link url either
port := resolveOtherPort(link, linkEntry)
if port == "" {
continue
}
linkUrl := url.URL{
Scheme: scheme,
Host: host + ":" + port,
Path: linkEntryEnv["LINK_PATH"],
}
if linkEntryEnv["LINK_USERNAME"] != "" || linkEntryEnv["LINK_PASSWORD"] != "" {
linkUrl.User = url.UserPassword(linkEntryEnv["LINK_USERNAME"], linkEntryEnv["LINK_PASSWORD"])
}
prefix := strings.ToUpper(link) + "_"
prefix = strings.Replace(prefix, "-", "_", -1)
linkVars[prefix+"URL"] = linkUrl.String()
linkVars[prefix+"HOST"] = host
linkVars[prefix+"SCHEME"] = scheme
linkVars[prefix+"PORT"] = port
linkVars[prefix+"USERNAME"] = linkEntryEnv["LINK_USERNAME"]
linkVars[prefix+"PASSWORD"] = linkEntryEnv["LINK_PASSWORD"]
linkVars[prefix+"PATH"] = linkEntryEnv["LINK_PATH"]
}
return linkVars, nil
}
示例8: connect
func (c *cli) connect(cmd string) {
var cl *client.Client
if cmd != "" {
// TODO parse out connection string
}
u := url.URL{
Scheme: "http",
}
if c.port > 0 {
u.Host = fmt.Sprintf("%s:%d", c.host, c.port)
} else {
u.Host = c.host
}
if c.username != "" {
u.User = url.UserPassword(c.username, c.password)
}
cl, err := client.NewClient(
client.Config{
URL: u,
Username: c.username,
Password: c.password,
})
if err != nil {
fmt.Printf("Could not create client %s", err)
return
}
c.client = cl
if _, v, e := c.client.Ping(); e != nil {
fmt.Printf("Failed to connect to %s\n", c.client.Addr())
} else {
c.version = v
fmt.Printf("Connected to %s version %s\n", c.client.Addr(), c.version)
}
}
示例9: connect
func (c *CommandLine) connect(cmd string) {
var cl *client.Client
if cmd != "" {
// Remove the "connect" keyword if it exists
cmd = strings.TrimSpace(strings.Replace(cmd, "connect", "", -1))
if cmd == "" {
return
}
if strings.Contains(cmd, ":") {
h := strings.Split(cmd, ":")
if i, e := strconv.Atoi(h[1]); e != nil {
fmt.Printf("Connect error: Invalid port number %q: %s\n", cmd, e)
return
} else {
c.Port = i
}
if h[0] == "" {
c.Host = default_host
} else {
c.Host = h[0]
}
} else {
c.Host = cmd
// If they didn't specify a port, always use the default port
c.Port = default_port
}
}
u := url.URL{
Scheme: "http",
}
if c.Port > 0 {
u.Host = fmt.Sprintf("%s:%d", c.Host, c.Port)
} else {
u.Host = c.Host
}
if c.Username != "" {
u.User = url.UserPassword(c.Username, c.Password)
}
cl, err := client.NewClient(
client.Config{
URL: u,
Username: c.Username,
Password: c.Password,
UserAgent: "InfluxDBShell/" + version,
})
if err != nil {
fmt.Printf("Could not create client %s", err)
return
}
c.Client = cl
if _, v, e := c.Client.Ping(); e != nil {
fmt.Printf("Failed to connect to %s\n", c.Client.Addr())
} else {
c.Version = v
fmt.Printf("Connected to %s version %s\n", c.Client.Addr(), c.Version)
}
}
示例10: redactPassword
// redactPassword returns the URL as a string with the password redacted.
func redactPassword(u url.URL) string {
if u.User == nil {
return u.String()
}
u.User = url.User(u.User.Username())
return u.String()
}
示例11: String
// String encodes dsn to a URL string format.
func (dsn *DSN) String() string {
u := url.URL{
Host: fmt.Sprintf("tcp(%s)", dsn.Host),
Path: "/" + dsn.Database,
RawQuery: url.Values{
"timeout": {dsn.Timeout.String()},
}.Encode(),
}
// Set password, if available.
if dsn.Password == "" {
u.User = url.User(dsn.User)
} else {
u.User = url.UserPassword(dsn.User, dsn.Password)
}
// Remove leading double-slash.
return strings.TrimPrefix(u.String(), "//")
}
示例12: open
func (p *Postgres) open(logger logrus.FieldLogger) (*conn, error) {
v := url.Values{}
set := func(key, val string) {
if val != "" {
v.Set(key, val)
}
}
set("connect_timeout", strconv.Itoa(p.ConnectionTimeout))
set("sslkey", p.SSL.KeyFile)
set("sslcert", p.SSL.CertFile)
set("sslrootcert", p.SSL.CAFile)
if p.SSL.Mode == "" {
// Assume the strictest mode if unspecified.
p.SSL.Mode = sslVerifyFull
}
set("sslmode", p.SSL.Mode)
u := url.URL{
Scheme: "postgres",
Host: p.Host,
Path: "/" + p.Database,
RawQuery: v.Encode(),
}
if p.User != "" {
if p.Password != "" {
u.User = url.UserPassword(p.User, p.Password)
} else {
u.User = url.User(p.User)
}
}
db, err := sql.Open("postgres", u.String())
if err != nil {
return nil, err
}
c := &conn{db, flavorPostgres, logger}
if _, err := c.migrate(); err != nil {
return nil, fmt.Errorf("failed to perform migrations: %v", err)
}
return c, nil
}
示例13: getURL
func getURL() url.URL {
ur := url.URL{
Scheme: "mongodb",
Host: host,
Path: db,
}
if name != "" {
u := url.UserPassword(name, password)
ur.User = u
}
return ur
}
示例14: addAuthFromNetrc
// addAuthFromNetrc adds auth information to the URL from the user's
// netrc file if it can be found. This will only add the auth info
// if the URL doesn't already have auth info specified and the
// the username is blank.
func addAuthFromNetrc(u *url.URL) error {
// If the URL already has auth information, do nothing
if u.User != nil && u.User.Username() != "" {
return nil
}
// Get the netrc file path
path := os.Getenv("NETRC")
if path == "" {
filename := ".netrc"
if runtime.GOOS == "windows" {
filename = "_netrc"
}
var err error
path, err = homedir.Expand("~/" + filename)
if err != nil {
return err
}
}
// If the file is not a file, then do nothing
if fi, err := os.Stat(path); err != nil {
// File doesn't exist, do nothing
if os.IsNotExist(err) {
return nil
}
// Some other error!
return err
} else if fi.IsDir() {
// File is directory, ignore
return nil
}
// Load up the netrc file
net, err := netrc.ParseFile(path)
if err != nil {
return fmt.Errorf("Error parsing netrc file at %q: %s", path, err)
}
machine := net.FindMachine(u.Host)
if machine == nil {
// Machine not found, no problem
return nil
}
// Set the user info
u.User = url.UserPassword(machine.Login, machine.Password)
return nil
}
示例15: String
// The string representation of a Node is a URL.
// Please see ParseNode for a description of the format.
func (n *Node) String() string {
u := url.URL{Scheme: "enode"}
if n.Incomplete() {
u.Host = fmt.Sprintf("%x", n.ID[:])
} else {
addr := net.TCPAddr{IP: n.IP, Port: int(n.TCP)}
u.User = url.User(fmt.Sprintf("%x", n.ID[:]))
u.Host = addr.String()
if n.UDP != n.TCP {
u.RawQuery = "discport=" + strconv.Itoa(int(n.UDP))
}
}
return u.String()
}