@@ -17,28 +17,42 @@ import (
1717const (
1818 commitMsgFilePath = ".git/COMMIT_EDITMSG"
1919 formatDoc = "<type>(<scope>): <subject>"
20- errorTitle = "\033 [0;31m============================ Invalid Commit Message ================================\033 [0m"
21- errorTemplate = "\n %s\n commit message: \033 [0;31m%s\033 [0m\n correct format: \033 [0;92m%s\033 [0m\n \n %s\n %s\n \n "
22- footer = "\033 [0;31m====================================================================================\033 [0m"
20+ scopeDoc = "The <scope> can be empty (e.g. if the change is a global or difficult to assign to a single component), in which case the parentheses are omitted."
21+ styleDoc = "The type and scope should always be lowercase."
2322)
2423
24+ func textRed (s string ) string {
25+ return fmt .Sprintf ("\033 [0;31m%s\033 [0m" , s )
26+ }
27+
28+ func textBrightGreen (s string ) string {
29+ return fmt .Sprintf ("\033 [0;92m%s\033 [0m" , s )
30+ }
31+
32+ func textBrightYellow (s string ) string {
33+ return fmt .Sprintf ("\033 [0;93m%s\033 [0m" , s )
34+ }
35+
2536var (
2637 r = flag .String ("rule" , "" , "select rule file path (config.yaml)" )
2738
2839 FormatRegularPattern = `([a-zA-Z]+)(\(.*\))?:\s+(.*)`
2940
30- scopeDoc = "\033 [0;93mThe <scope> can be empty (e.g. if the change is a global or difficult to assign to a single component), in which case the parentheses are omitted.\033 [0m"
31- styleDoc = "\033 [0;93mThe type and scope should always be lowercase.\033 [0m"
41+ errorTitle = "============================ Invalid Message ================================"
42+ errorTemplate = "\n %s\n title message: %s\n correct format: %s\n \n %s\n \n See: %s\n "
43+ footer = "============================================================================="
3244
3345 ErrStyle = errors .New ("invalid style error" )
3446 ErrType = errors .New ("invalid type error" )
3547 ErrFormat = errors .New ("invalid format error" )
3648 ErrScope = errors .New ("invalid scope error" )
3749
38- DefaultRules = Config {
50+ DefaultConfig = Config {
3951 SkipPrefixes : []string {
4052 "Merge branch " ,
53+ "BREAKING: " ,
4154 },
55+ Reference : "https://github.com/masahiro331/go-commitlinter#description" ,
4256 TypeRules : TypeRules {
4357 {
4458 Type : "feat" ,
7791 Description : "for updates that do not apply to the above, such as dependency updates." ,
7892 },
7993 },
94+ StyleDoc : styleDoc ,
95+ ScopeDoc : scopeDoc ,
8096 }
8197)
8298
@@ -88,9 +104,9 @@ type TypeRule struct {
88104type TypeRules []TypeRule
89105
90106func (typeRules TypeRules ) String () string {
91- ret := "Allows type values\n "
107+ ret := "Allowed < type> values\n "
92108 for _ , tr := range typeRules {
93- ret += fmt .Sprintf ("\033 [0;93m %s\033 [0m \ t %s\n " , tr .Type , tr .Description )
109+ ret += fmt .Sprintf ("%s\t %s\n " , textBrightYellow ( tr .Type ) , tr .Description )
94110 }
95111
96112 return ret
@@ -99,6 +115,9 @@ func (typeRules TypeRules) String() string {
99115type Config struct {
100116 SkipPrefixes []string `yaml:"skip_prefixes"`
101117 TypeRules TypeRules `yaml:"type_rules"`
118+ Reference string `yaml:"reference"`
119+ StyleDoc string `yaml:"style_doc"`
120+ ScopeDoc string `yaml:"scope_doc"`
102121}
103122
104123type Format struct {
@@ -107,14 +126,9 @@ type Format struct {
107126 Subject string
108127}
109128
110- type Linter struct {
111- Conf Config
112- Format Format
113- }
114-
115129func NewConfig (filepath string ) (Config , error ) {
116130 if filepath == "" {
117- return DefaultRules , nil
131+ return DefaultConfig , nil
118132 }
119133
120134 f , err := os .Open (filepath )
@@ -222,6 +236,27 @@ func run() (string, Config, error) {
222236 return "" , conf , nil
223237}
224238
239+ func finally (m string , conf Config , err error ) {
240+ message := ""
241+ switch err {
242+ case ErrFormat , ErrType :
243+ message = fmt .Sprintf (errorTemplate , textRed (errorTitle ), textRed (m ), textBrightGreen (formatDoc ), conf .TypeRules , textBrightGreen (conf .Reference ))
244+ case ErrStyle :
245+ message = fmt .Sprintf (errorTemplate , textRed (errorTitle ), textRed (m ), textBrightGreen (formatDoc ), textBrightYellow (conf .StyleDoc ), textBrightGreen (conf .Reference ))
246+ case ErrScope :
247+ message = fmt .Sprintf (errorTemplate , textRed (errorTitle ), textRed (m ), textBrightGreen (formatDoc ), textBrightYellow (conf .ScopeDoc ), textBrightGreen (conf .Reference ))
248+ case nil :
249+ return
250+ default :
251+ log .Fatal (xerrors .Errorf ("unspecified error: %w" , err ))
252+ }
253+ message = fmt .Sprintf ("%s\n %s" , message , textRed (footer ))
254+ if err != nil {
255+ fmt .Println (message )
256+ os .Exit (1 )
257+ }
258+ }
259+
225260func getMessage () (string , error ) {
226261 reader := bufio .NewReader (os .Stdin )
227262 b , _ , _ := reader .ReadLine ()
@@ -243,26 +278,6 @@ func getMessage() (string, error) {
243278 return string (b ), nil
244279}
245280
246- func finally (m string , conf Config , err error ) {
247- message := ""
248- switch err {
249- case ErrFormat , ErrType :
250- message = fmt .Sprintf (errorTemplate , errorTitle , m , formatDoc , conf .TypeRules , footer )
251- case ErrStyle :
252- message = fmt .Sprintf (errorTemplate , errorTitle , m , formatDoc , styleDoc , footer )
253- case ErrScope :
254- message = fmt .Sprintf (errorTemplate , errorTitle , m , formatDoc , scopeDoc , footer )
255- case nil :
256- return
257- default :
258- log .Fatal (xerrors .Errorf ("unspecified error: %w" , err ))
259- }
260- if err != nil {
261- fmt .Println (message )
262- os .Exit (1 )
263- }
264- }
265-
266281func main () {
267282 finally (run ())
268- }
283+ }
0 commit comments