From 097fad59752a8b9c0a1984660c4e284297ec798e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20SZKIBA?= Date: Fri, 16 Aug 2024 10:18:52 +0200 Subject: [PATCH] refactor: Remove the jq filter feature. --- README.md | 25 ++++++++-------------- action.yml | 15 +------------- cmd/cmd.go | 47 +++++++----------------------------------- cmd/help.md | 12 +++++------ cmd/jq.go | 35 ------------------------------- cmd/k6registry/main.go | 10 --------- go.mod | 2 -- go.sum | 4 ---- 8 files changed, 23 insertions(+), 127 deletions(-) delete mode 100644 cmd/jq.go diff --git a/README.md b/README.md index 266b86e..74c052f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. @@ -97,25 +94,23 @@ changed | `true` if the output has changed compared to `ref`, otherwise `false` ## 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] [file] +k6registry [flags] [file] ``` ### Flags @@ -126,8 +121,6 @@ k6registry [flags] [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 ``` diff --git a/action.yml b/action.yml index 81ae9c9..62411a0 100644 --- a/action.yml +++ b/action.yml @@ -1,5 +1,5 @@ name: k6registry -description: k6 extension registry processor +description: k6 extension registry generator author: Grafana Labs branding: @@ -7,11 +7,6 @@ branding: color: purple inputs: - filter: - description: jq compatible filter - required: false - default: . - in: description: input file name required: true @@ -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 diff --git a/cmd/cmd.go b/cmd/cmd.go index f8a1b4a..ac7df61 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -5,12 +5,9 @@ import ( "context" _ "embed" "encoding/json" - "fmt" - "io" "os" "github.com/spf13/cobra" - "gopkg.in/yaml.v3" ) //go:embed help.md @@ -19,8 +16,6 @@ var help string type options struct { out string compact bool - raw bool - yaml bool mute bool loose bool lint bool @@ -33,13 +28,13 @@ func New() (*cobra.Command, error) { legacy := false root := &cobra.Command{ - Use: "k6registry [flags] [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 { @@ -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") @@ -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 } @@ -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) @@ -156,5 +125,5 @@ func printer(output io.Writer, opts *options) func(interface{}) error { encoder.SetIndent("", " ") } - return encoder.Encode + return encoder.Encode(registry) } diff --git a/cmd/help.md b/cmd/help.md index bcfd6c3..82c1a75 100644 --- a/cmd/help.md +++ b/cmd/help.md @@ -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. diff --git a/cmd/jq.go b/cmd/jq.go deleted file mode 100644 index d1843e7..0000000 --- a/cmd/jq.go +++ /dev/null @@ -1,35 +0,0 @@ -package cmd - -import ( - "github.com/itchyny/gojq" -) - -func jq(input interface{}, filter string, output func(interface{}) error) error { - query, err := gojq.Parse(filter) - if err != nil { - return err - } - - iter := query.Run(input) - - for { - v, ok := iter.Next() - if !ok { - break - } - - if err, ok := v.(error); ok { - if err, ok := err.(*gojq.HaltError); ok && err.Value() == nil { //nolint:errorlint - break - } - - return err - } - - if err := output(v); err != nil { - return err - } - } - - return nil -} diff --git a/cmd/k6registry/main.go b/cmd/k6registry/main.go index b0bbe00..7674983 100644 --- a/cmd/k6registry/main.go +++ b/cmd/k6registry/main.go @@ -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 diff --git a/go.mod b/go.mod index cd3db0f..e0cbbb7 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 6a0675f..a93bf61 100644 --- a/go.sum +++ b/go.sum @@ -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=