Skip to content

Commit

Permalink
Merge pull request #27 from grafana/26-simplify-responsibility
Browse files Browse the repository at this point in the history
Remove the jq filter feature
  • Loading branch information
szkiba authored Aug 16, 2024
2 parents d753925 + 097fad5 commit b902953
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 127 deletions.
25 changes: 9 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

**Data model and tooling for the k6 extension registry**

This repository contains the [JSON schema](docs/registry.schema.json) of the k6 extension registry and the [`k6registry`](#k6registry) command line tool for registry processing. The command line tool can also be used as a [GitHub Action](#github-action).
This repository contains the [JSON schema](docs/registry.schema.json) of the k6 extension registry and the [`k6registry`](#k6registry) command line tool for generating registry from source. The command line tool can also be used as a [GitHub Action](#github-action).

Check [k6 Extension Registry Concept](docs/registry.md) for information on design considerations.

**Example registry**
**Example registry source**

```yaml file=docs/example.yaml
- module: github.com/grafana/xk6-dashboard
Expand Down Expand Up @@ -62,15 +62,12 @@ The output of the processing will be written to the standard output by default.

name | reqired | default | description
-------|---------|---------|-------------
filter | no | `.` | jq compatible filter
in | yes | | input file name
out | no | stdout | output file name
mute | no | `false` | no output, only validation
loose | no | `false` | skip JSON schema validation
lint | no | `false` | enable built-in linter
compact| no | `false` | compact instead of pretty-printed output
raw | no | `false` | output raw strings, not JSON texts
yaml | no | `false` | output YAML instead of JSON
ref | no | | reference output URL for change detection

In GitHub action mode, the change can be indicated by comparing the output to a reference output. The reference output URL can be passed in the `ref` action parameter. The `changed` output variable will be `true` or `false` depending on whether the output has changed or not compared to the reference output.
Expand All @@ -97,25 +94,23 @@ changed | `true` if the output has changed compared to `ref`, otherwise `false`
<!-- #region cli -->
## k6registry
k6 extension registry processor
k6 extension registry generator
### Synopsis
Command line k6 extension registry processor.
Command line k6 extension registry generator.
k6registry is a command line tool that enables k6 extension registry processing and the generation of customized JSON output for different applications. Processing is based on popular `jq` expressions using an embedded `jq` implementation.
The source of the extension registry contains only the most important properties of the extensions. The rest of the properties are collected by k6registry using the API of the extensions' git repository managers.
The first argument is the jq filter expression. This is the basis for processing.
The source of the extension registry is read from the YAML format file specified as command line argument. If it is missing, the source is read from the standard input.
The extension registry is read from the YAML format file specified in the second argument. If it is missing, the extension registry is read from the standard input.
Repository metadata is collected using the API of the extensions' git repository managers. Currently only the GitHub API is supported.
Repository metadata is collected using the repository manager APIs. Currently only the GitHub API is supported.

The output of the processing will be written to the standard output by default. The output can be saved to a file using the `-o/--out` flag.
The output of the generation will be written to the standard output by default. The output can be saved to a file using the `-o/--out` flag.


```
k6registry [flags] <jq filter> [file]
k6registry [flags] [file]
```
### Flags
Expand All @@ -126,8 +121,6 @@ k6registry [flags] <jq filter> [file]
--loose skip JSON schema validation
--lint enable built-in linter
-c, --compact compact instead of pretty-printed output
-r, --raw output raw strings, not JSON texts
-y, --yaml output YAML instead of JSON
-V, --version print version
-h, --help help for k6registry
```
Expand Down
15 changes: 1 addition & 14 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
name: k6registry
description: k6 extension registry processor
description: k6 extension registry generator
author: Grafana Labs

branding:
icon: settings
color: purple

inputs:
filter:
description: jq compatible filter
required: false
default: .

in:
description: input file name
required: true
Expand All @@ -36,14 +31,6 @@ inputs:
description: compact instead of pretty-printed output
required: false

raw:
description: output raw strings, not JSON texts
required: false

yaml:
description: output YAML instead of JSON
required: false

ref:
description: reference output URL for change detection
required: false
Expand Down
47 changes: 8 additions & 39 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import (
"context"
_ "embed"
"encoding/json"
"fmt"
"io"
"os"

"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

//go:embed help.md
Expand All @@ -19,8 +16,6 @@ var help string
type options struct {
out string
compact bool
raw bool
yaml bool
mute bool
loose bool
lint bool
Expand All @@ -33,13 +28,13 @@ func New() (*cobra.Command, error) {
legacy := false

root := &cobra.Command{
Use: "k6registry [flags] <jq filter> [file]",
Short: "k6 extension registry processor",
Use: "k6registry [flags] [source-file]",
Short: "k6 extension registry generator",
Long: help,
SilenceUsage: true,
SilenceErrors: true,
DisableAutoGenTag: true,
Args: cobra.RangeArgs(1, 2),
Args: cobra.MaximumNArgs(1),
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
RunE: func(cmd *cobra.Command, args []string) error {
if legacy {
Expand All @@ -66,9 +61,7 @@ func New() (*cobra.Command, error) {
flags.BoolVar(&opts.loose, "loose", false, "skip JSON schema validation")
flags.BoolVar(&opts.lint, "lint", false, "enable built-in linter")
flags.BoolVarP(&opts.compact, "compact", "c", false, "compact instead of pretty-printed output")
flags.BoolVarP(&opts.raw, "raw", "r", false, "output raw strings, not JSON texts")
flags.BoolVarP(&opts.yaml, "yaml", "y", false, "output YAML instead of JSON")
root.MarkFlagsMutuallyExclusive("raw", "compact", "yaml", "mute")
root.MarkFlagsMutuallyExclusive("compact", "mute")

flags.BoolP("version", "V", false, "print version")

Expand All @@ -83,8 +76,8 @@ func New() (*cobra.Command, error) {
func run(ctx context.Context, args []string, opts *options) (result error) {
input := os.Stdin

if len(args) > 1 {
file, err := os.Open(args[1])
if len(args) > 0 {
file, err := os.Open(args[0])
if err != nil {
return err
}
Expand Down Expand Up @@ -122,32 +115,8 @@ func run(ctx context.Context, args []string, opts *options) (result error) {
return err
}

if err := jq(registry, args[0], printer(output, opts)); err != nil {
return err
}

return result
}

func printer(output io.Writer, opts *options) func(interface{}) error {
if opts.raw {
return func(v interface{}) error {
_, err := fmt.Fprintln(output, v)

return err
}
}

if opts.yaml {
encoder := yaml.NewEncoder(output)

return encoder.Encode
}

if opts.mute {
return func(_ interface{}) error {
return nil
}
return nil
}

encoder := json.NewEncoder(output)
Expand All @@ -156,5 +125,5 @@ func printer(output io.Writer, opts *options) func(interface{}) error {
encoder.SetIndent("", " ")
}

return encoder.Encode
return encoder.Encode(registry)
}
12 changes: 5 additions & 7 deletions cmd/help.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
Command line k6 extension registry processor.
Command line k6 extension registry generator.

k6registry is a command line tool that enables k6 extension registry processing and the generation of customized JSON output for different applications. Processing is based on popular `jq` expressions using an embedded `jq` implementation.
The source of the extension registry contains only the most important properties of the extensions. The rest of the properties are collected by k6registry using the API of the extensions' git repository managers.

The first argument is the jq filter expression. This is the basis for processing.
The source of the extension registry is read from the YAML (or JSON) format file specified as command line argument. If it is missing, the source is read from the standard input.

The extension registry is read from the YAML format file specified in the second argument. If it is missing, the extension registry is read from the standard input.
Repository metadata is collected using the API of the extensions' git repository managers. Currently only the GitHub API is supported.

Repository metadata is collected using the repository manager APIs. Currently only the GitHub API is supported.

The output of the processing will be written to the standard output by default. The output can be saved to a file using the `-o/--out` flag.
The output of the generation will be written to the standard output by default. The output can be saved to a file using the `-o/--out` flag.
35 changes: 0 additions & 35 deletions cmd/jq.go

This file was deleted.

10 changes: 0 additions & 10 deletions cmd/k6registry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,10 @@ func getArgs() []string {
args = append(args, "--compact")
}

if getenv("INPUT_RAW", "false") == "true" {
args = append(args, "--raw")
}

if getenv("INPUT_YAML", "false") == "true" {
args = append(args, "--yaml")
}

if out := getenv("INPUT_OUT", ""); len(out) != 0 {
args = append(args, "--out", out)
}

args = append(args, getenv("INPUT_FILTER", "."))

args = append(args, getenv("INPUT_IN", ""))

return args
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/cli/go-gh/v2 v2.9.0
github.com/google/go-github/v62 v62.0.0
github.com/grafana/clireadme v0.1.0
github.com/itchyny/gojq v0.12.16
github.com/spf13/cobra v1.8.1
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/term v0.23.0
Expand All @@ -21,7 +20,6 @@ require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/henvic/httpretty v0.0.6 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/itchyny/timefmt-go v0.1.6 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ github.com/henvic/httpretty v0.0.6 h1:JdzGzKZBajBfnvlMALXXMVQWxWMF/ofTy8C3/OSUTx
github.com/henvic/httpretty v0.0.6/go.mod h1:X38wLjWXHkXT7r2+uK8LjCMne9rsuNaBLJ+5cU2/Pmo=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/itchyny/gojq v0.12.16 h1:yLfgLxhIr/6sJNVmYfQjTIv0jGctu6/DgDoivmxTr7g=
github.com/itchyny/gojq v0.12.16/go.mod h1:6abHbdC2uB9ogMS38XsErnfqJ94UlngIJGlRAIj4jTM=
github.com/itchyny/timefmt-go v0.1.6 h1:ia3s54iciXDdzWzwaVKXZPbiXzxxnv1SPGFfM/myJ5Q=
github.com/itchyny/timefmt-go v0.1.6/go.mod h1:RRDZYC5s9ErkjQvTvvU7keJjxUYzIISJGxm9/mAERQg=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down

0 comments on commit b902953

Please sign in to comment.