...

Package jsonnetx

import "github.com/ory/x/jsonnetx"
Overview
Index

Overview ▾

Constants

const GlobHelp = `Glob patterns supports the following special terms in the patterns:

    Special Terms | Meaning
    ------------- | -------
    '*'           | matches any sequence of non-path-separators
    '**'          | matches any sequence of characters, including path separators
    '?'           | matches any single non-path-separator character
    '[class]'     | matches any single non-path-separator character against a class of characters ([see below](#character-classes))
    '{alt1,...}'  | matches a sequence of characters if one of the comma-separated alternatives matches

    Any character with a special meaning can be escaped with a backslash ('\').

    #### Character Classes

    Character classes support the following:

    Class      | Meaning
    ---------- | -------
    '[abc]'    | matches any single character within the set
    '[a-z]'    | matches any single character in the range
    '[^class]' | matches any single character which does *not* match the class
`

Variables

FormatCommand represents the format command

var FormatCommand = &cobra.Command{
    Use: "format path/to/files/*.jsonnet [more/files.jsonnet, [supports/**/{foo,bar}.jsonnet]]",
    Long: `Formats JSONNet files using the official JSONNet formatter.

Use -w or --write to write output back to files instead of stdout.

` + GlobHelp,
    Args: cobra.MinimumNArgs(1),
    Run: func(cmd *cobra.Command, args []string) {
        verbose := flagx.MustGetBool(cmd, "verbose")
        for _, pattern := range args {
            files, err := doublestar.Glob(pattern)
            cmdx.Must(err, `Glob pattern "%s" is not valid: %s`, pattern, err)

            shouldWrite := flagx.MustGetBool(cmd, "write")
            for _, file := range files {
                if fi, err := os.Stat(file); err != nil {
                    cmdx.Must(err, "Unable to stat file %s: %s", file, err)
                } else if fi.IsDir() {
                    continue
                }

                if verbose {
                    fmt.Printf("Processing file: %s\n", file)
                }

                content, err := ioutil.ReadFile(file)
                cmdx.Must(err, `Unable to read file "%s" because: %s`, file, err)

                output, err := formatter.Format(file, string(content), formatter.DefaultOptions())
                cmdx.Must(err, `JSONNet file "%s" could not be formatted: %s`, file, err)

                if shouldWrite {
                    err := ioutil.WriteFile(file, []byte(output), 0644)
                    cmdx.Must(err, `Could not write to file "%s" because: %s`, file, err)
                } else {
                    fmt.Println(output)
                }
            }
        }
    },
}

LintCommand represents the lint command

var LintCommand = &cobra.Command{
    Use: "lint path/to/files/*.jsonnet [more/files.jsonnet, [supports/**/{foo,bar}.jsonnet]]",
    Long: `Lints JSONNet files using the official JSONNet linter and exits with a status code of 1 when issues are detected.

` + GlobHelp,
    Args: cobra.MinimumNArgs(1),
    Run: func(cmd *cobra.Command, args []string) {
        verbose := flagx.MustGetBool(cmd, "verbose")
        for _, pattern := range args {
            files, err := doublestar.Glob(pattern)
            cmdx.Must(err, `Glob path "%s" is not valid: %s`, pattern, err)

            for _, file := range files {
                if fi, err := os.Stat(file); err != nil {
                    cmdx.Must(err, "Unable to stat file %s: %s", file, err)
                } else if fi.IsDir() {
                    continue
                }

                if verbose {
                    fmt.Printf("Processing file: %s\n", file)
                }

                content, err := ioutil.ReadFile(file)
                cmdx.Must(err, `Unable to read file "%s" because: %s`, file, err)

                node, err := jsonnet.SnippetToAST(file, string(content))
                cmdx.Must(err, `Unable to parse JSONNet source "%s" because: %s`, file, err)

                ew := &linter.ErrorWriter{Writer: os.Stderr}
                linter.Lint(node, ew)
                if ew.ErrorsFound {
                    _, _ = fmt.Fprintf(os.Stderr, "Linter found issues.")
                    os.Exit(1)
                }
            }
        }
    },
}

RootCommand represents the jsonnet command

var RootCommand = &cobra.Command{
    Use:   "jsonnet",
    Short: "Helpers for linting and formatting JSONNet code",
}

func RegisterCommandRecursive

func RegisterCommandRecursive(parent *cobra.Command)

RegisterCommandRecursive adds all jsonnet helpers to the RootCommand