-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add registry for custom rule and formatters
- Loading branch information
1 parent
63f2581
commit d5963f6
Showing
2 changed files
with
120 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/conventionalcommit/commitlint/lint" | ||
) | ||
|
||
var globalRegistry = newRegistry() | ||
|
||
type registry struct { | ||
mut *sync.Mutex | ||
|
||
allRules map[string]lint.Rule | ||
allFormatters map[string]lint.Formatter | ||
} | ||
|
||
func newRegistry() *registry { | ||
reg := ®istry{ | ||
mut: &sync.Mutex{}, | ||
|
||
allRules: make(map[string]lint.Rule), | ||
allFormatters: make(map[string]lint.Formatter), | ||
} | ||
|
||
// Register Default Rules | ||
for _, rule := range defaultRules { | ||
err := reg.RegisterRule(rule) | ||
if err != nil { | ||
// default rules should not throw error | ||
panic(err) | ||
} | ||
} | ||
|
||
// Register Default Formatters | ||
for _, format := range defaultFormatters { | ||
err := reg.RegisterFormatter(format) | ||
if err != nil { | ||
// default formatters should not throw error | ||
panic(err) | ||
} | ||
} | ||
|
||
return reg | ||
} | ||
|
||
func (r *registry) RegisterRule(rule lint.Rule) error { | ||
r.mut.Lock() | ||
defer r.mut.Unlock() | ||
|
||
_, ok := r.allRules[rule.Name()] | ||
if ok { | ||
return fmt.Errorf("'%s' rule already registered", rule.Name()) | ||
} | ||
|
||
r.allRules[rule.Name()] = rule | ||
|
||
return nil | ||
} | ||
|
||
func (r *registry) RegisterFormatter(format lint.Formatter) error { | ||
r.mut.Lock() | ||
defer r.mut.Unlock() | ||
|
||
_, ok := r.allFormatters[format.Name()] | ||
if ok { | ||
return fmt.Errorf("'%s' formatter already registered", format.Name()) | ||
} | ||
|
||
r.allFormatters[format.Name()] = format | ||
|
||
return nil | ||
} | ||
|
||
func (r *registry) GetRule(name string) (lint.Rule, bool) { | ||
r.mut.Lock() | ||
defer r.mut.Unlock() | ||
|
||
rule, ok := r.allRules[name] | ||
return rule, ok | ||
} | ||
|
||
func (r *registry) GetFormatter(name string) (lint.Formatter, bool) { | ||
r.mut.Lock() | ||
defer r.mut.Unlock() | ||
|
||
format, ok := r.allFormatters[name] | ||
return format, ok | ||
} |