Skip to content

Commit

Permalink
Added --ignore option, v0.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
abulimov committed Apr 18, 2016
1 parent b10de83 commit 48fd65a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v0.4.1 [2016-04-18]

- Added ability to exclude some config lines based on regexp

## v0.4.0 [2016-04-08]

- Execute checks in parallel
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ with `--json`flag (useful for editor plugins integration).
Also you can manually disable running local HAProxy binary in check mode with
`--run-haproxy=false` flag.

If the config you are checking will be later processed by some template engine,
you can tell haproxy-lint to exclude some lines in config file based on regexp
pattern (works only for native checks) with `--ignore='some_regexp'` flag.

```console
haproxy-lint /etc/haproxy/haproxy.cfg
24:0:warning: ACL h_some declared but not used
Expand Down
11 changes: 10 additions & 1 deletion haproxy-lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
// -run-haproxy run haproxy binary in check mode and parse
// its output to extract alerts and warnigns
//
// -ignore regexp ignore lines in config file matching given regexp
// (works only for native checks)
//
// -v show version and exit
package main

Expand All @@ -26,7 +29,7 @@ import (
"github.com/abulimov/haproxy-lint/lib"
)

var version = "0.4.0"
var version = "0.4.1"

func myUsage() {
fmt.Printf("Usage: %s [OPTIONS] haproxy.cfg\n", os.Args[0])
Expand All @@ -37,6 +40,7 @@ func main() {
jsonFlag := flag.Bool("json", false, "Output in json")
haproxyFlag := flag.Bool("run-haproxy", true, "Try to run HAProxy binary in check mode")
versionFlag := flag.Bool("version", false, "print haproxy-lint version and exit")
ignoreFlag := flag.String("ignore", "", "ignore lines in config file matching this regexp")

flag.Usage = myUsage

Expand Down Expand Up @@ -69,6 +73,11 @@ func main() {
log.Fatal(err)
}

// if we need to strip some strings from config (for example, Jinja2 conditionals)
if *ignoreFlag != "" {
config = lib.Filter(config, *ignoreFlag)
}

sections := lib.GetSections(config)

// if we have local haproxy executable we shouldn't run
Expand Down
21 changes: 21 additions & 0 deletions lib/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"io/ioutil"
"os"
"regexp"
"strings"
)

Expand All @@ -13,6 +14,7 @@ func ReadConfig(f io.Reader) ([]string, error) {
if err != nil {
return nil, err
}

return strings.Split(string(b), "\n"), nil
}

Expand Down Expand Up @@ -41,3 +43,22 @@ func GetUsage(keyword, line string) string {
}
return ""
}

// Filter helps us replace lines matching pattern with empty strings.
// Helpfull when we are trying to strip some template engine conditionals.
func Filter(lines []string, pattern string) []string {
var result []string
if pattern == "" {
return lines
}

re := regexp.MustCompile(pattern)
for _, line := range lines {
if re.MatchString(line) {
result = append(result, "")
} else {
result = append(result, line)
}
}
return result
}
59 changes: 58 additions & 1 deletion lib/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package lib

import "testing"
import (
"reflect"
"testing"
)

func TestGetUsage(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -49,3 +52,57 @@ func TestGetUsage(t *testing.T) {
}
}
}

func TestFilter(t *testing.T) {
tests := []struct {
// Test description.
name string
// Parameters.
lines []string
pattern string
// Expected results.
want []string
}{
{
name: "File without lines to filter",
lines: []string{
"global",
" daemon",
" maxconn 256",
},
pattern: "{%",
want: []string{
"global",
" daemon",
" maxconn 256",
},
},
{
name: "File with lines to filter",
lines: []string{
"global",
" daemon",
" {% if haproxy_domain == 'ru' %}",
" maxconn 1024",
" {% else %}",
" maxconn 256",
" {% endif %}",
},
pattern: "{%",
want: []string{
"global",
" daemon",
"",
" maxconn 1024",
"",
" maxconn 256",
"",
},
},
}
for _, tt := range tests {
if got := Filter(tt.lines, tt.pattern); !reflect.DeepEqual(got, tt.want) {
t.Errorf("%q. Filter() = %v, want %v", tt.name, got, tt.want)
}
}
}

0 comments on commit 48fd65a

Please sign in to comment.