Skip to content

Commit

Permalink
fix wrapped answer values
Browse files Browse the repository at this point in the history
  • Loading branch information
hay-kot committed Feb 22, 2023
1 parent 89164e5 commit 322122a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
62 changes: 51 additions & 11 deletions scaffold/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"io/fs"

"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/core"
"github.com/hay-kot/scaffold/internal/core/rwfs"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

var projectNames = [...]string{
Expand Down Expand Up @@ -65,16 +68,23 @@ func (p *Project) validate() (str string, err error) {
func (p *Project) AskQuestions(def map[string]string) (map[string]any, error) {
qs := []*survey.Question{}

if name, ok := def["Project"]; !ok {
qs = append(qs, &survey.Question{
Name: "Project",
Prompt: &survey.Input{
Message: "Project name",
},
Validate: survey.Required,
})
} else {
p.Name = name
projectMode := p.NameTemplate != "templates"

if projectMode {
name, ok := def["Project"]

switch ok {
case false:
qs = append(qs, &survey.Question{
Name: "Project",
Prompt: &survey.Input{
Message: "Project name",
},
Validate: survey.Required,
})
case true:
p.Name = name
}
}

// Read scaffold.yaml
Expand Down Expand Up @@ -118,7 +128,37 @@ func (p *Project) AskQuestions(def map[string]string) (map[string]any, error) {
return nil, err
}

p.Name = vars["Project"].(string)
if projectMode {
p.Name = vars["Project"].(string)
} else {
p.Name = "templates"
vars["Project"] = "templates"
}

// Unwrap core.OptionAnswer types into their values
for k, v := range vars {
switch vt := v.(type) {
case core.OptionAnswer:
vars[k] = vt.Value
case []core.OptionAnswer:
values := make([]string, len(vt))
for i, v := range vt {
values[i] = v.Value
}

vars[k] = values
}
}

if log.Logger.GetLevel() == zerolog.DebugLevel {
for k, v := range vars {
log.Debug().
Str("key", k).
Type("type", v).
Interface("value", v).
Msg("question")
}
}

return vars, nil
}
5 changes: 4 additions & 1 deletion scaffold/project_scaffold_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"

"github.com/AlecAivazis/survey/v2"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -89,7 +90,9 @@ func (q Question) ToSurveyQuestion() *survey.Question {
Default: q.Prompt.Default,
}
default:
panic("not implemented")
log.Fatal().
Str("question", q.Name).
Msgf("Unknown prompt type")
}

return out
Expand Down

0 comments on commit 322122a

Please sign in to comment.