Skip to content

Commit

Permalink
Merge pull request #62 from losisin/feature/indent-cli-option
Browse files Browse the repository at this point in the history
add indent cli option
  • Loading branch information
losisin committed Apr 30, 2024
2 parents f04bbe4 + 2a02a16 commit f87b874
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 7 additions & 4 deletions pkg/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strings"

"gopkg.in/yaml.v3"
)
Expand All @@ -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{}

Expand Down Expand Up @@ -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
Expand Down
28 changes: 26 additions & 2 deletions pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestGenerateJsonSchema(t *testing.T) {
},
outputPath: "../testdata/output.json",
draft: 2020,
indent: 4,
}

err := GenerateJsonSchema(config)
Expand Down Expand Up @@ -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)"),
},
Expand All @@ -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"),
},
Expand All @@ -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"),
},
Expand Down
1 change: 1 addition & 0 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Config struct {
input multiStringFlag
outputPath string
draft int
indent int

args []string
}
Expand Down
2 changes: 1 addition & 1 deletion plugin.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion testdata/anchors.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
}
},
"type": "object"
}
}
2 changes: 1 addition & 1 deletion testdata/basic.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@
}
},
"type": "object"
}
}
2 changes: 1 addition & 1 deletion testdata/full.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,4 @@
"nameOverride"
],
"type": "object"
}
}
2 changes: 1 addition & 1 deletion testdata/meta.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
}
},
"type": "object"
}
}

0 comments on commit f87b874

Please sign in to comment.