Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: Add "coder projects create" command#246

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
kylecarbs merged 21 commits intomainfromcreateproject
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
21 commits
Select commitHold shift + click to select a range
7167fa0
Refactor parameter parsing to return nil values if none computed
kylecarbsFeb 10, 2022
65380db
Refactor parameter to allow for hiding redisplay
kylecarbsFeb 10, 2022
94eb484
Refactor parameters to enable schema matching
kylecarbsFeb 10, 2022
a6ce22d
Refactor provisionerd to dynamically update parameter schemas
kylecarbsFeb 10, 2022
e53f0be
Refactor job update for provisionerd
kylecarbsFeb 10, 2022
4466836
Handle multiple states correctly when provisioning a project
kylecarbsFeb 10, 2022
8fe05d6
Add project import job resource table
kylecarbsFeb 10, 2022
79a56b6
Basic creation flow works!
kylecarbsFeb 10, 2022
dc86c0e
Create project fully works!!!
kylecarbsFeb 10, 2022
bff96b6
Only show job status if completed
kylecarbsFeb 11, 2022
8766a33
Add create workspace support
kylecarbsFeb 11, 2022
aac220f
Replace Netflix/go-expect with ActiveState
kylecarbsFeb 11, 2022
c493bf9
Fix linting errors
kylecarbsFeb 11, 2022
485c07b
Use forked chzyer/readline
kylecarbsFeb 11, 2022
b5a774a
Add create workspace CLI
kylecarbsFeb 11, 2022
d2cbf36
Add CLI test
kylecarbsFeb 11, 2022
a8d00d8
Move jobs to their own APIs
kylecarbsFeb 11, 2022
9c7746f
Merge branch 'main' into createproject
kylecarbsFeb 11, 2022
e4770bb
Remove go-expect
kylecarbsFeb 12, 2022
c6cee94
Fix requested changes
kylecarbsFeb 12, 2022
f9814be
Skip workspacecreate test on windows
kylecarbsFeb 12, 2022
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
1 change: 1 addition & 0 deletions.vscode/settings.json
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,6 +44,7 @@
"nhooyr",
"nolint",
"nosec",
"ntqry",
"oneof",
"parameterscopeid",
"promptui",
Expand Down
82 changes: 79 additions & 3 deletionscli/clitest/clitest.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
package clitest

import (
"archive/tar"
"bufio"
"bytes"
"errors"
"io"
"os"
"path/filepath"
"regexp"
"testing"

"github.com/Netflix/go-expect"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"

"github.com/coder/coder/cli"
"github.com/coder/coder/cli/config"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/provisioner/echo"
Copy link
Contributor

Choose a reason for hiding this comment

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

Neat 🎉

)

var (
// Used to ensure terminal output doesn't have anything crazy!
// See: https://stackoverflow.com/a/29497680
stripAnsi = regexp.MustCompile("[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))")
)

// New creates a CLI instance with a configuration pointed to a
// temporary testing directory.
func New(t *testing.T, args ...string) (*cobra.Command, config.Root) {
cmd := cli.Root()
dir := t.TempDir()
Expand All@@ -19,7 +37,27 @@ func New(t *testing.T, args ...string) (*cobra.Command, config.Root) {
return cmd, root
}

func StdoutLogs(t *testing.T) io.Writer {
// SetupConfig applies the URL and SessionToken of the client to the config.
func SetupConfig(t *testing.T, client *codersdk.Client, root config.Root) {
err := root.Session().Write(client.SessionToken)
require.NoError(t, err)
err = root.URL().Write(client.URL.String())
require.NoError(t, err)
}

// CreateProjectVersionSource writes the echo provisioner responses into a
// new temporary testing directory.
func CreateProjectVersionSource(t *testing.T, responses *echo.Responses) string {
directory := t.TempDir()
data, err := echo.Tar(responses)
require.NoError(t, err)
extractTar(t, data, directory)
return directory
}

// NewConsole creates a new TTY bound to the command provided.
// All ANSI escape codes are stripped to provide clean output.
func NewConsole(t *testing.T, cmd *cobra.Command) *expect.Console {
reader, writer := io.Pipe()
scanner := bufio.NewScanner(reader)
t.Cleanup(func() {
Expand All@@ -31,8 +69,46 @@ func StdoutLogs(t *testing.T) io.Writer {
if scanner.Err() != nil {
return
}
t.Log(scanner.Text())
t.Log(stripAnsi.ReplaceAllString(scanner.Text(), ""))
}
}()
return writer

console, err := expect.NewConsole(expect.WithStdout(writer))
require.NoError(t, err)
cmd.SetIn(console.Tty())
cmd.SetOut(console.Tty())
return console
}

func extractTar(t *testing.T, data []byte, directory string) {
reader := tar.NewReader(bytes.NewBuffer(data))
for {
header, err := reader.Next()
if errors.Is(err, io.EOF) {
break
}
require.NoError(t, err)
// #nosec
path := filepath.Join(directory, header.Name)
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should have a check here to sanitize theheader.Name - making sure we can't specify paths like.. that might move up the filesystem. There might be a vulnerability where you could construct atar that has a header with improper paths (like../../) - and let you write a file over an existing file.

This would be bad, for example, if a malicious actor could write over a critical config file, a binary (ie, they overwrite ourprovisionerd with a malicious version that uploads secrets).

Kind of a similar to the example called out here:securego/gosec#439 (comment)

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Yes, and we handle this from provisionerd. But I didn't feel it was necessary forclitest.

We should abstract the tar/untar logic out so it's in one place, then add tests for that specific functionality.

mode := header.FileInfo().Mode()
if mode == 0 {
mode = 0600
}
switch header.Typeflag {
case tar.TypeDir:
err = os.MkdirAll(path, mode)
require.NoError(t, err)
case tar.TypeReg:
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, mode)
require.NoError(t, err)
// Max file size of 10MB.
_, err = io.CopyN(file, reader, (1<<20)*10)
if errors.Is(err, io.EOF) {
err = nil
}
require.NoError(t, err)
err = file.Close()
require.NoError(t, err)
}
}
}
31 changes: 31 additions & 0 deletionscli/clitest/clitest_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
//go:build !windows

package clitest_test

import (
"testing"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}

func TestCli(t *testing.T) {
t.Parallel()
clitest.CreateProjectVersionSource(t, nil)
client := coderdtest.New(t)
cmd, config := clitest.New(t)
clitest.SetupConfig(t, client, config)
console := clitest.NewConsole(t, cmd)
go func() {
err := cmd.Execute()
require.NoError(t, err)
}()
_, err := console.ExpectString("coder")
require.NoError(t, err)
}
10 changes: 5 additions & 5 deletionscli/login.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -49,7 +49,7 @@ func login() *cobra.Command {
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s Your Coder deployment hasn't been set up!\n", color.HiBlackString(">"))

_, err :=runPrompt(cmd, &promptui.Prompt{
_, err :=prompt(cmd, &promptui.Prompt{
Label: "Would you like to create the first user?",
IsConfirm: true,
Default: "y",
Expand All@@ -61,23 +61,23 @@ func login() *cobra.Command {
if err != nil {
return xerrors.Errorf("get current user: %w", err)
}
username, err :=runPrompt(cmd, &promptui.Prompt{
username, err :=prompt(cmd, &promptui.Prompt{
Label: "What username would you like?",
Default: currentUser.Username,
})
if err != nil {
return xerrors.Errorf("pick username prompt: %w", err)
}

organization, err :=runPrompt(cmd, &promptui.Prompt{
organization, err :=prompt(cmd, &promptui.Prompt{
Label: "What is the name of your organization?",
Default: "acme-corp",
})
if err != nil {
return xerrors.Errorf("pick organization prompt: %w", err)
}

email, err :=runPrompt(cmd, &promptui.Prompt{
email, err :=prompt(cmd, &promptui.Prompt{
Label: "What's your email?",
Validate: func(s string) error {
err := validator.New().Var(s, "email")
Expand All@@ -91,7 +91,7 @@ func login() *cobra.Command {
return xerrors.Errorf("specify email prompt: %w", err)
}

password, err :=runPrompt(cmd, &promptui.Prompt{
password, err :=prompt(cmd, &promptui.Prompt{
Label: "Enter a password:",
Mask: '*',
})
Expand Down
11 changes: 3 additions & 8 deletionscli/login_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,8 +8,6 @@ import (
"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/stretchr/testify/require"

"github.com/Netflix/go-expect"
)

func TestLogin(t *testing.T) {
Expand All@@ -24,12 +22,9 @@ func TestLogin(t *testing.T) {

t.Run("InitialUserTTY", func(t *testing.T) {
t.Parallel()
console, err := expect.NewConsole(expect.WithStdout(clitest.StdoutLogs(t)))
require.NoError(t, err)
client := coderdtest.New(t)
root, _ := clitest.New(t, "login", client.URL.String())
root.SetIn(console.Tty())
root.SetOut(console.Tty())
console := clitest.NewConsole(t, root)
go func() {
err := root.Execute()
require.NoError(t, err)
Expand All@@ -45,12 +40,12 @@ func TestLogin(t *testing.T) {
for i := 0; i < len(matches); i += 2 {
match := matches[i]
value := matches[i+1]
_, err = console.ExpectString(match)
_, err:= console.ExpectString(match)
require.NoError(t, err)
_, err = console.SendLine(value)
require.NoError(t, err)
}
_, err = console.ExpectString("Welcome to Coder")
_, err:= console.ExpectString("Welcome to Coder")
require.NoError(t, err)
})
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp