// Package pgpassfile is a parser PostgreSQL .pgpass files.
package pgpassfileimport ()// Entry represents a line in a PG passfile.typeEntrystruct { Hostname string Port string Database string Username string Password string}// Passfile is the in memory data structure representing a PG passfile.typePassfilestruct { Entries []*Entry}// ReadPassfile reads the file at path and parses it into a Passfile.func ( string) (*Passfile, error) { , := os.Open()if != nil {returnnil, }defer .Close()returnParsePassfile()}// ParsePassfile reads r and parses it into a Passfile.func ( io.Reader) (*Passfile, error) { := &Passfile{} := bufio.NewScanner()for .Scan() { := parseLine(.Text())if != nil { .Entries = append(.Entries, ) } }return , .Err()}// Match (not colons or escaped colon or escaped backslash)+. Essentially gives a split on unescaped// colon.var colonSplitterRegexp = regexp.MustCompile("(([^:]|(\\:)))+")// var colonSplitterRegexp = regexp.MustCompile("((?:[^:]|(?:\\:)|(?:\\\\))+)")// parseLine parses a line into an *Entry. It returns nil on comment lines or any other unparsable// line.func parseLine( string) *Entry {const ( = "\r" = "\n" ) = strings.TrimSpace()ifstrings.HasPrefix(, "#") {returnnil } = strings.Replace(, `\\`, , -1) = strings.Replace(, `\:`, , -1) := strings.Split(, ":")iflen() != 5 {returnnil }// Unescape escaped colons and backslashesfor := range { [] = strings.Replace([], , `\`, -1) [] = strings.Replace([], , `:`, -1) }return &Entry{Hostname: [0],Port: [1],Database: [2],Username: [3],Password: [4], }}// FindPassword finds the password for the provided hostname, port, database, and username. For a// Unix domain socket hostname must be set to "localhost". An empty string will be returned if no// match is found.//// See https://www.postgresql.org/docs/current/libpq-pgpass.html for more password file information.func ( *Passfile) (, , , string) ( string) {for , := range .Entries {if (.Hostname == "*" || .Hostname == ) && (.Port == "*" || .Port == ) && (.Database == "*" || .Database == ) && (.Username == "*" || .Username == ) {return .Password } }return""}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.