parser

package
v6.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2024 License: Apache-2.0 Imports: 26 Imported by: 0

README

HAProxy configuration parser

Autogenerated code

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

make gentypes

Example

package main

import (
    "github.com/haproxytech/client-native/v6/config-parser"
    "github.com/haproxytech/client-native/v6/config-parser/options"
    "github.com/haproxytech/client-native/v6/config-parser/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 preferred
        //
        // 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)
}

Documentation

Index

Constants

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

Variables

View Source
var DefaultSectionName = "" //nolint:gochecknoglobals

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
	FCGIApp               *Parsers
	CrtStore              *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)
	GetResult(sectionType Section, sectionName string, attribute string) ([]common.ReturnResultLine, 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
	SectionsDefaultsFromGet(sectionType Section, sectionName string) (string, error)
	SectionsDefaultsFromSet(sectionType Section, sectionName, defaultsSection 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
	DefaultSectionName 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) GetResult

func (p *Parsers) GetResult(attribute string) ([]common.ReturnResultLine, 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"
	FCGIApp    Section = "fcgi-app"
	CrtStore   Section = "crt-store"
	// 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