Skip to content

feat(cli): provide parameter values via command line #8898

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 34 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
More fixes
  • Loading branch information
mtojek committed Aug 8, 2023
commit 177127c85728ddeb3c5dd258650132310ea68e8c
36 changes: 36 additions & 0 deletions cli/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli_test

import (
"context"
"fmt"
"net/http"
"os"
"regexp"
Expand Down Expand Up @@ -357,6 +358,41 @@ func TestCreateWithRichParameters(t *testing.T) {
}
<-doneChan
})

t.Run("ParameterFlags", func(t *testing.T) {
t.Parallel()

client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, echoResponses)
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)

template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

inv, root := clitest.New(t, "create", "my-workspace", "--template", template.Name,
"--parameter", fmt.Sprintf("%s=%s", firstParameterName, firstParameterValue),
"--parameter", fmt.Sprintf("%s=%s", secondParameterName, secondParameterValue),
"--parameter", fmt.Sprintf("%s=%s", immutableParameterName, immutableParameterValue))
clitest.SetupConfig(t, client, root)
doneChan := make(chan struct{})
pty := ptytest.New(t).Attach(inv)
go func() {
defer close(doneChan)
err := inv.Run()
assert.NoError(t, err)
}()

matches := []string{
"Confirm create?", "yes",
}
for i := 0; i < len(matches); i += 2 {
match := matches[i]
value := matches[i+1]
pty.ExpectMatch(match)
pty.WriteLine(value)
}
<-doneChan
})
}

func TestCreateValidateRichParameters(t *testing.T) {
Expand Down
22 changes: 10 additions & 12 deletions cli/parameterresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,16 @@ func (pr *ParameterResolver) resolveWithCommandLineOrEnv(resolved []codersdk.Wor
richParameterDone:
}

if pr.promptBuildOptions {
for _, buildOption := range pr.buildOptions {
for i, r := range resolved {
if r.Name == buildOption.Name {
resolved[i].Value = buildOption.Value
goto buildOptionDone
}
for _, buildOption := range pr.buildOptions {
for i, r := range resolved {
if r.Name == buildOption.Name {
resolved[i].Value = buildOption.Value
goto buildOptionDone
}

resolved = append(resolved, buildOption)
buildOptionDone:
}

resolved = append(resolved, buildOption)
buildOptionDone:
}
return resolved
}
Expand Down Expand Up @@ -162,8 +160,8 @@ func (pr *ParameterResolver) verifyConstraints(resolved []codersdk.WorkspaceBuil
return xerrors.Errorf("parameter %q is not present in the template", r.Name)
}

if tvp.Ephemeral && !pr.promptBuildOptions {
return xerrors.Errorf("ephemeral parameter %q can be used only with --build-options flag", r.Name)
if tvp.Ephemeral && !pr.promptBuildOptions && len(pr.buildOptions) == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we check len 0 here? Wouldn't it be possible for some build options to be provided but not this one? Should are check the contents instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition only applies to ephemeral/one-time build parameters, hence tvp.Ephemeral. !pr.promptBuildOptions means that the user didn't use the flag --build-options to input manually values for next build options, and len(pr.buildOptions) == 0 means that they didn't provide them via --build-option key=value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the reasoning for the other checks, however my thinking is that, say there are two ephemeral values. The user providers only one of them via --build-option. Then len(pr.buildOptions) == 1 even though current tvp wasn't provided. Does that make sense?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return xerrors.Errorf("ephemeral parameter %q can be used only with --build-options or --build-option flag", r.Name)
}

if !tvp.Mutable && action != WorkspaceCreate {
Expand Down