Skip to content

Commit

Permalink
feat: configurable ignored files and directories
Browse files Browse the repository at this point in the history
  • Loading branch information
luissimas committed Jun 4, 2024
1 parent bc0443a commit b34935f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- [X] Read config
- [X] Find all files recursivelly
- [X] Parse markdown links
- [ ] Configurable ignore file patterns
- [X] Configurable ignore file patterns
- [ ] Get zettelkasten from git url
- [ ] Support private repositories (Maybe with Github's PAT?)
- [ ] Exclude links to non existing files
Expand Down
2 changes: 1 addition & 1 deletion cmd/zettelkasten-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func main() {
}

fs := os.DirFS(absolute_path)
collector := collector.NewCollector(fs)
collector := collector.NewCollector(fs, cfg.IgnoreFiles)

promHandler := promhttp.Handler()
http.Handle("/metrics", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
11 changes: 7 additions & 4 deletions internal/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ type Collector struct {
config CollectorConfig
}

func NewCollector(fileSystem fs.FS) Collector {
func NewCollector(fileSystem fs.FS, ignorePatterns []string) Collector {
return Collector{
config: CollectorConfig{
FileSystem: fileSystem,
IgnorePatterns: []string{".obsidian"},
IgnorePatterns: ignorePatterns,
},
}
}
Expand Down Expand Up @@ -61,9 +61,12 @@ func (c *Collector) collectMetrics() (Metrics, error) {
notes := make(map[string]NoteMetrics)

err := fs.WalkDir(c.config.FileSystem, ".", func(path string, dir fs.DirEntry, err error) error {
// Skip all files in ignored directories
// Skip ignored files or directories
if slices.Contains(c.config.IgnorePatterns, filepath.Base(path)) {
return filepath.SkipDir
if dir.IsDir() {
return filepath.SkipDir
}
return nil
}
// Skip other directories and non markdown files
if dir.IsDir() || filepath.Ext(path) != ".md" {
Expand Down
6 changes: 5 additions & 1 deletion internal/collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ created-at: "2024-05-29"
---
Link to [one](./one.md) and also a full link [[./dir1/dir2/three]] and a [[./dir1/two.md|full link with .md]]
`)},
"ignoredir/foo": {Data: []byte("Foo contents")},
"ignoredir/bar": {Data: []byte("Bar contents")},
"ignoredir/test.md": {Data: []byte("Test.md contents")},
"zettel/dir1/ignore.md": {Data: []byte("Ignore.md contents")},
}
c := NewCollector(fs)
c := NewCollector(fs, []string{"ignore.md", "ignoredir"})
expected := Metrics{
NoteCount: 4,
LinkCount: 7,
Expand Down
10 changes: 6 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ import (
type Config struct {
IP string `koanf:"ip" validate:"required|ip"`
Port int `koanf:"port" validate:"required|uint"`
LogLevel slog.Level `koanf:"log_level"`
ZettelkastenDirectory string `koanf:"zettelkasten_directory" validate:"required"`
LogLevel slog.Level `koanf:"log_level"`
IgnoreFiles []string `koanf:"ignore_files"`
}

func LoadConfig() (Config, error) {
k := koanf.New(".")

// Set default values
k.Load(structs.Provider(Config{
IP: "0.0.0.0",
Port: 6969,
LogLevel: slog.LevelInfo,
IP: "0.0.0.0",
Port: 6969,
LogLevel: slog.LevelInfo,
IgnoreFiles: []string{".git", ".obsidian", ".trash"},
}, "koanf"), nil)

// Load env variables
Expand Down
4 changes: 4 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestLoadConfig_DefaultValues(t *testing.T) {
Port: 6969,
LogLevel: slog.LevelInfo,
ZettelkastenDirectory: "/any/dir",
IgnoreFiles: []string{".git", ".obsidian", ".trash"},
}
assert.Equal(t, expected, c)
}
Expand All @@ -34,6 +35,7 @@ func TestLoadConfig_PartialEnv(t *testing.T) {
Port: 4444,
LogLevel: slog.LevelDebug,
ZettelkastenDirectory: "/any/dir",
IgnoreFiles: []string{".git", ".obsidian", ".trash"},
}
assert.Equal(t, expected, c)
}
Expand All @@ -44,13 +46,15 @@ func TestLoadConfig_FullEnv(t *testing.T) {
t.Setenv("PORT", "4444")
t.Setenv("LOG_LEVEL", "DEBUG")
t.Setenv("ZETTELKASTEN_DIRECTORY", "/any/dir")
t.Setenv("IGNORE_FILES", ".obsidian,test,/something/another,dir/file.md")
c, err := LoadConfig()
if assert.NoError(t, err) {
expected := Config{
IP: "127.0.0.1",
Port: 4444,
LogLevel: slog.LevelDebug,
ZettelkastenDirectory: "/any/dir",
IgnoreFiles: []string{".obsidian", "test", "/something/another", "dir/file.md"},
}
assert.Equal(t, expected, c)
}
Expand Down

0 comments on commit b34935f

Please sign in to comment.