mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: use and display default template values when creating wkspc. (#1584)
This commit is contained in:
+18
-2
@@ -30,6 +30,7 @@ func ParameterSchema(cmd *cobra.Command, parameterSchema codersdk.TemplateVersio
|
|||||||
_, _ = fmt.Fprint(cmd.OutOrStdout(), "\033[1A")
|
_, _ = fmt.Fprint(cmd.OutOrStdout(), "\033[1A")
|
||||||
value, err = Select(cmd, SelectOptions{
|
value, err = Select(cmd, SelectOptions{
|
||||||
Options: options,
|
Options: options,
|
||||||
|
Default: parameterSchema.DefaultSourceValue,
|
||||||
HideSearch: true,
|
HideSearch: true,
|
||||||
})
|
})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -37,9 +38,24 @@ func ParameterSchema(cmd *cobra.Command, parameterSchema codersdk.TemplateVersio
|
|||||||
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+Styles.Prompt.String()+Styles.Field.Render(value))
|
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+Styles.Prompt.String()+Styles.Field.Render(value))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
text := "Enter a value"
|
||||||
|
if parameterSchema.DefaultSourceValue != "" {
|
||||||
|
text += fmt.Sprintf(" (default: %q)", parameterSchema.DefaultSourceValue)
|
||||||
|
}
|
||||||
|
text += ":"
|
||||||
|
|
||||||
value, err = Prompt(cmd, PromptOptions{
|
value, err = Prompt(cmd, PromptOptions{
|
||||||
Text: Styles.Bold.Render("Enter a value:"),
|
Text: Styles.Bold.Render(text),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return value, err
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// If they didn't specify anything, use the default value if set.
|
||||||
|
if len(options) == 0 && value == "" {
|
||||||
|
value = parameterSchema.DefaultSourceValue
|
||||||
|
}
|
||||||
|
|
||||||
|
return value, nil
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-1
@@ -35,7 +35,9 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SelectOptions struct {
|
type SelectOptions struct {
|
||||||
Options []string
|
Options []string
|
||||||
|
// Default will be highlighted first if it's a valid option.
|
||||||
|
Default string
|
||||||
Size int
|
Size int
|
||||||
HideSearch bool
|
HideSearch bool
|
||||||
}
|
}
|
||||||
@@ -50,9 +52,16 @@ func Select(cmd *cobra.Command, opts SelectOptions) (string, error) {
|
|||||||
if flag.Lookup("test.v") != nil {
|
if flag.Lookup("test.v") != nil {
|
||||||
return opts.Options[0], nil
|
return opts.Options[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var defaultOption interface{}
|
||||||
|
if opts.Default != "" {
|
||||||
|
defaultOption = opts.Default
|
||||||
|
}
|
||||||
|
|
||||||
var value string
|
var value string
|
||||||
err := survey.AskOne(&survey.Select{
|
err := survey.AskOne(&survey.Select{
|
||||||
Options: opts.Options,
|
Options: opts.Options,
|
||||||
|
Default: defaultOption,
|
||||||
PageSize: opts.Size,
|
PageSize: opts.Size,
|
||||||
}, &value, survey.WithIcons(func(is *survey.IconSet) {
|
}, &value, survey.WithIcons(func(is *survey.IconSet) {
|
||||||
is.Help.Text = "Type to search"
|
is.Help.Text = "Type to search"
|
||||||
|
|||||||
+35
-11
@@ -1,6 +1,7 @@
|
|||||||
package cli_test
|
package cli_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -44,6 +45,7 @@ func TestCreate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
<-doneChan
|
<-doneChan
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("CreateFromList", func(t *testing.T) {
|
t.Run("CreateFromList", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
client := coderdtest.New(t, nil)
|
client := coderdtest.New(t, nil)
|
||||||
@@ -74,6 +76,7 @@ func TestCreate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
<-doneChan
|
<-doneChan
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("FromNothing", func(t *testing.T) {
|
t.Run("FromNothing", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
client := coderdtest.New(t, nil)
|
client := coderdtest.New(t, nil)
|
||||||
@@ -105,33 +108,52 @@ func TestCreate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
<-doneChan
|
<-doneChan
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("WithParameter", func(t *testing.T) {
|
t.Run("WithParameter", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
client := coderdtest.New(t, nil)
|
client := coderdtest.New(t, nil)
|
||||||
user := coderdtest.CreateFirstUser(t, client)
|
user := coderdtest.CreateFirstUser(t, client)
|
||||||
coderdtest.NewProvisionerDaemon(t, client)
|
coderdtest.NewProvisionerDaemon(t, client)
|
||||||
|
|
||||||
|
defaultValue := "something"
|
||||||
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
|
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
|
||||||
Parse: []*proto.Parse_Response{{
|
Parse: []*proto.Parse_Response{{
|
||||||
Type: &proto.Parse_Response_Complete{
|
Type: &proto.Parse_Response_Complete{
|
||||||
Complete: &proto.Parse_Complete{
|
Complete: &proto.Parse_Complete{
|
||||||
ParameterSchemas: []*proto.ParameterSchema{{
|
ParameterSchemas: []*proto.ParameterSchema{
|
||||||
AllowOverrideSource: true,
|
{
|
||||||
Name: "region",
|
AllowOverrideSource: true,
|
||||||
Description: "description",
|
Name: "region",
|
||||||
DefaultSource: &proto.ParameterSource{
|
Description: "description 1",
|
||||||
Scheme: proto.ParameterSource_DATA,
|
DefaultSource: &proto.ParameterSource{
|
||||||
Value: "something",
|
Scheme: proto.ParameterSource_DATA,
|
||||||
|
Value: defaultValue,
|
||||||
|
},
|
||||||
|
DefaultDestination: &proto.ParameterDestination{
|
||||||
|
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
DefaultDestination: &proto.ParameterDestination{
|
{
|
||||||
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
|
AllowOverrideSource: true,
|
||||||
|
Name: "username",
|
||||||
|
Description: "description 2",
|
||||||
|
DefaultSource: &proto.ParameterSource{
|
||||||
|
Scheme: proto.ParameterSource_DATA,
|
||||||
|
// No default value
|
||||||
|
Value: "",
|
||||||
|
},
|
||||||
|
DefaultDestination: &proto.ParameterDestination{
|
||||||
|
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}},
|
}},
|
||||||
Provision: echo.ProvisionComplete,
|
Provision: echo.ProvisionComplete,
|
||||||
ProvisionDryRun: echo.ProvisionComplete,
|
ProvisionDryRun: echo.ProvisionComplete,
|
||||||
})
|
})
|
||||||
|
|
||||||
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
|
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
|
||||||
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
|
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
|
||||||
cmd, root := clitest.New(t, "create", "")
|
cmd, root := clitest.New(t, "create", "")
|
||||||
@@ -145,9 +167,11 @@ func TestCreate(t *testing.T) {
|
|||||||
err := cmd.Execute()
|
err := cmd.Execute()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
matches := []string{
|
matches := []string{
|
||||||
"Specify a name", "my-workspace",
|
"Specify a name", "my-workspace",
|
||||||
"Enter a value", "bananas",
|
fmt.Sprintf("Enter a value (default: %q):", defaultValue), "bingo",
|
||||||
|
"Enter a value:", "boingo",
|
||||||
"Confirm create?", "yes",
|
"Confirm create?", "yes",
|
||||||
}
|
}
|
||||||
for i := 0; i < len(matches); i += 2 {
|
for i := 0; i < len(matches); i += 2 {
|
||||||
|
|||||||
+1
-1
@@ -164,7 +164,7 @@ func login() *cobra.Command {
|
|||||||
cliui.Styles.Paragraph.Render(fmt.Sprintf("Welcome to Coder, %s! You're authenticated.", cliui.Styles.Keyword.Render(username)))+"\n")
|
cliui.Styles.Paragraph.Render(fmt.Sprintf("Welcome to Coder, %s! You're authenticated.", cliui.Styles.Keyword.Render(username)))+"\n")
|
||||||
|
|
||||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(),
|
_, _ = fmt.Fprintf(cmd.OutOrStdout(),
|
||||||
cliui.Styles.Paragraph.Render("Get started by creating a template: "+cliui.Styles.Code.Render("coder templates create"))+"\n")
|
cliui.Styles.Paragraph.Render("Get started by creating a template: "+cliui.Styles.Code.Render("coder templates init"))+"\n")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user