Skip to content

Commit a7be005

Browse files
authored
Merge pull request #2317 from mahendrabagul/consider-default-value-even-if-its-empty
Consider default value even if it was set to empty in non-terminal devspace execution
2 parents cb4990a + 5b7492b commit a7be005

6 files changed

Lines changed: 74 additions & 10 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: v2beta1
2+
name: test
3+
4+
vars:
5+
MYSQL_VERSION:
6+
question: Which mysql version do you want to use?
7+
default: ""
8+
pipelines:
9+
dev: |-
10+
echo ${MYSQL_VERSION}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: v2beta1
2+
name: test
3+
4+
vars:
5+
MYSQL_VERSION:
6+
question: Which mysql version do you want to use?
7+
pipelines:
8+
dev: |-
9+
echo ${MYSQL_VERSION}

pkg/devspace/config/loader/variable/undefined_variable.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ func convertStringValue(value string) interface{} {
6969
}
7070

7171
func askQuestion(variable *latest.Variable, log log.Logger) (string, error) {
72+
params := getParams(variable)
73+
74+
answer, err := log.Question(params)
75+
if err != nil {
76+
return "", err
77+
}
78+
79+
return answer, nil
80+
}
81+
82+
func getParams(variable *latest.Variable) *survey.QuestionOptions {
7283
params := &survey.QuestionOptions{}
7384

7485
if variable == nil {
@@ -91,9 +102,14 @@ func askQuestion(variable *latest.Variable, log log.Logger) (string, error) {
91102
if variable.Default != "" {
92103
params.DefaultValue = fmt.Sprintf("%v", variable.Default)
93104
}
94-
105+
if variable.Default != nil {
106+
params.DefaultValueSet = true
107+
}
95108
if len(variable.Options) > 0 {
96109
params.Options = variable.Options
110+
if variable.Default == nil {
111+
params.DefaultValue = params.Options[0]
112+
}
97113
} else if variable.ValidationPattern != "" {
98114
params.ValidationRegexPattern = variable.ValidationPattern
99115

@@ -102,11 +118,5 @@ func askQuestion(variable *latest.Variable, log log.Logger) (string, error) {
102118
}
103119
}
104120
}
105-
106-
answer, err := log.Question(params)
107-
if err != nil {
108-
return "", err
109-
}
110-
111-
return answer, nil
121+
return params
112122
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package variable
2+
3+
import (
4+
"fmt"
5+
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
6+
"gopkg.in/yaml.v3"
7+
"os"
8+
"testing"
9+
)
10+
11+
func TestGetParams(t *testing.T) {
12+
testCases := map[string]bool{"testing/with_default_value/devspace.yaml": true, "testing/without_default_value/devspace.yaml": false}
13+
for input, expected := range testCases {
14+
config := getConfig(input)
15+
variable := config.Vars["MYSQL_VERSION"]
16+
actual := getParams(variable)
17+
if expected != actual.DefaultValueSet {
18+
t.Errorf("TestCase %s\nactual:%t\nexpected:%t", input, actual.DefaultValueSet, true)
19+
}
20+
}
21+
}
22+
23+
func getConfig(filename string) *latest.Config {
24+
v := &latest.Config{}
25+
yamlFile, err := os.ReadFile(filename)
26+
if err != nil {
27+
fmt.Printf("yamlFile.Get err #%v ", err)
28+
}
29+
err = yaml.Unmarshal(yamlFile, v)
30+
if err != nil {
31+
fmt.Printf("Unmarshal: %v", err)
32+
}
33+
return v
34+
}

pkg/util/log/stream_logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,9 @@ func (s *StreamLogger) Question(params *survey.QuestionOptions) (string, error)
516516
s.m.Lock()
517517
defer s.m.Unlock()
518518

519-
if !s.isTerminal && (params.DefaultValue == "" || params.DefaultValue == "<nil>") {
519+
if !s.isTerminal && !params.DefaultValueSet {
520520
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)
521-
} else if !s.isTerminal && params.DefaultValue != "" {
521+
} else if !s.isTerminal && params.DefaultValueSet {
522522
return params.DefaultValue, nil
523523
}
524524

pkg/util/survey/survey.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
type QuestionOptions struct {
1414
Question string
1515
DefaultValue string
16+
DefaultValueSet bool
1617
ValidationRegexPattern string
1718
ValidationMessage string
1819
ValidationFunc func(value string) error

0 commit comments

Comments
 (0)