passfile

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2021 License: MIT Imports: 11 Imported by: 56

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Expand

func Expand(homeDir string, file string) string

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

func Match

func Match(u *dburl.URL, homeDir, 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(u *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(u *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, homeDir, name string) (*sql.DB, error)

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

func OpenURL added in v0.8.3

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

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

func Path

func Path(homeDir, 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(homeDir, 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(v Entry) bool

Equals returns true when v 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