Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: v2beta1
name: test

vars:
MYSQL_VERSION:
question: Which mysql version do you want to use?
default: ""
pipelines:
dev: |-
echo ${MYSQL_VERSION}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: v2beta1
name: test

vars:
MYSQL_VERSION:
question: Which mysql version do you want to use?
pipelines:
dev: |-
echo ${MYSQL_VERSION}
26 changes: 18 additions & 8 deletions pkg/devspace/config/loader/variable/undefined_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ func convertStringValue(value string) interface{} {
}

func askQuestion(variable *latest.Variable, log log.Logger) (string, error) {
params := getParams(variable)

answer, err := log.Question(params)
if err != nil {
return "", err
}

return answer, nil
}

func getParams(variable *latest.Variable) *survey.QuestionOptions {
params := &survey.QuestionOptions{}

if variable == nil {
Expand All @@ -91,9 +102,14 @@ func askQuestion(variable *latest.Variable, log log.Logger) (string, error) {
if variable.Default != "" {
params.DefaultValue = fmt.Sprintf("%v", variable.Default)
}

if variable.Default != nil {
params.DefaultValueSet = true
}
if len(variable.Options) > 0 {
params.Options = variable.Options
if variable.Default == nil {
params.DefaultValue = params.Options[0]
}
} else if variable.ValidationPattern != "" {
params.ValidationRegexPattern = variable.ValidationPattern

Expand All @@ -102,11 +118,5 @@ func askQuestion(variable *latest.Variable, log log.Logger) (string, error) {
}
}
}

answer, err := log.Question(params)
if err != nil {
return "", err
}

return answer, nil
return params
}
34 changes: 34 additions & 0 deletions pkg/devspace/config/loader/variable/undefined_variable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package variable

import (
"fmt"
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
"gopkg.in/yaml.v3"
"os"
"testing"
)

func TestGetParams(t *testing.T) {
testCases := map[string]bool{"testing/with_default_value/devspace.yaml": true, "testing/without_default_value/devspace.yaml": false}
for input, expected := range testCases {
config := getConfig(input)
variable := config.Vars["MYSQL_VERSION"]
actual := getParams(variable)
if expected != actual.DefaultValueSet {
t.Errorf("TestCase %s\nactual:%t\nexpected:%t", input, actual.DefaultValueSet, true)
}
}
}

func getConfig(filename string) *latest.Config {
v := &latest.Config{}
yamlFile, err := os.ReadFile(filename)
if err != nil {
fmt.Printf("yamlFile.Get err #%v ", err)
}
err = yaml.Unmarshal(yamlFile, v)
if err != nil {
fmt.Printf("Unmarshal: %v", err)
}
return v
}
4 changes: 2 additions & 2 deletions pkg/util/log/stream_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,9 @@ func (s *StreamLogger) Question(params *survey.QuestionOptions) (string, error)
s.m.Lock()
defer s.m.Unlock()

if !s.isTerminal && (params.DefaultValue == "" || params.DefaultValue == "<nil>") {
if !s.isTerminal && !params.DefaultValueSet {
return "", fmt.Errorf("cannot ask question '%s' because currently you're not using devspace in a terminal and default value is also not provided", params.Question)
} else if !s.isTerminal && params.DefaultValue != "" {
} else if !s.isTerminal && params.DefaultValueSet {
return params.DefaultValue, nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/util/survey/survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type QuestionOptions struct {
Question string
DefaultValue string
DefaultValueSet bool
ValidationRegexPattern string
ValidationMessage string
ValidationFunc func(value string) error
Expand Down