style: apply code formatting and linting fixes

- Remove unnecessary blank lines and improve spacing
- Fix linting warnings (remove redundant nil checks)
- Standardize indentation and formatting
- No functional changes, only style improvements
This commit is contained in:
carraes
2025-07-06 12:01:32 -03:00
parent ea1a50ba33
commit fae70c6b2b
5 changed files with 32 additions and 81 deletions
+9 -30
View File
@@ -8,12 +8,10 @@ import (
"github.com/BurntSushi/toml"
)
type Config struct {
Preserve []string `toml:"preserve"`
}
func Default() *Config {
return &Config{
Preserve: []string{
@@ -32,7 +30,6 @@ func Default() *Config {
}
}
func Load() (*Config, string, error) {
if configPath, found := findProjectConfig(); found {
@@ -43,7 +40,6 @@ func Load() (*Config, string, error) {
return config, configPath, nil
}
if configPath, found := findGlobalConfig(); found {
config, err := loadFromFile(configPath)
if err != nil {
@@ -52,11 +48,9 @@ func Load() (*Config, string, error) {
return config, configPath, nil
}
return Default(), "", nil
}
func findProjectConfig() (string, bool) {
cwd, err := os.Getwd()
@@ -69,7 +63,6 @@ func findProjectConfig() (string, bool) {
return localConfig, true
}
if gitRoot := findGitRoot(cwd); gitRoot != "" {
gitConfig := filepath.Join(gitRoot, ".shush.toml")
if fileExists(gitConfig) && gitConfig != localConfig {
@@ -80,7 +73,6 @@ func findProjectConfig() (string, bool) {
return "", false
}
func findGlobalConfig() (string, bool) {
home, err := os.UserHomeDir()
if err != nil {
@@ -95,7 +87,6 @@ func findGlobalConfig() (string, bool) {
return "", false
}
func findGitRoot(startDir string) string {
dir := startDir
for {
@@ -106,63 +97,55 @@ func findGitRoot(startDir string) string {
parent := filepath.Dir(dir)
if parent == dir {
break
break
}
dir = parent
}
return ""
}
func loadFromFile(path string) (*Config, error) {
config := &Config{}
if _, err := toml.DecodeFile(path, config); err != nil {
return nil, err
}
if len(config.Preserve) == 0 {
config.Preserve = Default().Preserve
}
return config, nil
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func (c *Config) ShouldPreserveComment(comment string) bool {
comment = strings.TrimSpace(comment)
for _, pattern := range c.Preserve {
if matchesPattern(comment, pattern) {
return true
}
}
return false
}
func matchesPattern(text, pattern string) bool {
if strings.Contains(pattern, "*") {
return matchesWildcard(text, pattern)
}
return strings.Contains(text, pattern)
}
func matchesWildcard(text, pattern string) bool {
parts := strings.Split(pattern, "*")
if !strings.HasPrefix(pattern, "*") {
if !strings.HasPrefix(text, parts[0]) {
@@ -171,9 +154,8 @@ func matchesWildcard(text, pattern string) bool {
text = text[len(parts[0]):]
parts = parts[1:]
} else {
parts = parts[1:]
parts = parts[1:]
}
if !strings.HasSuffix(pattern, "*") && len(parts) > 0 {
lastPart := parts[len(parts)-1]
@@ -183,26 +165,23 @@ parts = parts[1:]
text = text[:len(text)-len(lastPart)]
parts = parts[:len(parts)-1]
}
for _, part := range parts {
if part == "" {
continue
continue
}
index := strings.Index(text, part)
if index == -1 {
return false
}
text = text[index+len(part):]
}
return true
}
func CreateExampleConfig() error {
content := `# Shush Configuration
# Patterns to preserve in comments (supports wildcards with *)
@@ -222,6 +201,6 @@ preserve = [
"*DEBUG*", # Example wildcard: preserves any comment containing DEBUG
]
`
return os.WriteFile(".shush.toml", []byte(content), 0644)
}