Skip to content

Commit

Permalink
Merge pull request #205 from shalb/generation-improve
Browse files Browse the repository at this point in the history
generation defaults added to interactive mode
  • Loading branch information
romanprog authored Jun 14, 2023
2 parents 64c8146 + f8ed411 commit 95940ce
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
16 changes: 14 additions & 2 deletions pkg/project/templating.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,21 @@ func toYaml(in interface{}) (string, error) {
// Type for tmpl functions list with additional options, like set path to templated file.
type ExtendedFuncMap template.FuncMap

func (f *ExtendedFuncMap) Get(path string) template.FuncMap {
func (f *ExtendedFuncMap) Get(path string, s *Stack) template.FuncMap {
readFileEx := func(f string) (string, error) {
return readFile(f, path)
}
var p, pathFuncName string
if s == nil {
pathFuncName = "projectPath"
p = config.Global.ProjectConfigsPath
} else {
pathFuncName = "templatePath"
p = filepath.Join(s.TemplateDir)
}
getPath := func () string {
return p
}
var templateFunctionsMap = template.FuncMap{
"ReconcilerVersionTag": printVersion,
"reqEnv": getEnv,
Expand All @@ -57,6 +68,7 @@ func (f *ExtendedFuncMap) Get(path string) template.FuncMap {
"cidrSubnet": utils.CidrSubnet,
"toYaml": toYaml,
"readFile": readFileEx,
pathFuncName: getPath,
}
for key, val := range sprig.FuncMap() {
if _, ok := templateFunctionsMap[key]; !ok {
Expand Down Expand Up @@ -125,7 +137,7 @@ func tmplWithMissingKey(data []byte, values interface{}, missingKey string, p *P
}
// Copy common template functions.
funcs := ExtendedFuncMap{}
for k, v := range funcs.Get(filepath.Dir(fileName)) {
for k, v := range funcs.Get(filepath.Dir(fileName), s) {
tmplFuncMap[k] = v
}
for _, drv := range TemplateDriversMap {
Expand Down
10 changes: 9 additions & 1 deletion pkg/project/ui/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,15 @@ func (g *Generator) RunTemplate() (err error) {
continue
}
for {
respond := climenu.GetText(opt.Description, "")
message := opt.Description
if opt.Default != "" {
message = fmt.Sprintf("%s (%s)", opt.Description, opt.Default)
}
respond := climenu.GetText(message, "")
if respond == "" && opt.Default != "" {
respond = opt.Default
}
fmt.Println(respond)
if verifyRegex(respond, opt.Regex) {
g.dataForTmpl[opt.Name] = respond
break
Expand Down
1 change: 1 addition & 0 deletions pkg/units/shell/common/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (u *Unit) LoadState(spec interface{}, modKey string, p *project.StateProjec
if !exists {
return fmt.Errorf("load unit from state: backend '%v' does not exists in curent project", u.BackendName)
}
stack.Backend = backend
u.MyName = modName
u.StackPtr = stack
u.ProjectPtr = &p.Project
Expand Down
4 changes: 4 additions & 0 deletions pkg/units/shell/terraform/base/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (u *Unit) genDepsRemoteStates() ([]byte, error) {
// De-duplication.
DeDuplication[dep.Unit] = true
modBackend := dep.Unit.Stack().Backend
if dep.Unit.Stack() == nil {
continue
}
// log.Warnf("%v", modBackend)
rs, err := modBackend.GetRemoteStateHCL(dep.Unit.Stack().Name, dep.Unit.Name())
if err != nil {
log.Debug(err.Error())
Expand Down
1 change: 1 addition & 0 deletions tests/test-project/local-tmpl/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ units:
test_insert_yaml: {{ insertYAML .variables.list_one }}
test_map_output: {{ remoteState "this.test-complex-output-data.map[\"key\"]" }}
test_map_output_direct: {{ remoteState `this.test-complex-output-data.map.key` }}
test: {{ templatePath }}

0 comments on commit 95940ce

Please sign in to comment.