package pgservicefile
import (
)
type Service struct {
Name string
Settings map[string]string
}
type Servicefile struct {
Services []*Service
servicesByName map[string]*Service
}
func ( *Servicefile) ( string) (*Service, error) {
, := .servicesByName[]
if ! {
return nil, errors.New("not found")
}
return , nil
}
func ( string) (*Servicefile, error) {
, := os.Open()
if != nil {
return nil,
}
defer .Close()
return ParseServicefile()
}
func ( io.Reader) (*Servicefile, error) {
:= &Servicefile{}
var *Service
:= bufio.NewScanner()
:= 0
for .Scan() {
+= 1
:= .Text()
= strings.TrimSpace()
if == "" || strings.HasPrefix(, "#") {
} else if strings.HasPrefix(, "[") && strings.HasSuffix(, "]") {
= &Service{Name: [1 : len()-1], Settings: make(map[string]string)}
.Services = append(.Services, )
} else if != nil {
:= strings.SplitN(, "=", 2)
if len() != 2 {
return nil, fmt.Errorf("unable to parse line %d", )
}
:= strings.TrimSpace([0])
:= strings.TrimSpace([1])
.Settings[] =
} else {
return nil, fmt.Errorf("line %d is not in a section", )
}
}
.servicesByName = make(map[string]*Service, len(.Services))
for , := range .Services {
.servicesByName[.Name] =
}
return , .Err()
}