From cb41def70c4121ed2e1ca284844260a668783896 Mon Sep 17 00:00:00 2001 From: Aleksandar Stojanov Date: Tue, 30 Apr 2024 14:38:59 +0200 Subject: [PATCH 1/3] add indent cli option Signed-off-by: Aleksandar Stojanov --- .gitignore | 1 + .pre-commit-config.yaml | 4 +++- README.md | 2 +- pkg/cmd.go | 1 + pkg/cmd_test.go | 11 +++++++---- pkg/generator.go | 13 ++++++++++++- pkg/generator_test.go | 28 ++++++++++++++++++++++++++-- pkg/utils.go | 1 + plugin.yaml | 2 +- testdata/anchors.schema.json | 2 +- testdata/basic.schema.json | 2 +- testdata/full.schema.json | 2 +- testdata/meta.schema.json | 2 +- 13 files changed, 57 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index ef40f36..7113b5c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ # Test binary, built with `go test -c` *.test +schema # Output of the go coverage tool, specifically when used with LiteIDE *.out diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 936438f..c56ff6c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/losisin/helm-values-schema-json - rev: v1.2.4 + rev: v1.3.0 hooks: - id: helm-schema args: @@ -10,3 +10,5 @@ repos: - --output=values.schema.json # Draft version (4, 6, 7, 2019, or 2020) (default 2020) - --draft=2020 + # Indentation spaces (even number) + - --indent=4 diff --git a/README.md b/README.md index c20cf30..4f251bd 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ First [install pre-commit](https://pre-commit.com/#install) and then create or u ```yaml repos: - repo: https://github.com/losisin/helm-values-schema-json - rev: v1.2.4 + rev: v1.3.0 hooks: - id: helm-schema args: ["-input", "values.yaml"] diff --git a/pkg/cmd.go b/pkg/cmd.go index 40ba500..34ebaa6 100644 --- a/pkg/cmd.go +++ b/pkg/cmd.go @@ -16,6 +16,7 @@ func ParseFlags(progname string, args []string) (config *Config, output string, flags.Var(&conf.input, "input", "Multiple yaml files as inputs (comma-separated)") flags.StringVar(&conf.outputPath, "output", "values.schema.json", "Output file path") flags.IntVar(&conf.draft, "draft", 2020, "Draft version (4, 6, 7, 2019, or 2020)") + flags.IntVar(&conf.indent, "indent", 4, "Indentation spaces (even number)") err = flags.Parse(args) if err != nil { diff --git a/pkg/cmd_test.go b/pkg/cmd_test.go index cf6d8c0..2656887 100644 --- a/pkg/cmd_test.go +++ b/pkg/cmd_test.go @@ -13,13 +13,16 @@ func TestParseFlagsPass(t *testing.T) { conf Config }{ {[]string{"-input", "values.yaml"}, - Config{input: multiStringFlag{"values.yaml"}, outputPath: "values.schema.json", draft: 2020, args: []string{}}}, + Config{input: multiStringFlag{"values.yaml"}, outputPath: "values.schema.json", draft: 2020, indent: 4, args: []string{}}}, - {[]string{"-input", "values1.yaml values2.yaml"}, - Config{input: multiStringFlag{"values1.yaml values2.yaml"}, outputPath: "values.schema.json", draft: 2020, args: []string{}}}, + {[]string{"-input", "values1.yaml values2.yaml", "-indent", "2"}, + Config{input: multiStringFlag{"values1.yaml values2.yaml"}, outputPath: "values.schema.json", draft: 2020, indent: 2, args: []string{}}}, + + {[]string{"-input", "values.yaml", "-output", "my.schema.json", "-draft", "2019", "-indent", "2"}, + Config{input: multiStringFlag{"values.yaml"}, outputPath: "my.schema.json", draft: 2019, indent: 2, args: []string{}}}, {[]string{"-input", "values.yaml", "-output", "my.schema.json", "-draft", "2019"}, - Config{input: multiStringFlag{"values.yaml"}, outputPath: "my.schema.json", draft: 2019, args: []string{}}}, + Config{input: multiStringFlag{"values.yaml"}, outputPath: "my.schema.json", draft: 2019, indent: 4, args: []string{}}}, } for _, tt := range tests { diff --git a/pkg/generator.go b/pkg/generator.go index 31a5b99..2a9ff80 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "os" + "strings" "gopkg.in/yaml.v3" ) @@ -17,6 +18,15 @@ func GenerateJsonSchema(config *Config) error { return err } + // Determine the indentation string based on the number of spaces + if config.indent <= 0 { + return errors.New("indentation must be a positive number") + } + if config.indent%2 != 0 { + return errors.New("indentation must be an even number") + } + indentString := strings.Repeat(" ", config.indent) + // Initialize a Schema to hold the merged YAML data mergedSchema := &Schema{} @@ -70,10 +80,11 @@ func GenerateJsonSchema(config *Config) error { jsonSchemaMap["$schema"] = schemaURL // Include the schema draft version // If validation is successful, marshal the schema and save to the file - jsonBytes, err := json.MarshalIndent(jsonSchemaMap, "", " ") + jsonBytes, err := json.MarshalIndent(jsonSchemaMap, "", indentString) if err != nil { return err } + jsonBytes = append(jsonBytes, '\n') // Write the JSON schema to the output file outputPath := config.outputPath diff --git a/pkg/generator_test.go b/pkg/generator_test.go index eb244b9..d0733d3 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -17,6 +17,7 @@ func TestGenerateJsonSchema(t *testing.T) { }, outputPath: "../testdata/output.json", draft: 2020, + indent: 4, } err := GenerateJsonSchema(config) @@ -55,11 +56,32 @@ func TestGenerateJsonSchema_Errors(t *testing.T) { }, expectedErr: errors.New("invalid draft version"), }, + { + name: "Negative indentation number", + config: &Config{ + input: []string{"../testdata/basic.yaml"}, + draft: 2020, + outputPath: "testdata/failure/output_readonly_schema.json", + indent: 0, + }, + expectedErr: errors.New("indentation must be a positive number"), + }, + { + name: "Odd indentation number", + config: &Config{ + input: []string{"../testdata/basic.yaml"}, + draft: 2020, + outputPath: "testdata/failure/output_readonly_schema.json", + indent: 1, + }, + expectedErr: errors.New("indentation must be an even number"), + }, { name: "Missing file", config: &Config{ - input: []string{"missing.yaml"}, - draft: 2020, + input: []string{"missing.yaml"}, + draft: 2020, + indent: 4, }, expectedErr: errors.New("error reading YAML file(s)"), }, @@ -69,6 +91,7 @@ func TestGenerateJsonSchema_Errors(t *testing.T) { input: []string{"../testdata/fail"}, outputPath: "testdata/failure/output_readonly_schema.json", draft: 2020, + indent: 4, }, expectedErr: errors.New("error unmarshaling YAML"), }, @@ -78,6 +101,7 @@ func TestGenerateJsonSchema_Errors(t *testing.T) { input: []string{"../testdata/basic.yaml"}, outputPath: "testdata/failure/output_readonly_schema.json", draft: 2020, + indent: 4, }, expectedErr: errors.New("error writing schema to file"), }, diff --git a/pkg/utils.go b/pkg/utils.go index d4341f5..7767528 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -7,6 +7,7 @@ type Config struct { input multiStringFlag outputPath string draft int + indent int args []string } diff --git a/plugin.yaml b/plugin.yaml index 350fd1d..881c6a0 100644 --- a/plugin.yaml +++ b/plugin.yaml @@ -1,5 +1,5 @@ name: "schema" -version: "1.2.4" +version: "1.3.0" usage: "generate values.schema.json from values yaml" description: "Helm plugin for generating values.schema.json from multiple values files." ignoreFlags: false diff --git a/testdata/anchors.schema.json b/testdata/anchors.schema.json index f5313be..3282a74 100644 --- a/testdata/anchors.schema.json +++ b/testdata/anchors.schema.json @@ -21,4 +21,4 @@ } }, "type": "object" -} \ No newline at end of file +} diff --git a/testdata/basic.schema.json b/testdata/basic.schema.json index a29d34f..d09075d 100644 --- a/testdata/basic.schema.json +++ b/testdata/basic.schema.json @@ -58,4 +58,4 @@ } }, "type": "object" -} \ No newline at end of file +} diff --git a/testdata/full.schema.json b/testdata/full.schema.json index 439a59d..9b2b812 100644 --- a/testdata/full.schema.json +++ b/testdata/full.schema.json @@ -168,4 +168,4 @@ "nameOverride" ], "type": "object" -} \ No newline at end of file +} diff --git a/testdata/meta.schema.json b/testdata/meta.schema.json index 93e0cd6..f392785 100644 --- a/testdata/meta.schema.json +++ b/testdata/meta.schema.json @@ -51,4 +51,4 @@ } }, "type": "object" -} \ No newline at end of file +} From 613cb3aaa396ac982f1e51fd84828278bfc5c051 Mon Sep 17 00:00:00 2001 From: Aleksandar Stojanov Date: Tue, 30 Apr 2024 15:46:26 +0200 Subject: [PATCH 2/3] update readme Signed-off-by: Aleksandar Stojanov --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 4f251bd..c22f77e 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,8 @@ $ helm schema -help usage: helm schema [-input STR] [-draft INT] [-output STR] -draft int Draft version (4, 6, 7, 2019, or 2020) (default 2020) + -indent int + Indentation spaces (even number) (default 4) -input value Multiple yamlFiles as inputs (comma-separated) -output string From 2a02a1639912eb60cd80c504471272501515b5ed Mon Sep 17 00:00:00 2001 From: Aleksandar Stojanov Date: Tue, 30 Apr 2024 15:49:53 +0200 Subject: [PATCH 3/3] add step to check code in workflow Signed-off-by: Aleksandar Stojanov --- .github/workflows/ci.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index de00249..9ee8afb 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -31,6 +31,8 @@ jobs: with: args: --timeout 5m0s skip-cache: true + - name: Check code + run: make check - name: Run tests run: make test-all - name: Install plugin