parser

package module
v4.0.0-rc4 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2022 License: Apache-2.0 Imports: 23 Imported by: 9

README

HAProxy

HAProxy configuration parser

Contributors License

autogenerated code

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

make generate
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/v4"
    "github.com/haproxytech/config-parser/v4/options"
    "github.com/haproxytech/config-parser/v4/parsers/http/actions"
    // ...
)
// ...

func main() {
    p, err := parser.New(options.Path("config.cfg"))
    /* p, err := parser.New(
        options.UseMd5Hash,
        options.Path("config.cfg")
    )*/
    if err != nil {
        log.Panic(err)
    }

    {
        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{})
    }

    {
        // for options that can exists multiple times in config Insert is preffered
        //
        // setting http-request & http-response is a bit different
        // since they accept multiple structs
        httpRequestActionDeny := &actions.Deny{
            DenyStatus: "0",
            Cond:       "unless",
            CondTest:   "{ src 127.0.0.1 }",
        }
        err = p.Insert(parser.Backends, "web_servers", "http-request", httpRequestActionDeny)
        // you can also choose index where action should be inserted
        err = p.Insert(parser.Backends, "web_servers", "http-request", httpRequestActionDeny, 2)
    }

    {
        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
	HasDefaultParser      bool
	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
	LogForward            *Parsers
	// spoe parsers
	SPOEAgent   *Parsers
	SPOEGroup   *Parsers
	SPOEMessage *Parsers
	// contains filtered or unexported fields
}

type Parser

type Parser interface {
	LoadData(path string) error
	Process(reader io.Reader) error
	String() string
	Save(filename string) error
	StringWithHash() (string, error)
	Get(sectionType Section, sectionName string, attribute string, createIfNotExist ...bool) (common.ParserData, error)
	GetPreComments(sectionType Section, sectionName string, attribute string) ([]string, error)
	GetOne(sectionType Section, sectionName string, attribute string, index ...int) (common.ParserData, error)
	SectionsGet(sectionType Section) ([]string, error)
	SectionsDelete(sectionType Section, sectionName string) error
	SectionsCreate(sectionType Section, sectionName string) error
	Set(sectionType Section, sectionName string, attribute string, data common.ParserData, index ...int) error
	SetPreComments(sectionType Section, sectionName string, attribute string, preComment []string) error
	Delete(sectionType Section, sectionName string, attribute string, index ...int) error
	Insert(sectionType Section, sectionName string, attribute string, data common.ParserData, index ...int) error
	HasParser(sectionType Section, attribute string) bool
	SetLoggerState(active bool) error
}

func New

func New(opt ...options.ParserOption) (Parser, error)

type ParserInterface

type ParserInterface interface {
	Init()
	Parse(line string, parts []string, comment string) (changeState string, err error)
	PreParse(line string, parts []string, preComments []string, comment string) (changeState string, err error)
	GetParserName() string
	Get(createIfNotExist bool) (common.ParserData, error)
	GetPreComments() ([]string, 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
	SetPreComments(preComment []string)
	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) GetPreComments

func (p *Parsers) GetPreComments(attribute string) ([]string, 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

func (*Parsers) SetPreComments

func (p *Parsers) SetPreComments(attribute string, preComment []string) error

SetPreComments sets comment lines before parser

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"
	LogForward Section = "log-forward"
	// spoe sections
	SPOEAgent   Section = "spoe-agent"
	SPOEGroup   Section = "spoe-group"
	SPOEMessage Section = "spoe-message"
)

type UnlockError

type UnlockError struct{}

func (UnlockError) Error

func (e UnlockError) Error() string

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.
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