passfile

package
v0.8.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2021 License: MIT Imports: 12 Imported by: 64

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)
	}
}
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())
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Expand

func Expand(u *user.User, file string) string

Expand expands the tilde (~) in the front of a path to a the supplied directory.

func Match

func Match(u *user.User, v *dburl.URL, name string) (*url.Userinfo, error)

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

func MatchEntries(v *dburl.URL, entries []Entry) (*url.Userinfo, error)

MatchEntries returns a Userinfo when the normalized v is found in entries.

func MatchFile added in v0.8.1

func MatchFile(v *dburl.URL, file string) (*url.Userinfo, error)

MatchFile returns a Userinfo from a passfile entry matching database URL v read from the specified file.

func Open added in v0.8.2

func Open(urlstr, name string) (*sql.DB, error)

Open opens a database connection for the provided URL, reading the named passfile in the current user's home directory.

func OpenURL added in v0.8.3

func OpenURL(v *dburl.URL, name string) (*sql.DB, error)

OpenURL opens a database connection for the provided URL, reading the named passfile in the current user's home directory.

func Path

func Path(u *user.User, name string) string

Path returns the expanded path to the password file for name.

Uses $HOME/.<name>, overridden by environment variable $ENV{NAME} (for example, ~/.usqlpass and $ENV{USQLPASS}).

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

func Entries(u *user.User, name string) ([]Entry, error)

Entries returns the entries for the specified passfile name.

func NewEntry

func NewEntry(v []string) Entry

NewEntry creates a new passfile entry.

func Parse

func Parse(r io.Reader) ([]Entry, error)

Parse parses passfile entries from the reader.

func ParseFile

func ParseFile(file string) ([]Entry, error)

ParseFile parses passfile entries contained in file.

func (Entry) Equals

func (entry Entry) Equals(b Entry) bool

Equals returns true when b matches the entry.

type ErrEmptyField

type ErrEmptyField struct {
	Line  int
	Field int
}

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"
)

func (Error) Error

func (err Error) Error() string

Error satisfies the error interface.

type FileError

type FileError struct {
	File string
	Err  error
}

FileError is a file error.

func (*FileError) Error

func (err *FileError) Error() string

Error satisfies the error interface.

func (*FileError) Unwrap

func (err *FileError) Unwrap() error

Unwrap satisfies the unwrap interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL