-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REORG/MAJOR: Merge config-parser into client-native 5.0
- Loading branch information
Showing
644 changed files
with
74,911 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.