Skip to content

Commit

Permalink
Merge pull request #82 from grafana/81-generate-catalog-and-registry-…
Browse files Browse the repository at this point in the history
…at-the-same-time

feat: generate catalog and registry at the same time
  • Loading branch information
szkiba authored Sep 27, 2024
2 parents 87d35d8 + 18dbafe commit a53dd8b
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 30 deletions.
6 changes: 2 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ go-jsonschema --capitalization URL --capitalization OSS -p k6registry --only-mod
The example registry can be found in [example.yaml] file, the documentation ([registry.md], [README.md]) must be updated after modification.

```bash
go run ./cmd/k6registry --lint -o docs/example.json docs/example.yaml
go run ./cmd/k6registry --lint --catalog -o docs/example-catalog.json docs/example.yaml
go run ./cmd/k6registry --lint -o docs/example.json --catalog docs/example-catalog.json docs/example.yaml
go run ./cmd/k6registry --lint -q --api docs/example-api docs/example.yaml
tree -n --noreport --filesfirst -o docs/example-api.txt docs/example-api
mdcode update docs/registry.md
Expand All @@ -69,8 +68,7 @@ mdcode update README.md

```bash
export ORIGIN=https://registry.k6.io/registry.json
go run ./cmd/k6registry --lint -o docs/custom.json --origin $ORIGIN docs/custom.yaml
go run ./cmd/k6registry --lint --catalog -o docs/custom-catalog.json --origin $ORIGIN docs/custom.yaml
go run ./cmd/k6registry --lint --catalog docs/custom-catalog.json -o docs/custom.json --origin $ORIGIN docs/custom.yaml
```

## readme - Update README.md
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ quiet | 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
catalog| no | `false` | generate catalog instead of registry
catalog| no | | generate catalog to the specified file
ref | no | | reference output URL for change detection
origin | no | | external registry URL for default values

Expand Down Expand Up @@ -535,18 +535,18 @@ k6registry [flags] [source-file]
### Flags
```
-o, --out string write output to file instead of stdout
--api string write outputs to directory instead of stdout
--origin string external registry URL for default values
--test strings test api path(s) (example: /registry.json,/catalog.json)
-q, --quiet no output, only validation
--loose skip JSON schema validation
--lint enable built-in linter
-c, --compact compact instead of pretty-printed output
--catalog generate catalog instead of registry
-v, --verbose verbose logging
-V, --version print version
-h, --help help for k6registry
-o, --out string write output to file instead of stdout
--api string write outputs to directory instead of stdout
--origin string external registry URL for default values
--test strings test api path(s) (example: /registry.json,/catalog.json)
-q, --quiet no output, only validation
--loose skip JSON schema validation
--lint enable built-in linter
-c, --compact compact instead of pretty-printed output
--catalog string generate catalog to the specified file
-v, --verbose verbose logging
-V, --version print version
-h, --help help for k6registry
```
<!-- #endregion cli -->
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ inputs:
required: false

catalog:
description: generate catalog instead of registry
description: generate catalog to the specified file
required: false

origin:
Expand Down
42 changes: 36 additions & 6 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/fs"
"log/slog"
"os"
"path/filepath"

"github.com/grafana/k6registry"
"github.com/spf13/cobra"
Expand All @@ -20,7 +21,7 @@ var help string
type options struct {
out string
compact bool
catalog bool
catalog string
quiet bool
verbose bool
loose bool
Expand Down Expand Up @@ -77,9 +78,11 @@ func New(levelVar *slog.LevelVar) (*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.BoolVar(&opts.catalog, "catalog", false, "generate catalog instead of registry")
flags.StringVar(&opts.catalog, "catalog", "", "generate catalog to the specified file")
flags.BoolVarP(&opts.verbose, "verbose", "v", false, "verbose logging")
root.MarkFlagsMutuallyExclusive("compact", "quiet")
root.MarkFlagsMutuallyExclusive("api", "out")
root.MarkFlagsMutuallyExclusive("api", "catalog")

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

Expand Down Expand Up @@ -151,25 +154,52 @@ func run(ctx context.Context, args []string, opts *options) (result error) {
return testAPI(opts.test, opts.api)
}

if len(opts.catalog) > 0 {
if err := writeCatalog(registry, opts.catalog, opts.compact); err != nil {
return err
}
}

if opts.quiet {
return nil
}

return writeOutput(registry, output, opts)
return writeOutput(registry, output, opts.compact, false)
}

//nolint:forbidigo
func writeCatalog(registry k6registry.Registry, filename string, compact bool) (result error) {
file, err := os.Create(filepath.Clean(filename))
if err != nil {
return err
}

defer func() {
err := file.Close()
if result == nil && err != nil {
result = err
}
}()

if err := writeOutput(registry, file, compact, true); err != nil {
result = err
}

return
}

func writeOutput(registry k6registry.Registry, output io.Writer, opts *options) error {
func writeOutput(registry k6registry.Registry, output io.Writer, compact, catalog bool) error {
encoder := json.NewEncoder(output)

if !opts.compact {
if !compact {
encoder.SetIndent("", " ")
}

encoder.SetEscapeHTML(false)

var source interface{} = registry

if opts.catalog {
if catalog {
source = k6registry.RegistryToCatalog(registry)
}

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

if getenv("INPUT_CATALOG", "false") == "true" {
args = append(args, "--catalog")
if catalog := getenv("INPUT_CATALOG", ""); len(catalog) != 0 {
args = append(args, "--catalog", catalog)
}

if api := getenv("INPUT_API", ""); len(api) != 0 {
Expand Down
12 changes: 10 additions & 2 deletions docs/custom-catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
"categories": [
"data"
],
"compliance": {
"grade": "A",
"level": 100
},
"constraints": ">=v0.4.0",
"description": "Generate random fake data",
"imports": [
Expand All @@ -88,7 +92,7 @@
"name": "xk6-faker",
"owner": "grafana",
"public": true,
"stars": 54,
"stars": 55,
"timestamp": 1725533453,
"topics": [
"xk6"
Expand Down Expand Up @@ -132,6 +136,10 @@
"categories": [
"data"
],
"compliance": {
"grade": "A",
"level": 100
},
"description": "Load-test SQL Servers",
"imports": [
"k6/x/sql"
Expand All @@ -148,7 +156,7 @@
"name": "xk6-sql",
"owner": "grafana",
"public": true,
"stars": 109,
"stars": 110,
"timestamp": 1725979901,
"topics": [
"k6",
Expand Down
12 changes: 10 additions & 2 deletions docs/custom.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
"categories": [
"data"
],
"compliance": {
"grade": "A",
"level": 100
},
"constraints": ">=v0.4.0",
"description": "Generate random fake data",
"imports": [
Expand All @@ -75,7 +79,7 @@
"name": "xk6-faker",
"owner": "grafana",
"public": true,
"stars": 54,
"stars": 55,
"timestamp": 1725533453,
"topics": [
"xk6"
Expand All @@ -91,6 +95,10 @@
"categories": [
"data"
],
"compliance": {
"grade": "A",
"level": 100
},
"description": "Load-test SQL Servers",
"imports": [
"k6/x/sql"
Expand All @@ -107,7 +115,7 @@
"name": "xk6-sql",
"owner": "grafana",
"public": true,
"stars": 109,
"stars": 110,
"timestamp": 1725979901,
"topics": [
"k6",
Expand Down
9 changes: 9 additions & 0 deletions releases/v0.1.29.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
k6registry `v0.1.29` is here 🎉!

This is an internal maintenance release.

**Generate catalog and registry at the same time**

During customization, it is often necessary to generate both catalog and registry. These two outputs can now be generated simultaneously, with a single `k6registry` run.

The `--catalog` flag now specifies the location of the generated catalog.

0 comments on commit a53dd8b

Please sign in to comment.