Skip to content
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

Fix some CLI UX issues #5

Merged
merged 4 commits into from
Dec 2, 2020
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
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

This command line tool and associated Go package makes it easy to make custom builds of [k6](https://github.com/loadimpact/k6).

It is used heavily by k6 plugin developers as well as anyone who wishes to make custom `k6` binaries (with or without plugins).
It is used heavily by k6 extension developers as well as anyone who wishes to make custom `k6` binaries (with or without extensions).

⚠️ Still in development.

Expand All @@ -27,7 +27,7 @@ $ go get -u github.com/k6io/xk6/cmd/xk6
The `xk6` command has two primary uses:

1. Compile custom `k6` binaries
2. A replacement for `go run` while developing k6 plugins
2. A replacement for `go run` while developing k6 extensions

The `xk6` command will use the latest version of k6 by default. You can customize this for all invocations by setting the `K6_VERSION` environment variable.

Expand All @@ -46,27 +46,30 @@ $ xk6 build [<k6_version>]

- `<k6_version>` is the core k6 version to build; defaults to `K6_VERSION` env variable or latest.
- `--output` changes the output file.
- `--with` can be used multiple times to add plugins by specifying the Go module name and optionally its version, similar to `go get`. Module name is required, but specific version and/or local replacement are optional.
- `--with` can be used multiple times to add extensions by specifying the Go module name and optionally its version, similar to `go get`. Module name is required, but specific version and/or local replacement are optional.

Examples:

```bash
$ xk6 build \
--with github.com/k6io/xk6-sql

$ xk6 build v2.0.1 \
--with github.com/k6io/xk6-sql@v0.1.1
$ xk6 build v0.29.0 \
--with github.com/k6io/xk6-sql@v0.0.1

$ xk6 build \
--with github.com/k6io/xk6-sql=../../my-fork

$ xk6 build \
--with github.com/k6io/[email protected]=../../my-fork
--with github.com/k6io/xk6-sql=.

$ xk6 build \
--with github.com/k6io/[email protected]=../../my-fork
```

### For plugin development
### For extension development

If you run `xk6` from within the folder of the k6 plugin you're working on _without the `build` subcommand_, it will build k6 with your current module and run it, as if you manually plugged it in and invoked `go run`.
If you run `xk6` from within the folder of the k6 extension you're working on _without the `build` subcommand_, it will build k6 with your current module and run it, as if you manually plugged it in and invoked `go run`.

The binary will be built and run from the current directory, then cleaned up.

Expand All @@ -82,9 +85,8 @@ $ xk6 <args...>
For example:

```bash
$ xk6 list-modules
$ xk6 run
$ xk6 run --config config.json
$ xk6 version
$ xk6 run -u 10 -d 10s test.js
```

The race detector can be enabled by setting `XK6_RACE_DETECTOR=1`.
Expand All @@ -94,11 +96,11 @@ The race detector can be enabled by setting `XK6_RACE_DETECTOR=1`.

```go
builder := xk6.Builder{
k6Version: "v2.0.0",
k6Version: "v0.29.0",
Plugins: []xk6.Dependency{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's a lot of mention of "plugins" still, but not sure if we should bother with converting them all to "extensions".

Copy link
Contributor

Choose a reason for hiding this comment

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

I am for s/plugin/extension/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, let's do this in #7.

{
ModulePath: "github.com/k6io/xk6-sql",
Version: "v0.1.1",
Version: "v0.0.1",
},
},
}
Expand All @@ -111,7 +113,7 @@ Versions can be anything compatible with `go get`.

## Environment variables

Because the subcommands and flags are constrained to benefit rapid plugin prototyping, xk6 does read some environment variables to take cues for its behavior and/or configuration when there is no room for flags.
Because the subcommands and flags are constrained to benefit rapid extension prototyping, xk6 does read some environment variables to take cues for its behavior and/or configuration when there is no room for flags.

- `K6_VERSION` sets the version of k6 to build.
- `XK6_RACE_DETECTOR=1` enables the Go race detector in the build.
Expand Down
13 changes: 10 additions & 3 deletions cmd/xk6/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ func runBuild(ctx context.Context, args []string) error {
Version: ver,
})
if repl != "" {
if repl == "." {
if cwd, err := os.Getwd(); err != nil {
return err
} else {
repl = cwd
}
}
replacements = append(replacements, xk6.NewReplace(mod, repl))
}

Expand Down Expand Up @@ -139,9 +146,9 @@ func runBuild(ctx context.Context, args []string) error {

func getK6OutputFile() string {
if runtime.GOOS == "windows" {
return "k6.exe"
return ".\\k6.exe"
}
return "k6"
return "./k6"
Comment on lines -142 to +151
Copy link
Contributor

Choose a reason for hiding this comment

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

I am more inclined to have runDev use a temporary place for the k6 binary instead of overwriting the one in the current directory and then ... possibly deleting it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I slightly disagree. I get the overwriting/deletion concern, but this is meant to help with quick dev iterations when run from an extension repo, and it's much easier to have the binary in the CWD than in a temp dir somewhere... If you care about where it's written, you should use xk6 build --output instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

But ... you delete the file after that ... unless an env variable is set - so in the majority of the cases you will just delete the file 'k6' in the local directory, nothing else.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, and if XK6_SKIP_CLEANUP is set then I'll have to hunt down the binary in a temp dir, which is more annoying than it overwriting/deleting ./k6, that should've been produced by a previous xk6 run anyway.

@na-- Tie breaker? 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

The path is literally a few lines above
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess we can resolve this later :)

}

func runDev(ctx context.Context, args []string) error {
Expand Down Expand Up @@ -175,7 +182,7 @@ func runDev(ctx context.Context, args []string) error {
// and since this tool is a carry-through for the user's actual
// go.mod, we need to transfer their replace directives through
// to the one we're making
cmd = exec.Command("go", "list", "-m", "-f={{if .Replace}}{{.Path}} => {{.Replace}}{{end}}", "all")
cmd = exec.Command("go", "list", "-mod=readonly", "-m", "-f={{if .Replace}}{{.Path}} => {{.Replace}}{{end}}", "all")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if this can be considered a fix, but at least it doesn't fail when run from a vendored module directory.

cmd.Stderr = os.Stderr
out, err = cmd.Output()
if err != nil {
Expand Down