Skip to content

Commit

Permalink
Added ability to exclude lines or non-native checks, v0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
abulimov committed Apr 19, 2016
1 parent d4246e2 commit b0b99a9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v0.5.0 [2016-04-19]

- Added ability to exclude some config lines based on regexp for non-native checks
- Added more filtering for HAProxy output
- Refactored main func to improve readability

## v0.4.1 [2016-04-18]

- Added ability to exclude some config lines based on regexp
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ or build from source.
**To get more warnings you need a local HAProxy executable.**
Install it with [Homebrew](http://brew.sh) on OS X or package manager of your choice on Linux.


### Building from source

You need working Go compiler.
Expand All @@ -47,7 +46,7 @@ Also you can manually disable running local HAProxy binary in check mode with

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.
pattern with `--ignore='some_regexp'` flag.

```console
haproxy-lint /etc/haproxy/haproxy.cfg
Expand Down
51 changes: 37 additions & 14 deletions haproxy-lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// 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 @@ -29,13 +28,39 @@ import (
"github.com/abulimov/haproxy-lint/lib"
)

var version = "0.4.1"
var version = "0.5.0"

func myUsage() {
fmt.Printf("Usage: %s [OPTIONS] haproxy.cfg\n", os.Args[0])
flag.PrintDefaults()
}

func checkWithHAProxy(config []string, filePath string, createTempFile bool) ([]lib.Problem, error) {
if createTempFile {
// we need to create temp file with content
tempFilePath, err := lib.CreateTempConfig(config)
if err != nil {
log.Printf("Failed to create temp file to filter config: %v\n", err)
} else {
defer os.Remove(tempFilePath) // clean up
filePath = tempFilePath
}
}
return lib.RunHAProxyCheck(filePath)
}

func getConfig(filePath, ignorePattern string) ([]string, error) {
config, err := lib.ReadConfigFile(filePath)
if err != nil {
return nil, err
}
// if we need to strip some strings from config (for example, Jinja2 conditionals)
if ignorePattern != "" {
return lib.Filter(config, ignorePattern), nil
}
return config, nil
}

func main() {
jsonFlag := flag.Bool("json", false, "Output in json")
haproxyFlag := flag.Bool("run-haproxy", true, "Try to run HAProxy binary in check mode")
Expand All @@ -58,26 +83,24 @@ func main() {

var problems []lib.Problem
useHAProxy := *haproxyFlag

config, err := getConfig(filePath, *ignoreFlag)
if err != nil {
log.Fatal(err)
}

if useHAProxy {
haproxyProblems, err := lib.RunHAProxyCheck(filePath)
createTempFile := *ignoreFlag != ""
haproxyProblems, err := checkWithHAProxy(config, filePath, createTempFile)
if err != nil {
log.Println(err)
log.Printf("Failed to run HAProxy in check mode: %v\n", err)
// don't filter native checks as we failed to run HAProxy
useHAProxy = false
} else {
problems = append(problems, haproxyProblems...)
}
}

config, err := lib.ReadConfigFile(filePath)
if err != nil {
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
18 changes: 18 additions & 0 deletions lib/haproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strconv"
Expand Down Expand Up @@ -82,3 +83,20 @@ func RunHAProxyCheck(filePath string) ([]Problem, error) {
}
return ParseHaproxyOutput(&out)
}

// CreateTempConfig creates temporary file, containing given lines.
// If no error returned, this file needs to be removed by the caller.
func CreateTempConfig(lines []string) (string, error) {
tmpfile, err := ioutil.TempFile("", "haproxy-lint")
if err != nil {
return "", err
}
for _, line := range lines {
_, err := tmpfile.Write([]byte(line + "\n"))
if err != nil {
os.Remove(tmpfile.Name())
return "", err
}
}
return tmpfile.Name(), nil
}

0 comments on commit b0b99a9

Please sign in to comment.