Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ dir2consul uses environment variables to override default configuration values.

* D2C_CONSUL_KEY_PREFIX is the path prefix to prepend to all consul keys. Default: ""
* D2C_DIRECTORY is the directory we should walk. Default: local
* D2C_IGNORE_DIRS is a comma delimited list of directory patterns to ignore when walking the file system. Reference filepath.Match for pattern syntax. Default: .git
* D2C_IGNORE_TYPES is a comma delimited list of file suffixes to ignore when walking the file system. Default: ""
* D2C_IGNORE_DIR_REGEX is a PCRE regular expression that matches directories we ignore when walking the file system. Default: "^\.git|^\.github"
* D2C_IGNORE_FILE_REGEX is a PCRE regular expression that matches files we ignore when walking the file system. Default: "README\.md"

Additionally, Consul specific configuration variables are documented [here](https://www.consul.io/docs/commands/index.html#environment-variables).

Expand Down
61 changes: 25 additions & 36 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/hashicorp/consul/api"
Expand All @@ -21,18 +22,32 @@ var ConsulKeyPrefix = getenv("D2C_CONSUL_KEY_PREFIX", "")
// Directory is the directory we should walk
var Directory = getenv("D2C_DIRECTORY", "local")

// IgnoreDirs is a comma delimited list of directory patterns to ignore when walking the file system
var IgnoreDirs = strings.Split(getenv("D2C_IGNORE_DIRS", ".git"), ",")
// IgnoreDirRegex is a PCRE regular expression that matches directories we ignore when walking the file system
var IgnoreDirRegex = getenv("D2C_IGNORE_DIR_REGEX", `^\.git|^\.github`)

// IgnoreTypes is a comma delimited list of file suffixes to ignore when walking the file system
var IgnoreTypes = strings.Split(getenv("D2C_IGNORE_TYPES", ""), ",")
// IgnoreFileRegex is a PCRE regular expression that matches files we ignore when walking the file system
var IgnoreFileRegex = getenv("D2C_IGNORE_FILE_REGEX", `README\.md`)

var dirIgnoreRe, fileIgnoreRe *regexp.Regexp

func main() {
log.Println("dir2consul starting with configuration:")
log.Println("D2C_CONSUL_KEY_PREFIX:", ConsulKeyPrefix)
log.Println("D2C_DIRECTORY:", Directory)
log.Println("D2C_IGNORE_DIRS:", IgnoreDirs)
log.Println("D2C_IGNORE_TYPES:", IgnoreTypes)
log.Println("D2C_IGNORE_DIR_REGEX:", IgnoreDirRegex)
log.Println("D2C_IGNORE_FILE_REGEX:", IgnoreFileRegex)

var err error

// Compile regular expressions
dirIgnoreRe, err = regexp.Compile(IgnoreDirRegex)
if err != nil {
log.Fatal("Ignore Dir Regex failed to compile:", err)
}
fileIgnoreRe = regexp.MustCompile(IgnoreFileRegex)
if err != nil {
log.Fatal("Ignore File Regex failed to compile:", err)
}

os.Chdir(Directory)

Expand Down Expand Up @@ -102,11 +117,13 @@ func LoadKeyValuesFromDisk(kv *kv.List) error {
return err
}

if info.Mode().IsDir() && ignoreDir(path, IgnoreDirs) {
// Skip directories we want to ignore
if info.Mode().IsDir() && dirIgnoreRe.MatchString(path) {
return filepath.SkipDir
}

if info.Mode().IsDir() || !info.Mode().IsRegular() || ignoreFile(path, IgnoreTypes) {
// Skip directories, non-regular files, and files we want to ignore
if info.Mode().IsDir() || !info.Mode().IsRegular() || fileIgnoreRe.MatchString(path) {
return nil
}

Expand Down Expand Up @@ -149,34 +166,6 @@ func LoadKeyValuesFromDisk(kv *kv.List) error {
})
}

// ignoreDir returns true if the directory should be ignored. Reference filepath.Match for pattern syntax
func ignoreDir(path string, ignoreDirs []string) bool {
for _, dir := range ignoreDirs {
match, err := filepath.Match(dir, path)
if err != nil {
log.Fatal(err) // xxx: better error message
}
if match {
return true
}
}
return false
}

// ignoreFile returns true if the file should be ignored based on file extension matching
func ignoreFile(path string, ignoreExtensions []string) bool {
pathExtension := filepath.Ext(path)
if pathExtension == "" {
return false
}
for _, ignoreExtension := range ignoreExtensions {
if pathExtension == ignoreExtension {
return true
}
}
return false
}

// func loadHclFile() error {
// return nil
// }
Expand Down
72 changes: 0 additions & 72 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,73 +1 @@
package main

import "testing"

func TestIgnoreDir(t *testing.T) {
fixtures := []struct {
dir string
dirs []string
result bool
}{
{".git", []string{".git", "foo", "bar"}, true},
{".Git", []string{".git", "foo", "bar"}, false},
{"foo", []string{".git", "foo", "bar"}, true},
{"fOo", []string{".git", "foo", "bar"}, false},
{"bar", []string{".git", "foo", "bar"}, true},
{"baR", []string{".git", "foo", "bar"}, false},
{".git/a", []string{".git/*", "foo", "bar"}, true},
{".git/a", []string{".git", "foo", "bar"}, false},
{"a/.git", []string{".git", "foo", "bar"}, false},
{"a/.git/", []string{".git", "foo", "bar"}, false},
{"foo/a", []string{".git", "foo/a", "bar"}, true},
{"fo/oa", []string{".git", "foo", "bar"}, false},
{"a/foo", []string{".git", "foo", "bar"}, false},
{"a/foo/", []string{".git", "foo", "bar"}, false},
{"bar/a", []string{".git", "foo", "bar"}, false},
{"ba/ra", []string{".git", "foo", "bar"}, false},
{"a/bar", []string{".git", "foo", "bar"}, false},
{"a/bar/", []string{".git", "foo", "bar"}, false},
{"b/.git/a", []string{".git", "foo", "bar"}, false},
{"b/foo/a", []string{".git", "foo", "bar"}, false},
{"b/bar/a", []string{".git", "foo", "bar"}, false},
{".bar", []string{".git", "foo", "bar"}, false},
{"foo.bar", []string{".git", "foo", "bar"}, false},
{"foo.bar", []string{".git", "*foo", "bar"}, false},
{"foo.bar", []string{".git", "*foo*", "bar"}, true},
{"foo.bar", []string{".git", "foo/*", "bar"}, false},
{"foo.pdf", []string{".git", "foo", "bar"}, false},
{"bar.xyz.xyz", []string{".git", "foo", "bar"}, false},
}

for _, s := range fixtures {
if r := ignoreDir(s.dir, s.dirs); r != s.result {
t.Errorf("ignoreDir Failed on %s and %s", s.dir, s.dirs)
}
}
}

func TestIgnoreFile(t *testing.T) {
fixtures := []struct {
filename string
ignoreExt []string
result bool
}{
{"a.db", []string{".db", ".foo", ".bar"}, true},
{"a.foo", []string{".db", ".foo", ".bar"}, true},
{"a.bar", []string{".db", ".foo", ".bar"}, true},
{"a/a.db", []string{".db", ".foo", ".bar"}, true},
{"foo", []string{".db", ".foo", ".bar"}, false},
{"a.foo/foo", []string{".db", ".foo", ".bar"}, false},
{"a/a.foo/foo", []string{".db", ".foo", ".bar"}, false},
{"a.db/a.foo/a.bar/bar", []string{".db", ".foo", ".bar"}, false},
{"foo.txt", []string{".db", ".foo", ".bar"}, false},
{"a.foo/foo.txt", []string{".db", ".foo", ".bar"}, false},
{"a/a.foo/foo.txt", []string{".db", ".foo", ".bar"}, false},
{"a.db/a.foo/a.bar/bar.txt", []string{".db", ".foo", ".bar"}, false},
}

for _, s := range fixtures {
if r := ignoreFile(s.filename, s.ignoreExt); r != s.result {
t.Errorf("ignoreFile Failed on %s and %s", s.filename, s.ignoreExt)
}
}
}