Skip to content

Commit 237219c

Browse files
authored
Merge pull request #1580 from ksylvan/0705-fix-custom-pattern-loading
Alphabetical Pattern Sorting and Configuration Refactor
2 parents 6bd926d + 26fd700 commit 237219c

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

plugins/db/fsdb/db.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,11 @@ func NewDb(dir string) (db *Db) {
1616

1717
db.EnvFilePath = db.FilePath(".env")
1818

19-
// Check for custom patterns directory from environment variable
20-
customPatternsDir := os.Getenv("CUSTOM_PATTERNS_DIRECTORY")
21-
if customPatternsDir != "" {
22-
// Expand home directory if needed
23-
if strings.HasPrefix(customPatternsDir, "~/") {
24-
if homeDir, err := os.UserHomeDir(); err == nil {
25-
customPatternsDir = filepath.Join(homeDir, customPatternsDir[2:])
26-
}
27-
}
28-
}
29-
3019
db.Patterns = &PatternsEntity{
3120
StorageEntity: &StorageEntity{Label: "Patterns", Dir: db.FilePath("patterns"), ItemIsDir: true},
3221
SystemPatternFile: "system.md",
3322
UniquePatternsFilePath: db.FilePath("unique_patterns.txt"),
34-
CustomPatternsDir: customPatternsDir,
23+
CustomPatternsDir: "", // Will be set after loading .env file
3524
}
3625

3726
db.Sessions = &SessionsEntity{
@@ -62,6 +51,18 @@ func (o *Db) Configure() (err error) {
6251
return
6352
}
6453

54+
// Set custom patterns directory after loading .env file
55+
customPatternsDir := os.Getenv("CUSTOM_PATTERNS_DIRECTORY")
56+
if customPatternsDir != "" {
57+
// Expand home directory if needed
58+
if strings.HasPrefix(customPatternsDir, "~/") {
59+
if homeDir, err := os.UserHomeDir(); err == nil {
60+
customPatternsDir = filepath.Join(homeDir, customPatternsDir[2:])
61+
}
62+
}
63+
o.Patterns.CustomPatternsDir = customPatternsDir
64+
}
65+
6566
if err = o.Patterns.Configure(); err != nil {
6667
return
6768
}

plugins/db/fsdb/patterns.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"sort"
78
"strings"
89

910
"github.com/danielmiessler/fabric/common"
@@ -198,9 +199,32 @@ func (o *PatternsEntity) GetNames() (ret []string, err error) {
198199
ret = append(ret, name)
199200
}
200201

202+
// Sort the patterns alphabetically
203+
sort.Strings(ret)
204+
201205
return ret, nil
202206
}
203207

208+
// ListNames overrides StorageEntity.ListNames to use PatternsEntity.GetNames
209+
func (o *PatternsEntity) ListNames(shellCompleteList bool) (err error) {
210+
var names []string
211+
if names, err = o.GetNames(); err != nil {
212+
return
213+
}
214+
215+
if len(names) == 0 {
216+
if !shellCompleteList {
217+
fmt.Printf("\nNo %v\n", o.StorageEntity.Label)
218+
}
219+
return
220+
}
221+
222+
for _, item := range names {
223+
fmt.Printf("%s\n", item)
224+
}
225+
return
226+
}
227+
204228
// Get required for Storage interface
205229
func (o *PatternsEntity) Get(name string) (*Pattern, error) {
206230
// Use GetPattern with no variables

0 commit comments

Comments
 (0)