github.com/brandonmanuel/git-chglog@v0.0.0-20200903004639-7a62fa08787a/cmd/git-chglog/variables.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 // Previewable ... 9 type Previewable interface { 10 Display() string 11 Preview() string 12 } 13 14 // Defaults 15 var ( 16 defaultConfigDir = ".chglog" 17 defaultConfigFilename = "config.yml" 18 defaultTemplateFilename = "CHANGELOG.tpl.md" 19 ) 20 21 // Styles 22 var ( 23 styleGitHub = "github" 24 styleGitLab = "gitlab" 25 styleBitbucket = "bitbucket" 26 styleNone = "none" 27 styles = []string{ 28 styleGitHub, 29 styleGitLab, 30 styleBitbucket, 31 styleNone, 32 } 33 ) 34 35 type typeSample struct { 36 typeName string 37 title string 38 } 39 40 // CommitMessageFormat ... 41 type CommitMessageFormat struct { 42 display string 43 preview string 44 pattern string 45 patternMaps []string 46 typeSamples []typeSample 47 } 48 49 // Display ... 50 func (f *CommitMessageFormat) Display() string { 51 return f.display 52 } 53 54 // Preview ... 55 func (f *CommitMessageFormat) Preview() string { 56 return f.preview 57 } 58 59 // PatternMapString ... 60 func (f *CommitMessageFormat) PatternMapString() string { 61 if len(f.patternMaps) == 0 { 62 return " []" 63 } 64 65 arr := make([]string, len(f.patternMaps)) 66 for i, p := range f.patternMaps { 67 arr[i] = fmt.Sprintf( 68 "%s- %s", 69 strings.Repeat(" ", 6), 70 p, 71 ) 72 } 73 74 return fmt.Sprintf("\n%s", strings.Join(arr, "\n")) 75 } 76 77 // FilterTypeString ... 78 func (f *CommitMessageFormat) FilterTypesString() string { 79 if len(f.typeSamples) == 0 { 80 return " []" 81 } 82 83 arr := make([]string, len(f.typeSamples)) 84 for i, t := range f.typeSamples { 85 arr[i] = fmt.Sprintf( 86 "%s#%s- %s", 87 strings.Repeat(" ", 4), strings.Repeat(" ", 5), 88 t.typeName) 89 } 90 return fmt.Sprintf("\n%s", strings.Join(arr, "\n")) 91 } 92 93 // TitleMapsString ... 94 func (f *CommitMessageFormat) TitleMapsString() string { 95 if len(f.typeSamples) == 0 { 96 return " []" 97 } 98 99 arr := make([]string, len(f.typeSamples)) 100 for i, t := range f.typeSamples { 101 arr[i] = fmt.Sprintf( 102 "%s#%s%s: %s", 103 strings.Repeat(" ", 4), strings.Repeat(" ", 3), 104 t.typeName, t.title) 105 } 106 return fmt.Sprintf("\n%s", strings.Join(arr, "\n")) 107 } 108 109 // Formats 110 var ( 111 fmtTypeScopeSubject = &CommitMessageFormat{ 112 display: "<type>(<scope>): <subject>", 113 preview: "feat(core): Add new feature", 114 pattern: `^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$`, 115 patternMaps: []string{"Type", "Scope", "Subject"}, 116 typeSamples: []typeSample{ 117 {"feat", "Features"}, {"fix", "Bug Fixes"}, 118 {"perf", "Performance Improvements"}, {"refactor", "Code Refactoring"}}, 119 } 120 fmtTypeSubject = &CommitMessageFormat{ 121 display: "<type>: <subject>", 122 preview: "feat: Add new feature", 123 pattern: `^(\\w*)\\:\\s(.*)$`, 124 patternMaps: []string{"Type", "Subject"}, 125 typeSamples: []typeSample{ 126 {"feat", "Features"}, {"fix", "Bug Fixes"}, 127 {"perf", "Performance Improvements"}, {"refactor", "Code Refactoring"}}, 128 } 129 fmtGitBasic = &CommitMessageFormat{ 130 display: "<<type> subject>", 131 preview: "Add new feature", 132 pattern: `^((\\w+)\\s.*)$`, 133 patternMaps: []string{"Subject", "Type"}, 134 typeSamples: []typeSample{ 135 {"feat", "Features"}, {"fix", "Bug Fixes"}, 136 {"perf", "Performance Improvements"}, {"refactor", "Code Refactoring"}}, 137 } 138 fmtSubject = &CommitMessageFormat{ 139 display: "<subject>", 140 preview: "Add new feature (Not detect `type` field)", 141 pattern: `^(.*)$`, 142 patternMaps: []string{"Subject"}, 143 typeSamples: []typeSample{}, 144 } 145 fmtCommitEmoji = &CommitMessageFormat{ 146 display: ":<type>: <subject>", 147 preview: ":sparkles: Add new feature (Commit message with emoji format)", 148 pattern: `^:(\\w*)\\:\\s(.*)$`, 149 patternMaps: []string{"Type", "Subject"}, 150 typeSamples: []typeSample{ 151 {"sparkles", "Features"}, {"bug", "Bug Fixes"}, 152 {"zap", "Performance Improvements"}, {"recycle", "Code Refactoring"}}, 153 } 154 formats = []Previewable{ 155 fmtTypeScopeSubject, 156 fmtTypeSubject, 157 fmtGitBasic, 158 fmtSubject, 159 fmtCommitEmoji, 160 } 161 ) 162 163 // TemplateStyleFormat ... 164 type TemplateStyleFormat struct { 165 preview string 166 display string 167 } 168 169 // Display ... 170 func (t *TemplateStyleFormat) Display() string { 171 return t.display 172 } 173 174 // Preview ... 175 func (t *TemplateStyleFormat) Preview() string { 176 return t.preview 177 } 178 179 // Templates 180 var ( 181 tplKeepAChangelog = &TemplateStyleFormat{ 182 display: "keep-a-changelog", 183 preview: "https://github.com/BrandonManuel/example-type-scope-subject/blob/master/CHANGELOG.kac.md", 184 } 185 tplStandard = &TemplateStyleFormat{ 186 display: "standard", 187 preview: "https://github.com/BrandonManuel/example-type-scope-subject/blob/master/CHANGELOG.standard.md", 188 } 189 tplCool = &TemplateStyleFormat{ 190 display: "cool", 191 preview: "https://github.com/BrandonManuel/example-type-scope-subject/blob/master/CHANGELOG.cool.md", 192 } 193 templates = []Previewable{ 194 tplKeepAChangelog, 195 tplStandard, 196 tplCool, 197 } 198 )