From 18dbafe4157b762a25ece4614988afe1bea3edb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20SZKIBA?= Date: Fri, 27 Sep 2024 15:32:41 +0200 Subject: [PATCH] feat: generate catalog and registry at the same time --- CONTRIBUTING.md | 6 ++---- README.md | 26 ++++++++++++------------- action.yml | 2 +- cmd/cmd.go | 42 ++++++++++++++++++++++++++++++++++------ cmd/k6registry/main.go | 4 ++-- docs/custom-catalog.json | 12 ++++++++++-- docs/custom.json | 12 ++++++++++-- releases/v0.1.29.md | 9 +++++++++ 8 files changed, 83 insertions(+), 30 deletions(-) create mode 100644 releases/v0.1.29.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 640f3f4..2cdec47 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 @@ -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 diff --git a/README.md b/README.md index b6db8fb..3a80dce 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ``` diff --git a/action.yml b/action.yml index fd4d306..eb05bf9 100644 --- a/action.yml +++ b/action.yml @@ -40,7 +40,7 @@ inputs: required: false catalog: - description: generate catalog instead of registry + description: generate catalog to the specified file required: false origin: diff --git a/cmd/cmd.go b/cmd/cmd.go index b3c0670..cda42db 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -9,6 +9,7 @@ import ( "io/fs" "log/slog" "os" + "path/filepath" "github.com/grafana/k6registry" "github.com/spf13/cobra" @@ -20,7 +21,7 @@ var help string type options struct { out string compact bool - catalog bool + catalog string quiet bool verbose bool loose bool @@ -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") @@ -151,17 +154,44 @@ 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("", " ") } @@ -169,7 +199,7 @@ func writeOutput(registry k6registry.Registry, output io.Writer, opts *options) var source interface{} = registry - if opts.catalog { + if catalog { source = k6registry.RegistryToCatalog(registry) } diff --git a/cmd/k6registry/main.go b/cmd/k6registry/main.go index ba4ea17..7045f58 100644 --- a/cmd/k6registry/main.go +++ b/cmd/k6registry/main.go @@ -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 { diff --git a/docs/custom-catalog.json b/docs/custom-catalog.json index 6607b78..c7c26e6 100644 --- a/docs/custom-catalog.json +++ b/docs/custom-catalog.json @@ -71,6 +71,10 @@ "categories": [ "data" ], + "compliance": { + "grade": "A", + "level": 100 + }, "constraints": ">=v0.4.0", "description": "Generate random fake data", "imports": [ @@ -88,7 +92,7 @@ "name": "xk6-faker", "owner": "grafana", "public": true, - "stars": 54, + "stars": 55, "timestamp": 1725533453, "topics": [ "xk6" @@ -132,6 +136,10 @@ "categories": [ "data" ], + "compliance": { + "grade": "A", + "level": 100 + }, "description": "Load-test SQL Servers", "imports": [ "k6/x/sql" @@ -148,7 +156,7 @@ "name": "xk6-sql", "owner": "grafana", "public": true, - "stars": 109, + "stars": 110, "timestamp": 1725979901, "topics": [ "k6", diff --git a/docs/custom.json b/docs/custom.json index 77da5e7..b0a5b58 100644 --- a/docs/custom.json +++ b/docs/custom.json @@ -58,6 +58,10 @@ "categories": [ "data" ], + "compliance": { + "grade": "A", + "level": 100 + }, "constraints": ">=v0.4.0", "description": "Generate random fake data", "imports": [ @@ -75,7 +79,7 @@ "name": "xk6-faker", "owner": "grafana", "public": true, - "stars": 54, + "stars": 55, "timestamp": 1725533453, "topics": [ "xk6" @@ -91,6 +95,10 @@ "categories": [ "data" ], + "compliance": { + "grade": "A", + "level": 100 + }, "description": "Load-test SQL Servers", "imports": [ "k6/x/sql" @@ -107,7 +115,7 @@ "name": "xk6-sql", "owner": "grafana", "public": true, - "stars": 109, + "stars": 110, "timestamp": 1725979901, "topics": [ "k6", diff --git a/releases/v0.1.29.md b/releases/v0.1.29.md new file mode 100644 index 0000000..3304689 --- /dev/null +++ b/releases/v0.1.29.md @@ -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. \ No newline at end of file