pgservices

package module
v0.0.0-...-ff022d4 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2017 License: GPL-3.0 Imports: 8 Imported by: 0

README

pgservices

A little Go module to parse and return details from a Postgres Services (pg_service.conf) file.

I like using the definitions in the pg_service.conf file to connect to Postgres, but didn't find any Go packages that quite fit for me. As a result, I wrote this little module to help me load pg_service.conf contents and build a struct out of the contents. This should make it easy to then generate your own connection pg string and connect to the database.

This package makes use of the Go INI lib to do all of the heavy lifting, but does a small amount of additional checking before loading the pg_service.conf contents.

Synopsis

You should really only need to use the ParsePgServices function to make use of this package.

package main

import (
    "fmt"
    "os"

    "github.com/dudefellah/pgservices"
)

func main() {
// Load pg services
    fileReader, err := os.Open("/etc/postgresql-common/pg_service.conf")
    if err != nil {
        fmt.Printf("Error opening file %v\n", err)
        os.Exit(1)
    }

    defer fileReader.Close()

    pgServices, err := pgservices.ParsePgServices(fileReader)
    if err != nil {
        fmt.Printf("Crap! %v\n", err)
        os.Exit(1)
    }

    connectionString := myCustomFunctionThatGeneratesAConnectionStringFromTheParsedPgService(pgServices)
    ...
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SslModes = []string{
	"disable",
	"allow",
	"prefer",
	"require",
	"verify-ca",
	"verify-full",
}

SslModes makes it easier to check the sslmode option

Functions

This section is empty.

Types

type PostgresServiceGroup

type PostgresServiceGroup struct {
	Category map[string]postgresService
}

PostgresServiceGroup is the object that holds all of the service definitions found in a pg_service.conf file. It's basically the parsed version of that file

No public methods are exposed, so you can just get any of the details you'd like by accessing PostgresServiceGroup's map of postgresService objects, each of which contains all of the necessary pg connection data

func New

func New(
	bufReader io.Reader,
) *PostgresServiceGroup

New function for creating PostgresServiceGroup objects. This is just handy since we need to make sure we initialize the map contained in the struct during creation and this function does that for us.

func ParsePgServices

func ParsePgServices(
	bufReader io.Reader,
) (*PostgresServiceGroup, error)

ParsePgServices parses the pg_services.conf file contents by reading the supplied io.Reader object. Results are dropped into the PostgresServiceGroup object pointer and returned

Jump to

Keyboard shortcuts

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