Help us shape the future of Go! Take the 2026 Go Developer Survey

parser

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2020 License: Apache-2.0 Imports: 18 Imported by: 1

README

HAProxy

HAProxy configuration parser

autogenerated code

if you change types/types.go you need to run

go run go-generate.go $(pwd)
Contributing

For commit messages and general style please follow the haproxy project's CONTRIBUTING guide and use that where applicable.

Please use golangci-lint run from github.com/golangci/golangci-lint for linting code.

Example
package main

import (
    "github.com/haproxytech/config-parser/v2"
    // ...
)
// ...

func main() {
    p := parser.Parser{}
    err := p.LoadData("/path/to/haproxy/file.cfg")
    log.Println(err)
    log.Println(p.String())

    {
        data, _ := p.Get(parser.Comments, parser.CommentsSectionName, "# _version", true)
        if err == errors.ErrFetch {
            log.Panicln("we have an fetch error !!")
        }
        ver, _ := data.(*types.Int64C)
        ver.Value = ver.Value + 1
    }

    {
        p.Set(parser.Frontends, "http", "option forwardfor", types.OptionForwardFor{})
    }

    {
        data, err := p.Get(parser.Global, parser.GlobalSectionName, "stats socket")
        if err != nil {
            log.Panicln(err)
        }
        val, _ := data.([]types.Socket)
        log.Println(val[0])
        val[0].Path = "$PWD/haproxy-runtime-api.1.sock"
        log.Println(val[0])
    }

    {
        data, err := p.Get(parser.Global, parser.GlobalSectionName, "daemon")
        log.Println(data, err)
        if err == errors.ErrFetch {
            log.Panicln("we have an fetch error !!")
        }
        //remove it
        p.Set(parser.Global, parser.GlobalSectionName, "daemon", nil)
    }

    {
        datar, err := p.Get(parser.Resolvers, "ns1", "nameserver")
        if err == nil {
            ns := datar.([]types.Nameserver)
            log.Println(ns[0].Name, ns[0].Address)
            log.Println(ns[1].Name, ns[1].Address)
            ns[1].Name = "hahaha"
            ns[0].Address = "0.0.0.0:8080"
        }
        datar, err = p.Get(parser.Resolvers, "ns1", "nameserver")
        if err == nil {
            ns := datar.([]types.Nameserver)
            log.Println(ns[0].Name, ns[0].Address)
            log.Println(ns[1].Name, ns[1].Address)
        }
    }

    {
        log.Println("nbproc ==================================================")
        data, err := p.Get(parser.Global, parser.GlobalSectionName, "nbproc")
        if err != nil {
            log.Println(err)
        } else {
            d := data.(*types.Int64C)
            log.Println(d.Value)
            d.Value = 5
        }
    }

    p.Save(configFilename)
}

License

Apache License 2.0

Documentation

Index

Constants

View Source
const (
	CommentsSectionName = "data"
	GlobalSectionName   = "data"
	DefaultSectionName  = "data"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfiguredParsers

type ConfiguredParsers struct {
	State                 string
	ActiveComments        []string
	ActiveSectionComments []string
	Active                *Parsers
	Previous              *Parsers

	Comments   *Parsers
	Defaults   *Parsers
	Global     *Parsers
	Frontend   *Parsers
	Backend    *Parsers
	Listen     *Parsers
	Resolver   *Parsers
	Userlist   *Parsers
	Peers      *Parsers
	Mailers    *Parsers
	Cache      *Parsers
	Program    *Parsers
	HTTPErrors *Parsers
	Ring       *Parsers
	//spoe parsers
	SPOEAgent   *Parsers
	SPOEGroup   *Parsers
	SPOEMessage *Parsers
	// contains filtered or unexported fields
}

type Parser

type Parser struct {
	Parsers map[Section]map[string]*Parsers
	// contains filtered or unexported fields
}

Parser reads and writes configuration on given file

func (*Parser) Delete

func (p *Parser) Delete(sectionType Section, sectionName string, attribute string, index ...int) error

Delete remove attribute on defined index, in case of single attributes, index is ignored

func (*Parser) Get

func (p *Parser) Get(sectionType Section, sectionName string, attribute string, createIfNotExist ...bool) (common.ParserData, error)

Get get attribute from defaults section

func (*Parser) GetOne

func (p *Parser) GetOne(sectionType Section, sectionName string, attribute string, index ...int) (common.ParserData, error)

GetOne get attribute from defaults section

func (*Parser) HasParser

func (p *Parser) HasParser(sectionType Section, attribute string) bool

HasParser checks if we have a parser for attribute

func (*Parser) Insert

func (p *Parser) Insert(sectionType Section, sectionName string, attribute string, data common.ParserData, index ...int) error

Insert put attribute on defined index, in case of single attributes, index is ignored

func (*Parser) LoadData

func (p *Parser) LoadData(filename string) error

func (*Parser) ParseData

func (p *Parser) ParseData(dat string) error

func (*Parser) Process added in v2.0.3

func (p *Parser) Process(reader io.Reader) error

func (*Parser) ProcessLine

func (p *Parser) ProcessLine(line string, parts, previousParts []string, comment string, config ConfiguredParsers) ConfiguredParsers

ProcessLine parses line plus determines if we need to change state

func (*Parser) Save

func (p *Parser) Save(filename string) error

func (*Parser) SectionsCreate

func (p *Parser) SectionsCreate(sectionType Section, sectionName string) error

SectionsCreate creates one section of sectionType

func (*Parser) SectionsDelete

func (p *Parser) SectionsDelete(sectionType Section, sectionName string) error

SectionsDelete deletes one section of sectionType

func (*Parser) SectionsGet

func (p *Parser) SectionsGet(sectionType Section) ([]string, error)

SectionsGet lists all sections of certain type

func (*Parser) Set

func (p *Parser) Set(sectionType Section, sectionName string, attribute string, data common.ParserData, index ...int) error

Set sets attribute from defaults section, can be nil to disable/remove

func (*Parser) String

func (p *Parser) String() string

String returns configuration in writable form

type ParserInterface

type ParserInterface interface {
	Init()
	Parse(line string, parts, previousParts []string, comment string) (changeState string, err error)
	PreParse(line string, parts, previousParts []string, preComments []string, comment string) (changeState string, err error)
	GetParserName() string
	Get(createIfNotExist bool) (common.ParserData, error)
	GetOne(index int) (common.ParserData, error)
	Delete(index int) error
	Insert(data common.ParserData, index int) error
	Set(data common.ParserData, index int) error
	ResultAll() ([]common.ReturnResultLine, []string, error)
}

type Parsers

type Parsers struct {
	Parsers        map[string]ParserInterface
	ParserSequence []Section
	PreComments    []string
	PostComments   []string
}

func (*Parsers) Delete

func (p *Parsers) Delete(attribute string, index ...int) error

func (*Parsers) Get

func (p *Parsers) Get(attribute string, createIfNotExist ...bool) (common.ParserData, error)

func (*Parsers) GetOne

func (p *Parsers) GetOne(attribute string, index ...int) (common.ParserData, error)

func (*Parsers) HasParser

func (p *Parsers) HasParser(attribute string) bool

HasParser checks if we have a parser for attribute

func (*Parsers) Insert

func (p *Parsers) Insert(attribute string, data common.ParserData, index ...int) error

func (*Parsers) Set

func (p *Parsers) Set(attribute string, data common.ParserData, index ...int) error

Set sets data in parser, if you can have multiple items, index is a must

type Section

type Section string
const (
	Comments   Section = "#"
	Defaults   Section = "defaults"
	Global     Section = "global"
	Resolvers  Section = "resolvers"
	UserList   Section = "userlist"
	Peers      Section = "peers"
	Mailers    Section = "mailers"
	Frontends  Section = "frontend"
	Backends   Section = "backend"
	Listen     Section = "listen"
	Cache      Section = "cache"
	Program    Section = "program"
	HTTPErrors Section = "http-errors"
	Ring       Section = "ring"
	//spoe sections
	SPOEAgent   Section = "spoe-agent"
	SPOEGroup   Section = "spoe-group"
	SPOEMessage Section = "spoe-message"
)

Directories

Path Synopsis
Code generated by go generate; DO NOT EDIT.
Code generated by go generate; DO NOT EDIT.
extra
Code generated by go generate; DO NOT EDIT.
Code generated by go generate; DO NOT EDIT.
filters
Code generated by go generate; DO NOT EDIT.
Code generated by go generate; DO NOT EDIT.
http
Code generated by go generate; DO NOT EDIT.
Code generated by go generate; DO NOT EDIT.
simple
Code generated by go generate; DO NOT EDIT.
Code generated by go generate; DO NOT EDIT.
stats
Code generated by go generate; DO NOT EDIT.
Code generated by go generate; DO NOT EDIT.
tcp
Code generated by go generate; DO NOT EDIT.
Code generated by go generate; DO NOT EDIT.
parsers
Code generated by go generate; DO NOT EDIT.
Code generated by go generate; DO NOT EDIT.

Jump to

Keyboard shortcuts

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