Skip to content

Commit

Permalink
Merge pull request #5 from markspolakovs/master
Browse files Browse the repository at this point in the history
Support multiple rule paths as parameters
  • Loading branch information
martinhaus authored Oct 1, 2021
2 parents 43e0054 + 31fdf55 commit 890373a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The program loops over all of the alerting rules until it finds an override rule

# Usage

The application loads all of the valid files from specified directory and applies overrides if they are defined.
The application loads all of the valid files matching the specified globs and applies overrides if they are defined.

For examples look at `examples` directory.

Expand All @@ -35,9 +35,13 @@ groups:
## Running the program

```
./prometheus_merge <path_to_rules>
./prometheus_merge <glob> [<glob>]
```

The glob matching rules are per Go's [filepath.Glob](https://pkg.go.dev/path/filepath#Glob). (Note that you may need to quote the globs to prevent your shell expanding them first.)

**Note that the default behaviour has changed** - if `prometheus_merge` no longer produces output, you may need to change your folder paths to be globs (for example, `alerts/` to `alerts/*`).

The output is printed to stdout.

### Ansible integration
Expand Down
26 changes: 16 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"

Expand Down Expand Up @@ -139,15 +140,21 @@ func NegateFilterExpression(input string) string {
return strings.Join(exprFilterBodyElements[:], ",")
}

func getFilePaths(rootPath string) []string {
func getFilePaths(globs []string) []string {
var filePaths []string

files, err := ioutil.ReadDir(rootPath)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
filePaths = append(filePaths, rootPath+"/"+f.Name())
for _, root := range globs {
files, err := filepath.Glob(root)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
abspath, err := filepath.Abs(f)
if err != nil {
log.Fatal(err)
}
filePaths = append(filePaths, abspath)
}
}

return filePaths
Expand All @@ -168,12 +175,11 @@ func loadAlertFile(filePath string) *AlertFile {

func main() {

if len(os.Args) != 2 {
if len(os.Args) < 2 {
panic("No arguments provided")
}

filePaths := os.Args[1]
files := getFilePaths(filePaths)
files := getFilePaths(os.Args[1:])

var alertFiles []AlertFile

Expand Down

0 comments on commit 890373a

Please sign in to comment.