Help us shape the future of Go! Take the 2026 Go Developer Survey

clijq

package
v1.3.97 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package clijq cmd/skywire-cli/commands/jq/jq.go c4-vis-cli

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "jq <filter> [file...]",
	Short: "jq-like JSON processor (gojq)",
	Long: `Process JSON using jq filter syntax (powered by gojq).

Reads JSON from stdin (or the given files) and applies the filter, the same way
the standard jq(1) does. Canonically invoked as ` + "`skywire cli util jq`" + `.

  echo '{"a":1,"b":2}' | skywire cli util jq '.a'
  skywire cli util jq -r '.items[].name' data.json

To filter the output of another skywire command, use the built-in --jq global
flag instead of piping through this:  skywire cli pv --stats --jq '.count'.`,
	Hidden: true,
	Args:   cobra.MinimumNArgs(1),
	Run: func(_ *cobra.Command, args []string) {
		filter := args[0]
		files := args[1:]

		query, err := gojq.Parse(filter)
		if err != nil {
			fmt.Fprintf(os.Stderr, "jq: invalid query: %v\n", err)
			os.Exit(1)
		}

		code, err := gojq.Compile(query)
		if err != nil {
			fmt.Fprintf(os.Stderr, "jq: compile error: %v\n", err)
			os.Exit(1)
		}

		// Get inputs
		var inputs []any
		if nullInput {
			inputs = []any{nil}
		} else if len(files) > 0 {
			for _, file := range files {
				fileInputs, err := readJSONFile(file)
				if err != nil {
					fmt.Fprintf(os.Stderr, "jq: %v\n", err)
					os.Exit(1)
				}
				inputs = append(inputs, fileInputs...)
			}
		} else {
			stdinInputs, err := readJSONFromReader(os.Stdin)
			if err != nil {
				fmt.Fprintf(os.Stderr, "jq: %v\n", err)
				os.Exit(1)
			}
			inputs = stdinInputs
		}

		if slurpInput && len(inputs) > 0 {
			inputs = []any{inputs}
		}

		hasError := false
		for _, input := range inputs {
			iter := code.Run(input)
			for {
				v, ok := iter.Next()
				if !ok {
					break
				}
				if err, ok := v.(error); ok {
					if haltErr, ok := err.(*gojq.HaltError); ok && haltErr.Value() == nil {
						break
					}
					fmt.Fprintf(os.Stderr, "jq: %v\n", err)
					hasError = true
					continue
				}
				if err := outputValue(v); err != nil {
					fmt.Fprintf(os.Stderr, "jq: %v\n", err)
					hasError = true
				}
			}
		}

		if hasError {
			os.Exit(1)
		}
	},
}

RootCmd is the jq command. It is exposed to users as `skywire cli util jq` (the util parent unhides it); it stays Hidden as a bare top-level command so it doesn't clutter the root help. To filter the JSON output of ANY skywire command, prefer the built-in `--jq` global flag (e.g. `cli visor info --jq .overview`) — this standalone processor is for piping arbitrary JSON.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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