Documentation ¶
Overview ¶
Package passfile provides a mechanism for reading database credentials from passfiles.
Example (Entries) ¶
package main import ( "log" "os/user" "github.com/xo/dburl/passfile" ) func main() { u, err := user.Current() if err != nil { log.Fatal(err) } // read ~/.usqlpass or $ENV{USQLPASS} entries, err := passfile.Entries(u.HomeDir, "usqlpass") if err != nil { log.Fatal(err) } for i, entry := range entries { log.Printf("%d: %v", i, entry) } }
Output:
Example (Match) ¶
package main import ( "log" "os/user" "github.com/xo/dburl" "github.com/xo/dburl/passfile" ) func main() { v, err := user.Current() if err != nil { log.Fatal(err) } u, err := dburl.Parse("pg://") if err != nil { log.Fatal(err) } // read ~/.usqlpass or $ENV{USQLPASS} user, err := passfile.Match(u, v.HomeDir, "usqlpass") if err == nil { u.User = user } log.Println("url:", u.String()) }
Output:
Index ¶
- func Expand(homeDir string, file string) string
- func Match(u *dburl.URL, homeDir, name string) (*url.Userinfo, error)
- func MatchEntries(u *dburl.URL, entries []Entry, protocols ...string) (*url.Userinfo, error)
- func MatchFile(u *dburl.URL, file string, protocols ...string) (*url.Userinfo, error)
- func MatchProtocols(u *dburl.URL, homeDir, name string, protocols ...string) (*url.Userinfo, error)
- func Open(urlstr, homeDir, name string) (*sql.DB, error)
- func OpenURL(u *dburl.URL, homeDir, name string) (*sql.DB, error)
- func Path(homeDir, name string) string
- type Entry
- type ErrEmptyField
- type ErrInvalidEntry
- type Error
- type FileError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Match ¶
Match returns a Userinfo from a passfile entry matching database URL read from the file in $HOME/.<name> or $ENV{NAME}.
Equivalent to MatchFile(u, Path(homeDir, name), dburl.Protocols(u.Driver)...).
func MatchEntries ¶
MatchEntries returns a Userinfo when the normalized v is found in entries.
func MatchFile ¶
MatchFile returns a Userinfo from a passfile entry matching database URL v read from the specified file.
func MatchProtocols ¶
MatchProtocols returns a Userinfo from a passfile entry matching database URL read from the file in $HOME/.<name> or $ENV{NAME} using the specified protocols.
Equivalent to MatchFile(u, Path(homeDir, name), protocols...).
func Open ¶
Open opens a database connection for a URL, reading the named passfile in the home directory.
Types ¶
type Entry ¶
type Entry struct {
Protocol, Host, Port, DBName, Username, Password string
}
Entry is a passfile entry.
Corresponds to a non-empty line in a passfile.
func Entries ¶
Entries returns the entries for the specified passfile name.
Equivalent to ParseFile(Path(homeDir, name)).
type ErrEmptyField ¶
ErrEmptyField is the empty field error.
func (*ErrEmptyField) Error ¶
func (err *ErrEmptyField) Error() string
Error satisfies the error interface.
type ErrInvalidEntry ¶
type ErrInvalidEntry struct {
Line int
}
ErrInvalidEntry is the invalid entry error.
func (*ErrInvalidEntry) Error ¶
func (err *ErrInvalidEntry) Error() string
Error satisfies the error interface.
type Error ¶
type Error string
Error is a error.
const ( // ErrUnableToNormalizeURL is the unable to normalize URL error. ErrUnableToNormalizeURL Error = "unable to normalize URL" // ErrMustNotBeDirectory is the must not be directory error. ErrMustNotBeDirectory Error = "must not be directory" // ErrHasGroupOrWorldAccess is the has group or world access error. ErrHasGroupOrWorldAccess Error = "has group or world access" )