Skip to content

Commit

Permalink
Merge pull request #26 from dadav/feat_ensure_reference
Browse files Browse the repository at this point in the history
feat: Add flag to insert schema reference to values file
  • Loading branch information
dadav authored Apr 28, 2024
2 parents 950d3a5 + 46f5faf commit efda25e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ The binary has the following options:

```sh
Flags:
-r, --add-schema-reference "add reference to schema in values.yaml if not found"
-c, --chart-search-root string "directory to search recursively within for charts (default ".")"
-x, --dont-strip-helm-docs-prefix "disable the removal of the helm-docs prefix (--)"
-d, --dry-run "don't actually create files just print to stdout passed"
Expand Down Expand Up @@ -146,6 +147,8 @@ You'll have to place this line at the top of your `values.yaml` (`$schema=<path-
foo: bar
```

You can use the `-r` flag to make sure this line exists.

> [!NOTE]
> You can also point to an online available schema, if you upload a version of yours and want other to be able to implement it.
>
Expand Down
2 changes: 2 additions & 0 deletions cmd/helm-schema/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func newCommand(run func(cmd *cobra.Command, args []string) error) (*cobra.Comma
BoolP("dont-strip-helm-docs-prefix", "x", false, "disable the removal of the helm-docs prefix (--)")
cmd.PersistentFlags().
BoolP("no-dependencies", "n", false, "don't analyze dependencies")
cmd.PersistentFlags().
BoolP("add-schema-reference", "r", false, "add reference to schema in values.yaml if not found")
cmd.PersistentFlags().StringP("log-level", "l", "info", logLevelUsage)
cmd.PersistentFlags().
StringSliceP("value-files", "f", []string{"values.yaml"}, "filenames to check for chart values")
Expand Down
17 changes: 16 additions & 1 deletion cmd/helm-schema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Result struct {
}

func worker(
dryRun, uncomment, keepFullComment, dontRemoveHelmDocsPrefix bool,
dryRun, uncomment, addSchemaReference, keepFullComment, dontRemoveHelmDocsPrefix bool,
valueFileNames []string,
skipAutoGenerationConfig *schema.SkipAutoGenerationConfig,
outFile string,
Expand Down Expand Up @@ -114,6 +114,19 @@ func worker(
continue
}

// Check if we need to add a schema reference
if addSchemaReference {
schemaRef := `# yaml-language-server: $schema=values.schema.json`
if !strings.Contains(string(content), schemaRef) {
err = util.InsertLineToFile(schemaRef, valuesPath)
if err != nil {
result.Errors = append(result.Errors, err)
results <- result
continue
}
}
}

// Optional preprocessing
if uncomment {
// Remove comments from valid yaml
Expand Down Expand Up @@ -147,6 +160,7 @@ func exec(cmd *cobra.Command, _ []string) error {
chartSearchRoot := viper.GetString("chart-search-root")
dryRun := viper.GetBool("dry-run")
noDeps := viper.GetBool("no-dependencies")
addSchemaReference := viper.GetBool("add-schema-reference")
keepFullComment := viper.GetBool("keep-full-comment")
uncomment := viper.GetBool("uncomment")
outFile := viper.GetString("output-file")
Expand Down Expand Up @@ -188,6 +202,7 @@ func exec(cmd *cobra.Command, _ []string) error {
worker(
dryRun,
uncomment,
addSchemaReference,
keepFullComment,
dontRemoveHelmDocsPrefix,
valueFileNames,
Expand Down
22 changes: 22 additions & 0 deletions pkg/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"bufio"
"io"
"os"
"regexp"
"strings"

Expand Down Expand Up @@ -30,6 +31,27 @@ func appendAndNLStr(to *[]byte, from string) {
*to = append(*to, '\n')
}

// InsertLinetoFile inserts a line to the beginning of a file
func InsertLineToFile(line, file string) error {
fileInfo, err := os.Stat(file)
if err != nil {
return err
}
perm := fileInfo.Mode().Perm()
content, err := os.ReadFile(file)
if err != nil {
return err
}

eol := "\n"
if len(content) >= 2 && content[len(content)-2] == '\r' && content[len(content)-1] == '\n' {
eol = "\r\n"
}

newContent := line + eol + string(content)
return os.WriteFile(file, []byte(newContent), perm)
}

// RemoveCommentsFromYaml tries to remove comments if they contain valid yaml
func RemoveCommentsFromYaml(reader io.Reader) ([]byte, error) {
result := make([]byte, 0)
Expand Down

0 comments on commit efda25e

Please sign in to comment.