jsonnetx

package
v0.0.238 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const GlobHelp = `` /* 940-byte string literal not displayed */

Variables

View Source
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)
				}
			}
		}
	},
}

FormatCommand represents the format command

View Source
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)
				}
			}
		}
	},
}

LintCommand represents the lint command

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

RootCommand represents the jsonnet command

Functions

func RegisterCommandRecursive

func RegisterCommandRecursive(parent *cobra.Command)

RegisterCommandRecursive adds all jsonnet helpers to the RootCommand

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL