Skip to content

Commit

Permalink
REORG/MAJOR: Merge config-parser into client-native 5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
oliwer committed Sep 30, 2024
1 parent 7cb9574 commit 5011937
Show file tree
Hide file tree
Showing 644 changed files with 74,911 additions and 181 deletions.
5 changes: 4 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ issues:
# bugs of typecheck linter
- "undeclared name: `shellquote`"
- "github.com/kballard/go-shellquote\" imported but not used"
- "github.com/haproxytech/config-parser/v5/types\" imported but not used"
- "github.com/haproxytech/client-native/v5/config-parser/types\" imported but not used"
- "unused-parameter: parameter 'comment' seems to be unused"
- "unused-parameter: parameter 'parts' seems to be unused"
- "unused-parameter: parameter 'parserType' seems to be unused"

run:
skip-dirs:
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ e2e-docker:
docker build -f e2e/Dockerfile --build-arg GO_VERSION=${GO_VERSION} --build-arg HAPROXY_VERSION=${DOCKER_HAPROXY_VERSION} -t client-native-test:${DOCKER_HAPROXY_VERSION} .
docker run --rm -t client-native-test:${DOCKER_HAPROXY_VERSION}

.PHONY: gentypes
gentypes:
cd config-parser && go run generate/*.go ${PROJECT_PATH}/config-parser

.PHONY: spec
spec:
go run specification/build/build.go -file specification/haproxy-spec.yaml > specification/build/haproxy_spec.yaml

.PHONY: models
models: spec swagger-check
models: gentypes spec swagger-check
./bin/swagger generate model --additional-initialism=FCGI -f ${PROJECT_PATH}/specification/build/haproxy_spec.yaml -r ${PROJECT_PATH}/specification/copyright.txt -m models -t ${PROJECT_PATH}

.PHONY: swagger-check
Expand Down
113 changes: 113 additions & 0 deletions config-parser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
## HAProxy configuration parser

### autogenerated code
if you change types/types.go you need to run
```bash
make gentypes
```

### Example

```go
package main

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

```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions config-parser/bin/lint-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
V=$(./golangci-lint --version)

case "$V" in
*$GOLANGCI_LINT_VERSION*) echo "$V" ;;
*) curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(pwd) "v$GOLANGCI_LINT_VERSION" ;;
esac
142 changes: 142 additions & 0 deletions config-parser/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
Copyright 2019 HAProxy Technologies
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package common

import (
"strings"
)

// StringSplitIgnoreEmpty while spliting, removes empty items
func StringSplitIgnoreEmpty(s string, separators ...rune) []string {
f := func(c rune) bool {
willSplit := false
for _, sep := range separators {
if c == sep {
willSplit = true
break
}
}
return willSplit
}
return strings.FieldsFunc(s, f)
}

// StringSplitWithCommentIgnoreEmpty while splitting, removes empty items, if we have comment, separate it
func StringSplitWithCommentIgnoreEmpty(s string) (data []string, comment string) { //nolint:gocognit,nonamedreturns
data = []string{}

singleQuoteActive := false
doubleQuoteActive := false
var buff strings.Builder
for index, c := range s {
if !singleQuoteActive && !doubleQuoteActive {
if (c == '#' && index == 0) || (c == '#' && s[index-1] != '\\') {
if buff.Len() > 0 {
data = append(data, buff.String())
buff.Reset()
}
index++
for ; index < len(s); index++ {
if s[index] != ' ' {
break
}
}
comment = s[index:]
return data, comment
}
if (c == ' ' && index == 0) || (c == ' ' && s[index-1] != '\\') || c == '\t' {
if buff.Len() > 0 {
data = append(data, buff.String())
buff.Reset()
}
continue
}
}
buff.WriteRune(c)
if c == '"' {
if doubleQuoteActive {
if index == 0 || s[index-1] != '\\' {
doubleQuoteActive = false
}
} else if !singleQuoteActive {
if index == 0 || s[index-1] != '\\' {
doubleQuoteActive = true
}
}
}
if c == '\'' {
if singleQuoteActive {
singleQuoteActive = false
} else if !doubleQuoteActive {
if index == 0 || s[index-1] != '\\' {
singleQuoteActive = true
}
}
}
}
if buff.Len() > 0 {
data = append(data, buff.String())
}
return data, comment
}

// StringExtractComment checks if comment is added
func StringExtractComment(s string) string {
p := StringSplitIgnoreEmpty(s, '#')
if len(p) > 1 {
return p[len(p)-1]
}
return ""
}

// SplitRequest searches for "if" or "unless" and returns result
func SplitRequest(parts []string) (command, condition []string) { //nolint:nonamedreturns
if len(parts) == 0 {
return []string{}, []string{}
}
index := 0
found := false
for index < len(parts) {
switch parts[index] {
case "if", "unless":
found = true
}
if found {
break
}
index++
}
command = parts[:index]
condition = parts[index:]
return command, condition
}

// CutRight slices string around the last occurrence of sep returning the text
// before and after sep. The found result reports whether sep appears in s. If
// sep does not appear in s, cut returns s, "", false.
func CutRight(s, sep string) (before, after string, found bool) { //nolint:nonamedreturns
pos := strings.LastIndex(s, sep)
if pos < 0 {
before = s
found = false
} else {
before = s[:pos]
after = s[pos+len(sep):]
found = true
}
return before, after, found
}
Loading

0 comments on commit 5011937

Please sign in to comment.