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

Skip to content

feat: use and display default template values when creating wkspc. #1584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2022
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
20 changes: 18 additions & 2 deletions cli/cliui/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,32 @@ func ParameterSchema(cmd *cobra.Command, parameterSchema codersdk.TemplateVersio
_, _ = fmt.Fprint(cmd.OutOrStdout(), "\033[1A")
value, err = Select(cmd, SelectOptions{
Options: options,
Default: parameterSchema.DefaultSourceValue,
HideSearch: true,
})
if err == nil {
_, _ = fmt.Fprintln(cmd.OutOrStdout())
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+Styles.Prompt.String()+Styles.Field.Render(value))
}
} else {
text := "Enter a value"
if parameterSchema.DefaultSourceValue != "" {
text += fmt.Sprintf(" (default: %q)", parameterSchema.DefaultSourceValue)
}
text += ":"

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
}
11 changes: 10 additions & 1 deletion cli/cliui/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ func init() {
}

type SelectOptions struct {
Options []string
Options []string
// Default will be highlighted first if it's a valid option.
Default string
Size int
HideSearch bool
}
Expand All @@ -50,9 +52,16 @@ func Select(cmd *cobra.Command, opts SelectOptions) (string, error) {
if flag.Lookup("test.v") != nil {
return opts.Options[0], nil
}

var defaultOption interface{}
if opts.Default != "" {
defaultOption = opts.Default
}

var value string
err := survey.AskOne(&survey.Select{
Options: opts.Options,
Default: defaultOption,
PageSize: opts.Size,
}, &value, survey.WithIcons(func(is *survey.IconSet) {
is.Help.Text = "Type to search"
Expand Down
46 changes: 35 additions & 11 deletions cli/create_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -44,6 +45,7 @@ func TestCreate(t *testing.T) {
}
<-doneChan
})

t.Run("CreateFromList", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
Expand Down Expand Up @@ -74,6 +76,7 @@ func TestCreate(t *testing.T) {
}
<-doneChan
})

t.Run("FromNothing", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
Expand Down Expand Up @@ -105,33 +108,52 @@ func TestCreate(t *testing.T) {
}
<-doneChan
})

t.Run("WithParameter", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
coderdtest.NewProvisionerDaemon(t, client)

defaultValue := "something"
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
Parse: []*proto.Parse_Response{{
Type: &proto.Parse_Response_Complete{
Complete: &proto.Parse_Complete{
ParameterSchemas: []*proto.ParameterSchema{{
AllowOverrideSource: true,
Name: "region",
Description: "description",
DefaultSource: &proto.ParameterSource{
Scheme: proto.ParameterSource_DATA,
Value: "something",
ParameterSchemas: []*proto.ParameterSchema{
{
AllowOverrideSource: true,
Name: "region",
Description: "description 1",
DefaultSource: &proto.ParameterSource{
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,
ProvisionDryRun: echo.ProvisionComplete,
})

coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
cmd, root := clitest.New(t, "create", "")
Expand All @@ -145,9 +167,11 @@ func TestCreate(t *testing.T) {
err := cmd.Execute()
require.NoError(t, err)
}()

matches := []string{
"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",
}
for i := 0; i < len(matches); i += 2 {
Expand Down
2 changes: 1 addition & 1 deletion cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

_, _ = 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
}

Expand Down