@@ -2,6 +2,8 @@ package templaterunner
2
2
3
3
import (
4
4
"errors"
5
+ "os"
6
+ "regexp"
5
7
"strings"
6
8
7
9
"gopkg.in/yaml.v2"
@@ -24,6 +26,7 @@ type TemplateRunner interface {
24
26
25
27
// Template main template struct
26
28
type Template struct {
29
+ Env []string `yaml:"ENV"`
27
30
Prompt []PromptItem `yaml:"PROMPT"`
28
31
Template string `yaml:"TEMPLATE"`
29
32
}
@@ -34,6 +37,7 @@ type PromptItem struct {
34
37
Label string `yaml:"LABEL"`
35
38
ErrorLabel string `yaml:"ERROR_LABEL"`
36
39
DefaultValue string `yaml:"DEFAULT_VALUE"`
40
+ Regex string `yaml:"REGEX"`
37
41
Options []Options `yaml:"OPTIONS"`
38
42
}
39
43
@@ -67,17 +71,24 @@ func (tr *templateRunner) parseYaml(yamlData string) (Template, error) {
67
71
}
68
72
69
73
// 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 ) {
71
75
template , err := tr .parseYaml (yamlData )
72
76
if err != nil {
73
77
return "" , err
74
78
}
75
79
76
- variables := tr . prompt ( template )
77
- for k , v := range injectedVariables {
80
+ variables := [] keyValue {}
81
+ for k , v := range defaultVariables {
78
82
variables = append (variables , keyValue {Key : k , Value : v })
79
83
}
80
84
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
+
81
92
message := tr .parseTemplate (template .Template , variables )
82
93
83
94
return message , err
@@ -91,10 +102,11 @@ func (tr *templateRunner) parseTemplate(template string, variables []keyValue) s
91
102
return template
92
103
}
93
104
94
- func (tr * templateRunner ) prompt (template Template ) []keyValue {
105
+ func (tr * templateRunner ) prompt (template Template , defaultVariables [] keyValue ) []keyValue {
95
106
variables := []keyValue {}
96
107
for _ , step := range template .Prompt {
97
108
result := ""
109
+ defaultValue := ""
98
110
var errorMessage = "empty value"
99
111
100
112
if step .ErrorLabel != "" {
@@ -105,12 +117,18 @@ func (tr *templateRunner) prompt(template Template) []keyValue {
105
117
var labelMessage = step .Label
106
118
107
119
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 + ")"
109
127
}
110
128
111
129
labelMessage += ":"
112
130
113
- result = tr .promptManager .ReadValue (labelMessage , errorMessage , step . DefaultValue )
131
+ result = tr .promptManager .ReadValue (labelMessage , errorMessage , defaultValue )
114
132
} else {
115
133
result = tr .promptManager .ReadValueFromList (step .Label , step .Options )
116
134
}
0 commit comments