// Package pgservicefile is a parser for PostgreSQL service files (e.g. .pg_service.conf).
package pgservicefile import ( ) type Service struct { Name string Settings map[string]string } type Servicefile struct { Services []*Service servicesByName map[string]*Service } // GetService returns the named service. func ( *Servicefile) ( string) (*Service, error) { , := .servicesByName[] if ! { return nil, errors.New("not found") } return , nil } // ReadServicefile reads the file at path and parses it into a Servicefile. func ( string) (*Servicefile, error) { , := os.Open() if != nil { return nil, } defer .Close() return ParseServicefile() } // ParseServicefile reads r and parses it into a Servicefile. func ( io.Reader) (*Servicefile, error) { := &Servicefile{} var *Service := bufio.NewScanner() := 0 for .Scan() { += 1 := .Text() = strings.TrimSpace() if == "" || strings.HasPrefix(, "#") { // ignore comments and empty lines } else if strings.HasPrefix(, "[") && strings.HasSuffix(, "]") { = &Service{Name: [1 : len()-1], Settings: make(map[string]string)} .Services = append(.Services, ) } else { := strings.SplitN(, "=", 2) if len() != 2 { return nil, fmt.Errorf("unable to parse line %d", ) } := strings.TrimSpace([0]) := strings.TrimSpace([1]) .Settings[] = } } .servicesByName = make(map[string]*Service, len(.Services)) for , := range .Services { .servicesByName[.Name] = } return , .Err() }