github.com/fredbi/git-chglog@v0.0.0-20190706071416-d35c598eac81/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  // CommitMessageFormat ...
    36  type CommitMessageFormat struct {
    37  	display     string
    38  	preview     string
    39  	pattern     string
    40  	patternMaps []string
    41  }
    42  
    43  // Display ...
    44  func (f *CommitMessageFormat) Display() string {
    45  	return f.display
    46  }
    47  
    48  // Preview ...
    49  func (f *CommitMessageFormat) Preview() string {
    50  	return f.preview
    51  }
    52  
    53  // PatternMapString ...
    54  func (f *CommitMessageFormat) PatternMapString() string {
    55  	s := " []"
    56  	l := len(f.patternMaps)
    57  	if l == 0 {
    58  		return s
    59  	}
    60  
    61  	arr := make([]string, l)
    62  	for i, p := range f.patternMaps {
    63  		arr[i] = fmt.Sprintf(
    64  			"%s- %s",
    65  			strings.Repeat(" ", 6),
    66  			p,
    67  		)
    68  	}
    69  
    70  	return fmt.Sprintf("\n%s", strings.Join(arr, "\n"))
    71  }
    72  
    73  // Formats
    74  var (
    75  	fmtTypeScopeSubject = &CommitMessageFormat{
    76  		display:     "<type>(<scope>): <subject>",
    77  		preview:     "feat(core): Add new feature",
    78  		pattern:     `^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$`,
    79  		patternMaps: []string{"Type", "Scope", "Subject"},
    80  	}
    81  	fmtTypeSubject = &CommitMessageFormat{
    82  		display:     "<type>: <subject>",
    83  		preview:     "feat: Add new feature",
    84  		pattern:     `^(\\w*)\\:\\s(.*)$`,
    85  		patternMaps: []string{"Type", "Subject"},
    86  	}
    87  	fmtGitBasic = &CommitMessageFormat{
    88  		display:     "<<type> subject>",
    89  		preview:     "Add new feature",
    90  		pattern:     `^((\\w+)\\s.*)$`,
    91  		patternMaps: []string{"Subject", "Type"},
    92  	}
    93  	fmtSubject = &CommitMessageFormat{
    94  		display:     "<subject>",
    95  		preview:     "Add new feature (Not detect `type` field)",
    96  		pattern:     `^(.*)$`,
    97  		patternMaps: []string{"Subject"},
    98  	}
    99  	formats = []Previewable{
   100  		fmtTypeScopeSubject,
   101  		fmtTypeSubject,
   102  		fmtGitBasic,
   103  		fmtSubject,
   104  	}
   105  )
   106  
   107  // TemplateStyleFormat ...
   108  type TemplateStyleFormat struct {
   109  	preview string
   110  	display string
   111  }
   112  
   113  // Display ...
   114  func (t *TemplateStyleFormat) Display() string {
   115  	return t.display
   116  }
   117  
   118  // Preview ...
   119  func (t *TemplateStyleFormat) Preview() string {
   120  	return t.preview
   121  }
   122  
   123  // Templates
   124  var (
   125  	tplKeepAChangelog = &TemplateStyleFormat{
   126  		display: "keep-a-changelog",
   127  		preview: "https://github.com/git-chglog/example-type-scope-subject/blob/master/CHANGELOG.kac.md",
   128  	}
   129  	tplStandard = &TemplateStyleFormat{
   130  		display: "standard",
   131  		preview: "https://github.com/git-chglog/example-type-scope-subject/blob/master/CHANGELOG.standard.md",
   132  	}
   133  	tplCool = &TemplateStyleFormat{
   134  		display: "cool",
   135  		preview: "https://github.com/git-chglog/example-type-scope-subject/blob/master/CHANGELOG.cool.md",
   136  	}
   137  	templates = []Previewable{
   138  		tplKeepAChangelog,
   139  		tplStandard,
   140  		tplCool,
   141  	}
   142  )