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, "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() {
u, err := user.Current()
if err != nil {
log.Fatal(err)
}
v, err := dburl.Parse("pg://")
if err != nil {
log.Fatal(err)
}
// read ~/.usqlpass or $ENV{USQLPASS}
user, err := passfile.Match(u, v, "usqlpass")
if err == nil {
v.User = user
}
log.Println("url:", v.String())
}
Output:
Index ¶
- func Expand(u *user.User, file string) string
- func Match(u *user.User, v *dburl.URL, name string) (*url.Userinfo, error)
- func MatchEntries(v *dburl.URL, entries []Entry) (*url.Userinfo, error)
- func MatchFile(v *dburl.URL, file string) (*url.Userinfo, error)
- func Open(urlstr, name string) (*sql.DB, error)
- func OpenURL(v *dburl.URL, name string) (*sql.DB, error)
- func Path(u *user.User, 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 v read from the file in $HOME/.<name> or $ENV{NAME}.
Equivalent to MatchFile(v, Path(u, name))
func MatchEntries ¶ added in v0.8.1
MatchEntries returns a Userinfo when the normalized v is found in entries.
func MatchFile ¶ added in v0.8.1
MatchFile returns a Userinfo from a passfile entry matching database URL v read from the specified file.
func Open ¶ added in v0.8.2
Open opens a database connection for the provided URL, reading the named passfile in the current user's 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.
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 entrty 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" )