Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 813eb6b

Browse files
deansheatherkylecarbs
authored andcommitted
feat: use and display default template values when creating wkspc. (#1584)
1 parent 9164618 commit 813eb6b

File tree

4 files changed

+64
-15
lines changed

4 files changed

+64
-15
lines changed

cli/cliui/parameter.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,32 @@ func ParameterSchema(cmd *cobra.Command, parameterSchema codersdk.TemplateVersio
3030
_, _ = fmt.Fprint(cmd.OutOrStdout(), "\033[1A")
3131
value, err = Select(cmd, SelectOptions{
3232
Options: options,
33+
Default: parameterSchema.DefaultSourceValue,
3334
HideSearch: true,
3435
})
3536
if err == nil {
3637
_, _ = fmt.Fprintln(cmd.OutOrStdout())
3738
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+Styles.Prompt.String()+Styles.Field.Render(value))
3839
}
3940
} else {
41+
text := "Enter a value"
42+
if parameterSchema.DefaultSourceValue != "" {
43+
text += fmt.Sprintf(" (default: %q)", parameterSchema.DefaultSourceValue)
44+
}
45+
text += ":"
46+
4047
value, err = Prompt(cmd, PromptOptions{
41-
Text: Styles.Bold.Render("Enter a value:"),
48+
Text: Styles.Bold.Render(text),
4249
})
4350
}
44-
return value, err
51+
if err != nil {
52+
return "", err
53+
}
54+
55+
// If they didn't specify anything, use the default value if set.
56+
if len(options) == 0 && value == "" {
57+
value = parameterSchema.DefaultSourceValue
58+
}
59+
60+
return value, nil
4561
}

cli/cliui/select.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ func init() {
3535
}
3636

3737
type SelectOptions struct {
38-
Options []string
38+
Options []string
39+
// Default will be highlighted first if it's a valid option.
40+
Default string
3941
Size int
4042
HideSearch bool
4143
}
@@ -50,9 +52,16 @@ func Select(cmd *cobra.Command, opts SelectOptions) (string, error) {
5052
if flag.Lookup("test.v") != nil {
5153
return opts.Options[0], nil
5254
}
55+
56+
var defaultOption interface{}
57+
if opts.Default != "" {
58+
defaultOption = opts.Default
59+
}
60+
5361
var value string
5462
err := survey.AskOne(&survey.Select{
5563
Options: opts.Options,
64+
Default: defaultOption,
5665
PageSize: opts.Size,
5766
}, &value, survey.WithIcons(func(is *survey.IconSet) {
5867
is.Help.Text = "Type to search"

cli/create_test.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli_test
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/require"
@@ -44,6 +45,7 @@ func TestCreate(t *testing.T) {
4445
}
4546
<-doneChan
4647
})
48+
4749
t.Run("CreateFromList", func(t *testing.T) {
4850
t.Parallel()
4951
client := coderdtest.New(t, nil)
@@ -74,6 +76,7 @@ func TestCreate(t *testing.T) {
7476
}
7577
<-doneChan
7678
})
79+
7780
t.Run("FromNothing", func(t *testing.T) {
7881
t.Parallel()
7982
client := coderdtest.New(t, nil)
@@ -105,33 +108,52 @@ func TestCreate(t *testing.T) {
105108
}
106109
<-doneChan
107110
})
111+
108112
t.Run("WithParameter", func(t *testing.T) {
109113
t.Parallel()
110114
client := coderdtest.New(t, nil)
111115
user := coderdtest.CreateFirstUser(t, client)
112116
coderdtest.NewProvisionerDaemon(t, client)
117+
118+
defaultValue := "something"
113119
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
114120
Parse: []*proto.Parse_Response{{
115121
Type: &proto.Parse_Response_Complete{
116122
Complete: &proto.Parse_Complete{
117-
ParameterSchemas: []*proto.ParameterSchema{{
118-
AllowOverrideSource: true,
119-
Name: "region",
120-
Description: "description",
121-
DefaultSource: &proto.ParameterSource{
122-
Scheme: proto.ParameterSource_DATA,
123-
Value: "something",
123+
ParameterSchemas: []*proto.ParameterSchema{
124+
{
125+
AllowOverrideSource: true,
126+
Name: "region",
127+
Description: "description 1",
128+
DefaultSource: &proto.ParameterSource{
129+
Scheme: proto.ParameterSource_DATA,
130+
Value: defaultValue,
131+
},
132+
DefaultDestination: &proto.ParameterDestination{
133+
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
134+
},
124135
},
125-
DefaultDestination: &proto.ParameterDestination{
126-
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
136+
{
137+
AllowOverrideSource: true,
138+
Name: "username",
139+
Description: "description 2",
140+
DefaultSource: &proto.ParameterSource{
141+
Scheme: proto.ParameterSource_DATA,
142+
// No default value
143+
Value: "",
144+
},
145+
DefaultDestination: &proto.ParameterDestination{
146+
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
147+
},
127148
},
128-
}},
149+
},
129150
},
130151
},
131152
}},
132153
Provision: echo.ProvisionComplete,
133154
ProvisionDryRun: echo.ProvisionComplete,
134155
})
156+
135157
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
136158
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
137159
cmd, root := clitest.New(t, "create", "")
@@ -145,9 +167,11 @@ func TestCreate(t *testing.T) {
145167
err := cmd.Execute()
146168
require.NoError(t, err)
147169
}()
170+
148171
matches := []string{
149172
"Specify a name", "my-workspace",
150-
"Enter a value", "bananas",
173+
fmt.Sprintf("Enter a value (default: %q):", defaultValue), "bingo",
174+
"Enter a value:", "boingo",
151175
"Confirm create?", "yes",
152176
}
153177
for i := 0; i < len(matches); i += 2 {

cli/login.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func login() *cobra.Command {
164164
cliui.Styles.Paragraph.Render(fmt.Sprintf("Welcome to Coder, %s! You're authenticated.", cliui.Styles.Keyword.Render(username)))+"\n")
165165

166166
_, _ = fmt.Fprintf(cmd.OutOrStdout(),
167-
cliui.Styles.Paragraph.Render("Get started by creating a template: "+cliui.Styles.Code.Render("coder templates create"))+"\n")
167+
cliui.Styles.Paragraph.Render("Get started by creating a template: "+cliui.Styles.Code.Render("coder templates init"))+"\n")
168168
return nil
169169
}
170170

0 commit comments

Comments
 (0)