mirror of
https://github.com/danielmiessler/Fabric.git
synced 2026-02-14 07:55:03 -05:00
## CHANGES - Add custom patterns directory support via environment variable - Implement custom patterns plugin with registry integration - Override main patterns with custom directory patterns - Expand home directory paths in custom patterns config - Add comprehensive test coverage for custom patterns functionality - Integrate custom patterns into plugin setup workflow - Support pattern precedence with custom over main patterns
62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package custom_patterns
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/danielmiessler/fabric/plugins"
|
|
)
|
|
|
|
func NewCustomPatterns() (ret *CustomPatterns) {
|
|
label := "Custom Patterns"
|
|
ret = &CustomPatterns{}
|
|
|
|
ret.PluginBase = &plugins.PluginBase{
|
|
Name: label,
|
|
SetupDescription: "Custom Patterns - Set directory for your custom patterns (optional)",
|
|
EnvNamePrefix: plugins.BuildEnvVariablePrefix(label),
|
|
ConfigureCustom: ret.configure,
|
|
}
|
|
|
|
ret.CustomPatternsDir = ret.AddSetupQuestionCustom("Directory", false,
|
|
"Enter the path to your custom patterns directory (leave empty to skip)")
|
|
|
|
return
|
|
}
|
|
|
|
type CustomPatterns struct {
|
|
*plugins.PluginBase
|
|
CustomPatternsDir *plugins.SetupQuestion
|
|
}
|
|
|
|
func (o *CustomPatterns) configure() error {
|
|
if o.CustomPatternsDir.Value != "" {
|
|
// Expand home directory if needed
|
|
if strings.HasPrefix(o.CustomPatternsDir.Value, "~/") {
|
|
if homeDir, err := os.UserHomeDir(); err == nil {
|
|
o.CustomPatternsDir.Value = filepath.Join(homeDir, o.CustomPatternsDir.Value[2:])
|
|
}
|
|
}
|
|
|
|
// Convert to absolute path
|
|
if absPath, err := filepath.Abs(o.CustomPatternsDir.Value); err == nil {
|
|
o.CustomPatternsDir.Value = absPath
|
|
}
|
|
|
|
// Create the directory if it doesn't exist
|
|
if err := os.MkdirAll(o.CustomPatternsDir.Value, 0755); err != nil {
|
|
// If we can't create it, clear the value to avoid errors
|
|
o.CustomPatternsDir.Value = ""
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// IsConfigured returns true if a custom patterns directory has been set
|
|
func (o *CustomPatterns) IsConfigured() bool {
|
|
// Check if the plugin has been configured with a directory
|
|
return o.CustomPatternsDir.Value != ""
|
|
}
|