Skip to content

Commit fc33713

Browse files
Rodrigo NavarroRodrigo Navarro
Rodrigo Navarro
authored and
Rodrigo Navarro
committed
feat(prompt): add ENV key to aad environment variables to the flow and regex capacity hover default values
1 parent ded73b0 commit fc33713

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

cmf/cmf.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ func NewCMF(repository Repository, templateManager TemplateManager, fsManager FS
7474
}
7575
}
7676

77+
func (cmfInstance *cmf) getInnerVariables() map[string]string {
78+
extra := map[string]string{
79+
"BRANCH_NAME": cmfInstance.repository.BranchName(),
80+
}
81+
82+
return extra
83+
}
84+
7785
// GetVersion return current cmf version
7886
func (cmfInstance *cmf) GetVersion() {
7987
fmt.Println("Git - Commit Message Formatter v", version)
@@ -88,10 +96,7 @@ func (cmfInstance *cmf) CommitChanges() {
8896
cmfFile, _ = cmfInstance.fs.GetFileFromVirtualFS(defaultYamlFile)
8997
}
9098

91-
extra := map[string]string{
92-
"BRANCH_NAME": cmfInstance.repository.BranchName(),
93-
}
94-
message, _ := cmfInstance.templateManager.Run(cmfFile, extra)
99+
message, _ := cmfInstance.templateManager.Run(cmfFile, cmfInstance.getInnerVariables())
95100
cmfInstance.repository.Commit(message)
96101
}
97102

@@ -104,10 +109,7 @@ func (cmfInstance *cmf) CommitAmend() {
104109
cmfFile, _ = cmfInstance.fs.GetFileFromVirtualFS(defaultYamlFile)
105110
}
106111

107-
extra := map[string]string{
108-
"BRANCH_NAME": cmfInstance.repository.BranchName(),
109-
}
110-
message, _ := cmfInstance.templateManager.Run(cmfFile, extra)
112+
message, _ := cmfInstance.templateManager.Run(cmfFile, cmfInstance.getInnerVariables())
111113
cmfInstance.repository.Amend(message)
112114
}
113115

templaterunner/templaterunner.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package templaterunner
22

33
import (
44
"errors"
5+
"os"
6+
"regexp"
57
"strings"
68

79
"gopkg.in/yaml.v2"
@@ -24,6 +26,7 @@ type TemplateRunner interface {
2426

2527
// Template main template struct
2628
type Template struct {
29+
Env []string `yaml:"ENV"`
2730
Prompt []PromptItem `yaml:"PROMPT"`
2831
Template string `yaml:"TEMPLATE"`
2932
}
@@ -34,6 +37,7 @@ type PromptItem struct {
3437
Label string `yaml:"LABEL"`
3538
ErrorLabel string `yaml:"ERROR_LABEL"`
3639
DefaultValue string `yaml:"DEFAULT_VALUE"`
40+
Regex string `yaml:"REGEX"`
3741
Options []Options `yaml:"OPTIONS"`
3842
}
3943

@@ -67,17 +71,24 @@ func (tr *templateRunner) parseYaml(yamlData string) (Template, error) {
6771
}
6872

6973
// Run return the result of run the template
70-
func (tr *templateRunner) Run(yamlData string, injectedVariables map[string]string) (string, error) {
74+
func (tr *templateRunner) Run(yamlData string, defaultVariables map[string]string) (string, error) {
7175
template, err := tr.parseYaml(yamlData)
7276
if err != nil {
7377
return "", err
7478
}
7579

76-
variables := tr.prompt(template)
77-
for k, v := range injectedVariables {
80+
variables := []keyValue{}
81+
for k, v := range defaultVariables {
7882
variables = append(variables, keyValue{Key: k, Value: v})
7983
}
8084

85+
for _, environmentVariable := range template.Env {
86+
variables = append(variables, keyValue{Key: environmentVariable, Value: os.Getenv(environmentVariable)})
87+
}
88+
89+
promptVariables := tr.prompt(template, variables)
90+
variables = append(variables, promptVariables...)
91+
8192
message := tr.parseTemplate(template.Template, variables)
8293

8394
return message, err
@@ -91,10 +102,11 @@ func (tr *templateRunner) parseTemplate(template string, variables []keyValue) s
91102
return template
92103
}
93104

94-
func (tr *templateRunner) prompt(template Template) []keyValue {
105+
func (tr *templateRunner) prompt(template Template, defaultVariables []keyValue) []keyValue {
95106
variables := []keyValue{}
96107
for _, step := range template.Prompt {
97108
result := ""
109+
defaultValue := ""
98110
var errorMessage = "empty value"
99111

100112
if step.ErrorLabel != "" {
@@ -105,12 +117,18 @@ func (tr *templateRunner) prompt(template Template) []keyValue {
105117
var labelMessage = step.Label
106118

107119
if step.DefaultValue != "" {
108-
labelMessage += " (" + step.DefaultValue + ")"
120+
defaultValue = tr.parseTemplate(step.DefaultValue, defaultVariables)
121+
if step.Regex != "" {
122+
r, _ := regexp.Compile(step.Regex)
123+
defaultValue = r.FindStringSubmatch(defaultValue)[0]
124+
}
125+
126+
labelMessage += " (" + defaultValue + ")"
109127
}
110128

111129
labelMessage += ":"
112130

113-
result = tr.promptManager.ReadValue(labelMessage, errorMessage, step.DefaultValue)
131+
result = tr.promptManager.ReadValue(labelMessage, errorMessage, defaultValue)
114132
} else {
115133
result = tr.promptManager.ReadValueFromList(step.Label, step.Options)
116134
}

0 commit comments

Comments
 (0)