Skip to content

Commit

Permalink
feat: Add markdown code block support.
Browse files Browse the repository at this point in the history
Add support for extracting extension list from markdown code blocks.
  • Loading branch information
szkiba committed Mar 19, 2023
1 parent ee6f5d5 commit 8b287c7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ For a real life example check [k6-crocus](https://github.com/szkiba/k6-crocus) a
- [name](#name)
- [version](#version)
- [with](#with)
- [markdown](#markdown)
- [platform](#platform)
- [output](#output)
- [archive](#archive)
Expand Down Expand Up @@ -50,6 +51,7 @@ Parameter | CLI | Environment
[`name`](#name) | `-n, --name=name` | `XK6BUNDLER_NAME`
[`version`](#version) | `-v, --version=version` | `XK6BUNDLER_VERSION`
[`with`](#with) | `-w, --with=extension` | `XK6BUNDLER_WITH`
[`markdown`](#markdown) | `-m, --markdown=path` | `XK6BUNDLER_MARKDOWN`
[`platform`](#platform) | `-p, --platform=target` | `XK6BUNDLER_PLATFORM`
[`output`](#output) | `-o, --output=path` | `XK6BUNDLER_OUTPUT`
[`archive`](#archive) | `-a, --archive=path` | `XK6BUNDLER_ARCHIVE`
Expand All @@ -68,6 +70,10 @@ Bundle version. Optional, if missing then xk6bundler will try to guess from `GIT

xk6 extension to add in `module[@version][=replacement]` format. When using CLI, it 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. Replacement path must be absolute. When using GitHub Action, it can contains whilespace separated list of modules. Optional, if missing then no xk6 extension will be bundled.

### markdown

Extract xk6 extension list from markdown code blocks marked with language `xk6`. Multiple `xk6` code blocks will merge to single extension list. Each line in code block contains extension in `module[@version][=replacement]` format (see [with](#with)).

### platform

Target platform in `os/arch` format. When using CLI, it can be used multiple times to add target platform. When using GitHub Action, it can contains whilespace separated list of target platforms. Optinal, default value is `linux/amd64 windows/amd64 darwin/amd64`
Expand Down
44 changes: 44 additions & 0 deletions cmd/xk6bundler/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"strings"

"github.com/jessevdk/go-flags"
"gitlab.com/golang-commonmark/markdown"
"go.k6.io/xk6"
"gopkg.in/ini.v1"
)
Expand All @@ -51,6 +52,7 @@ type options struct {
Name string `short:"n" long:"name" value-name:"name" env:"XK6BUNDLER_NAME" description:"Short name of the bundle."` //nolint:lll
Version string `short:"v" long:"version" value-name:"version" env:"XK6BUNDLER_VERSION" default:"SNAPSHOT" description:"Bundle version."` //nolint:lll
With []string `short:"w" long:"with" value-name:"extension" env:"XK6BUNDLER_WITH" env-delim:"," description:"Add extension in 'module[@version][=replacement]' format. 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. Replacement path must be absolute."` //nolint:lll
Markdown string `short:"m" long:"markdown" value-name:"markdown" env:"XK6BUNDLER_MARKDOWN" description:"Extract extension list from Markdown code blocks. Code block language should be 'xk6' and contains extension list in format 'module[@version][=replacement]'."` //nolint:lll
Platform []string `short:"p" long:"platform" value-name:"target" env:"XK6BUNDLER_PLATFORM" default:"linux/amd64" default:"windows/amd64" default:"darwin/amd64" description:"Add target platform in 'os/arch' format. Can be used multiple times to add target platform."` //nolint:lll
Output string `short:"o" long:"output" value-name:"path" env:"XK6BUNDLER_OUTPUT" default:"dist/{{.Name}}_{{.Os}}_{{.Arch}}/k6{{.Ext}}" description:"Output file path template."` //nolint:lll
Archive string `short:"a" long:"archive" value-name:"path" env:"XK6BUNDLER_ARCHIVE" default:"dist/{{.Name}}_{{.Version}}_{{.Os}}_{{.Arch}}.tar.gz" description:"Archive (.tar.gz) file path template."` //nolint:lll
Expand Down Expand Up @@ -78,6 +80,10 @@ func parseOptions(args []string) (*options, error) {
fixGitHubAction(opts)
}

if err := extractMarkdown(opts); err != nil {
return nil, err
}

if err := parseWith(opts); err != nil {
return nil, err
}
Expand Down Expand Up @@ -281,3 +287,41 @@ func parsePlatform(opts *options) error {

return nil
}

func extractMarkdown(opts *options) error {
if len(opts.Markdown) == 0 {
return nil
}

data, err := os.ReadFile(opts.Markdown)
if err != nil {
return err
}

md := markdown.New(markdown.XHTMLOutput(true), markdown.Nofollow(true))
tokens := md.Parse(data)

buffer := new(strings.Builder)

for _, t := range tokens {
switch t := t.(type) {
case *markdown.Fence:
if t.Params == "xk6" {
if _, err := buffer.WriteString(t.Content); err != nil {
return err
}

if _, err := buffer.WriteRune('\n'); err != nil {
return err
}
}
default:
}
}

if buffer.Len() > 0 {
opts.With = append(opts.With, strings.Fields(buffer.String())...)
}

return nil
}
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/go-task/slim-sprig v2.20.0+incompatible
github.com/jessevdk/go-flags v1.5.0
gitlab.com/golang-commonmark/markdown v0.0.0-20211110145824-bf3e522c626a
go.k6.io/xk6 v0.9.0
gopkg.in/ini.v1 v1.67.0
)
Expand All @@ -17,7 +18,12 @@ require (
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/stretchr/testify v1.8.2 // indirect
gitlab.com/golang-commonmark/html v0.0.0-20191124015941-a22733972181 // indirect
gitlab.com/golang-commonmark/linkify v0.0.0-20200225224916-64bca66f6ad3 // indirect
gitlab.com/golang-commonmark/mdurl v0.0.0-20191124015652-932350d1cb84 // indirect
gitlab.com/golang-commonmark/puny v0.0.0-20191124015043-9f83538fa04f // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,39 @@ github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LF
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gitlab.com/golang-commonmark/html v0.0.0-20191124015941-a22733972181 h1:K+bMSIx9A7mLES1rtG+qKduLIXq40DAzYHtb0XuCukA=
gitlab.com/golang-commonmark/html v0.0.0-20191124015941-a22733972181/go.mod h1:dzYhVIwWCtzPAa4QP98wfB9+mzt33MSmM8wsKiMi2ow=
gitlab.com/golang-commonmark/linkify v0.0.0-20191026162114-a0c2df6c8f82/go.mod h1:Gn+LZmCrhPECMD3SOKlE+BOHwhOYD9j7WT9NUtkCrC8=
gitlab.com/golang-commonmark/linkify v0.0.0-20200225224916-64bca66f6ad3 h1:1Coh5BsUBlXoEJmIEaNzVAWrtg9k7/eJzailMQr1grw=
gitlab.com/golang-commonmark/linkify v0.0.0-20200225224916-64bca66f6ad3/go.mod h1:Gn+LZmCrhPECMD3SOKlE+BOHwhOYD9j7WT9NUtkCrC8=
gitlab.com/golang-commonmark/markdown v0.0.0-20211110145824-bf3e522c626a h1:O85GKETcmnCNAfv4Aym9tepU8OE0NmcZNqPlXcsBKBs=
gitlab.com/golang-commonmark/markdown v0.0.0-20211110145824-bf3e522c626a/go.mod h1:LaSIs30YPGs1H5jwGgPhLzc8vkNc/k0rDX/fEZqiU/M=
gitlab.com/golang-commonmark/mdurl v0.0.0-20191124015652-932350d1cb84 h1:qqjvoVXdWIcZCLPMlzgA7P9FZWdPGPvP/l3ef8GzV6o=
gitlab.com/golang-commonmark/mdurl v0.0.0-20191124015652-932350d1cb84/go.mod h1:IJZ+fdMvbW2qW6htJx7sLJ04FEs4Ldl/MDsJtMKywfw=
gitlab.com/golang-commonmark/puny v0.0.0-20191124015043-9f83538fa04f h1:Wku8eEdeJqIOFHtrfkYUByc4bCaTeA6fL0UJgfEiFMI=
gitlab.com/golang-commonmark/puny v0.0.0-20191124015043-9f83538fa04f/go.mod h1:Tiuhl+njh/JIg0uS/sOJVYi0x2HEa5rc1OAaVsb5tAs=
gitlab.com/opennota/wd v0.0.0-20180912061657-c5d65f63c638 h1:uPZaMiz6Sz0PZs3IZJWpU5qHKGNy///1pacZC9txiUI=
gitlab.com/opennota/wd v0.0.0-20180912061657-c5d65f63c638/go.mod h1:EGRJaqe2eO9XGmFtQCvV3Lm9NLico3UhFwUpCG/+mVU=
go.k6.io/xk6 v0.9.0 h1:6ihaW4Qsi+Lh3FB6PWwg6Qq+Z6uf4wnkib90smsMTGs=
go.k6.io/xk6 v0.9.0/go.mod h1:4+BwjriwZ52roJ9RHHQhTBb9Q6ZctEUtsCO10H8rJIo=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
Expand Down

0 comments on commit 8b287c7

Please # to comment.