Skip to content

Commit

Permalink
Adding URL.Short func
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Shaw committed Mar 2, 2017
1 parent bf1ba90 commit 34edfac
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions dburl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,30 @@
// Standard URLs are of the form
// protocol+transport://user:pass@host/dbname?opt1=a&opt2=b
//
// For example, the following are URLs that can be processed using Parse or Open:
// postgres://user:pass@localhost/mydb
// mysql://user:pass@localhost/mydb
// For example, the following are URLs that can be processed using Parse or
// Open:
//
// postgres://user:pass@localhost/dbname
// pg://user:pass@localhost/dbname?sslmode=disable
// mysql://user:pass@localhost/dbname
// sqlserver://user:pass@remote-host.com/dbname
// oracle://user:pass@somehost.com/oracledb
// sqlite:/path/to/file.db
// file:myfile.sqlite3?loc=auto
//
// Protocol aliases:
//
// The following protocol aliases are available, and will be parsed according
// to the rules for their respective driver.
//
// Database (driver) | Aliases
// ------------------------------------------------------------------
// Microsoft SQL Server (mssql) | ms, sqlserver
// MySQL (mysql) | my, mariadb, maria, percona, aurora
// Oracle (ora) | or, oracle, oci8, oci
// PostgreSQL (postgres) | pg, postgresql, pgsql
// SQLite3 (sqlite3) | sq, sqlite, file
//
package dburl

import (
Expand Down Expand Up @@ -43,6 +62,33 @@ func (u *URL) String() string {
return p.String()
}

// Short provides a short description of the user, host, and database.
func (u *URL) Short() string {
s := u.Driver[:2]
if s == "po" {
s = "pg"
}

s += ":"

if u.User != nil {
if username := u.User.Username(); username != "" {
s += username + "@"
}
}
if u.Host != "" {
s += u.Host
}
if u.Path != "" && u.Path != "/" {
s += u.Path
}
if u.Opaque != "" {
s += u.Opaque
}

return s
}

// Parse parses a rawurl string and normalizes the scheme.
func Parse(rawurl string) (*URL, error) {
// parse url
Expand Down

0 comments on commit 34edfac

Please sign in to comment.