Skip to content

Commit

Permalink
Rough s/xcaddy/xk6
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mirić committed Oct 21, 2020
1 parent 4f3e0ef commit 3e0f794
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 117 deletions.
74 changes: 37 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
`xcaddy` - Custom Caddy Builder
`xk6` - Custom k6 Builder
===============================

This command line tool and associated Go package makes it easy to make custom builds of the [Caddy Web Server](https://github.com/caddyserver/caddy).
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 Caddy plugin developers as well as anyone who wishes to make custom `caddy` binaries (with or without plugins).
It is used heavily by k6 plugin developers as well as anyone who wishes to make custom `k6` binaries (with or without plugins).

⚠️ Still in development. Supports Caddy 2 only.
⚠️ Still in development.

Stay updated, be aware of changes, and please submit feedback! Thanks!

Expand All @@ -15,58 +15,58 @@ Stay updated, be aware of changes, and please submit feedback! Thanks!

## Install

You can [download binaries](https://github.com/caddyserver/xcaddy/releases) that are already compiled for your platform, or build `xcaddy` from source:
You can [download binaries](https://github.com/k6io/xk6/releases) that are already compiled for your platform, or build `xk6` from source:

```bash
$ go get -u github.com/caddyserver/xcaddy/cmd/xcaddy
$ go get -u github.com/k6io/xk6/cmd/xk6
```


## Command usage

The `xcaddy` command has two primary uses:
The `xk6` command has two primary uses:

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

The `xcaddy` command will use the latest version of Caddy by default. You can customize this for all invocations by setting the `CADDY_VERSION` environment variable.
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.

As usual with `go` command, the `xcaddy` command will pass the `GOOS`, `GOARCH`, and `GOARM` environment variables through for cross-compilation.
As usual with `go` command, the `xk6` command will pass the `GOOS`, `GOARCH`, and `GOARM` environment variables through for cross-compilation.


### Custom builds

Syntax:

```
$ xcaddy build [<caddy_version>]
$ xk6 build [<k6_version>]
[--output <file>]
[--with <module[@version][=replacement]>...]
```

- `<caddy_version>` is the core Caddy version to build; defaults to `CADDY_VERSION` env variable or latest.
- `<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.

Examples:

```bash
$ xcaddy build \
--with github.com/caddyserver/ntlm-transport
$ xk6 build \
--with github.com/k6io/xk6-sql

$ xcaddy build v2.0.1 \
--with github.com/caddyserver/ntlm-transport@v0.1.1
$ xk6 build v2.0.1 \
--with github.com/k6io/xk6-sql@v0.1.1

$ xcaddy build \
--with github.com/caddyserver/ntlm-transport=../../my-fork
$ xk6 build \
--with github.com/k6io/xk6-sql=../../my-fork

$ xcaddy build \
--with github.com/caddyserver/ntlm-transport@v0.1.1=../../my-fork
$ xk6 build \
--with github.com/k6io/xk6-sql@v0.1.1=../../my-fork
```

### For plugin development

If you run `xcaddy` from within the folder of the Caddy plugin you're working on _without the `build` subcommand_, it will build Caddy 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 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`.

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

Expand All @@ -75,34 +75,34 @@ The current working directory must be inside an initialized Go module.
Syntax:

```
$ xcaddy <args...>
$ xk6 <args...>
```
- `<args...>` are passed through to the `caddy` command.
- `<args...>` are passed through to the `k6` command.

For example:

```bash
$ xcaddy list-modules
$ xcaddy run
$ xcaddy run --config caddy.json
$ xk6 list-modules
$ xk6 run
$ xk6 run --config config.json
```

The race detector can be enabled by setting `XCADDY_RACE_DETECTOR=1`.
The race detector can be enabled by setting `XK6_RACE_DETECTOR=1`.


## Library usage

```go
builder := xcaddy.Builder{
CaddyVersion: "v2.0.0",
Plugins: []xcaddy.Dependency{
builder := xk6.Builder{
k6Version: "v2.0.0",
Plugins: []xk6.Dependency{
{
ModulePath: "github.com/caddyserver/ntlm-transport",
ModulePath: "github.com/k6io/xk6-sql",
Version: "v0.1.1",
},
},
}
err := builder.Build(context.Background(), "./caddy")
err := builder.Build(context.Background(), "./k6")
```

Versions can be anything compatible with `go get`.
Expand All @@ -111,11 +111,11 @@ Versions can be anything compatible with `go get`.

## Environment variables

Because the subcommands and flags are constrained to benefit rapid plugin prototyping, xcaddy 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 plugin prototyping, xk6 does read some environment variables to take cues for its behavior and/or configuration when there is no room for flags.

- `CADDY_VERSION` sets the version of Caddy to build.
- `XCADDY_RACE_DETECTOR=1` enables the Go race detector in the build.
- `XCADDY_SKIP_CLEANUP=1` causes xcaddy to leave build artifacts on disk after exiting.
- `K6_VERSION` sets the version of k6 to build.
- `XK6_RACE_DETECTOR=1` enables the Go race detector in the build.
- `XK6_SKIP_CLEANUP=1` causes xk6 to leave build artifacts on disk after exiting.


---
Expand Down
12 changes: 6 additions & 6 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package xcaddy
package xk6

import (
"context"
Expand All @@ -31,11 +31,11 @@ import (
"github.com/Masterminds/semver/v3"
)

// Builder can produce a custom Caddy build with the
// Builder can produce a custom k6 build with the
// configuration it represents.
type Builder struct {
Compile
CaddyVersion string `json:"caddy_version,omitempty"`
K6Version string `json:"k6_version,omitempty"`
Plugins []Dependency `json:"plugins,omitempty"`
Replacements []Replace `json:"replacements,omitempty"`
TimeoutGet time.Duration `json:"timeout_get,omitempty"`
Expand All @@ -44,7 +44,7 @@ type Builder struct {
SkipCleanup bool `json:"skip_cleanup,omitempty"`
}

// Build builds Caddy at the configured version with the
// Build builds k6 at the configured version with the
// configured plugins and plops down a binary at outputFile.
func (b Builder) Build(ctx context.Context, outputFile string) error {
if outputFile == "" {
Expand Down Expand Up @@ -90,7 +90,7 @@ func (b Builder) Build(ctx context.Context, outputFile string) error {
}
env = setEnv(env, fmt.Sprintf("CGO_ENABLED=%s", b.Compile.CgoEnabled()))

log.Println("[INFO] Building Caddy")
log.Println("[INFO] Building k6")

// compile
cmd := buildEnv.newCommand("go", "build",
Expand Down Expand Up @@ -250,5 +250,5 @@ const (
// used for temporary folder paths.
yearMonthDayHourMin = "2006-01-02-1504"

defaultCaddyModulePath = "github.com/caddyserver/caddy"
defaultK6ModulePath = "github.com/loadimpact/k6"
)
2 changes: 1 addition & 1 deletion builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package xcaddy
package xk6

import (
"fmt"
Expand Down
62 changes: 31 additions & 31 deletions cmd/xcaddy/main.go → cmd/xk6/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import (
"runtime"
"strings"

"github.com/caddyserver/xcaddy"
"github.com/k6io/xk6"
)

var (
caddyVersion = os.Getenv("CADDY_VERSION")
raceDetector = os.Getenv("XCADDY_RACE_DETECTOR") == "1"
skipCleanup = os.Getenv("XCADDY_SKIP_CLEANUP") == "1"
k6Version = os.Getenv("K6_VERSION")
raceDetector = os.Getenv("XK6_RACE_DETECTOR") == "1"
skipCleanup = os.Getenv("XK6_SKIP_CLEANUP") == "1"
)

func main() {
Expand All @@ -54,9 +54,9 @@ func main() {

func runBuild(ctx context.Context, args []string) error {
// parse the command line args... rather primitively
var argCaddyVersion, output string
var plugins []xcaddy.Dependency
var replacements []xcaddy.Replace
var argK6Version, output string
var plugins []xk6.Dependency
var replacements []xk6.Replace
for i := 0; i < len(args); i++ {
switch args[i] {
case "--with":
Expand All @@ -69,12 +69,12 @@ func runBuild(ctx context.Context, args []string) error {
return err
}
mod = strings.TrimSuffix(mod, "/") // easy to accidentally leave a trailing slash if pasting from a URL, but is invalid for Go modules
plugins = append(plugins, xcaddy.Dependency{
plugins = append(plugins, xk6.Dependency{
PackagePath: mod,
Version: ver,
})
if repl != "" {
replacements = append(replacements, xcaddy.NewReplace(mod, repl))
replacements = append(replacements, xk6.NewReplace(mod, repl))
}

case "--output":
Expand All @@ -85,29 +85,29 @@ func runBuild(ctx context.Context, args []string) error {
output = args[i]

default:
if argCaddyVersion != "" {
return fmt.Errorf("missing flag; caddy version already set at %s", argCaddyVersion)
if argK6Version != "" {
return fmt.Errorf("missing flag; k6 version already set at %s", argK6Version)
}
argCaddyVersion = args[i]
argK6Version = args[i]
}
}

// prefer caddy version from command line argument over env var
if argCaddyVersion != "" {
caddyVersion = argCaddyVersion
// prefer k6 version from command line argument over env var
if argK6Version != "" {
k6Version = argK6Version
}

// ensure an output file is always specified
if output == "" {
output = getCaddyOutputFile()
output = getK6OutputFile()
}

// perform the build
builder := xcaddy.Builder{
Compile: xcaddy.Compile{
builder := xk6.Builder{
Compile: xk6.Compile{
Cgo: os.Getenv("CGO_ENABLED") == "1",
},
CaddyVersion: caddyVersion,
K6Version: k6Version,
Plugins: plugins,
Replacements: replacements,
RaceDetector: raceDetector,
Expand Down Expand Up @@ -137,15 +137,15 @@ func runBuild(ctx context.Context, args []string) error {
return nil
}

func getCaddyOutputFile() string {
func getK6OutputFile() string {
if runtime.GOOS == "windows" {
return "caddy.exe"
return "k6.exe"
}
return "caddy"
return "k6"
}

func runDev(ctx context.Context, args []string) error {
binOutput := getCaddyOutputFile()
binOutput := getK6OutputFile()

// get current/main module name
cmd := exec.Command("go", "list", "-m")
Expand All @@ -167,8 +167,8 @@ func runDev(ctx context.Context, args []string) error {

// make sure the module being developed is replaced
// so that the local copy is used
replacements := []xcaddy.Replace{
xcaddy.NewReplace(currentModule, moduleDir),
replacements := []xk6.Replace{
xk6.NewReplace(currentModule, moduleDir),
}

// replace directives only apply to the top-level/main go.mod,
Expand All @@ -186,7 +186,7 @@ func runDev(ctx context.Context, args []string) error {
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
continue
}
replacements = append(replacements, xcaddy.NewReplace(
replacements = append(replacements, xk6.NewReplace(
strings.TrimSpace(parts[0]),
strings.TrimSpace(parts[1]),
))
Expand All @@ -201,13 +201,13 @@ func runDev(ctx context.Context, args []string) error {
}
importPath := normalizeImportPath(currentModule, cwd, moduleDir)

// build caddy with this module plugged in
builder := xcaddy.Builder{
Compile: xcaddy.Compile{
// build k6 with this module plugged in
builder := xk6.Builder{
Compile: xk6.Compile{
Cgo: os.Getenv("CGO_ENABLED") == "1",
},
CaddyVersion: caddyVersion,
Plugins: []xcaddy.Dependency{
K6Version: k6Version,
Plugins: []xk6.Dependency{
{PackagePath: importPath},
},
Replacements: replacements,
Expand Down
Loading

0 comments on commit 3e0f794

Please sign in to comment.