cmd

package
v0.0.0-...-e3dc4ac Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2021 License: Unlicense Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AppendCmd = &cobra.Command{
	Use:   "append <query> <newValue>",
	Short: "append value(s) to a list or a string",
	Args:  cobra.ExactArgs(2),

	RunE: func(cmd *cobra.Command, args []string) error {
		newValue := args[1]
		return performSet(args[0],
			func(list *ast.ListType) error {
				node, err := hclq.HclFromJSON(newValue)
				if err != nil {
					return err
				}
				list.List = append(list.List, node)
				return nil
			}, func(tok *token.Token) error {
				tok.Text = `"` + trimToken(tok.Text) + newValue + `"`
				tok.Type = token.STRING
				return nil
			})
	},
}

AppendCmd cobra command

View Source
var GetCmd = &cobra.Command{
	Use:   "get <query>",
	Short: "retrieve values matching <query>",
	Args:  cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		input, err := getInputReader()
		if err != nil {
			return err
		}
		doc, err := hclq.FromReader(input)
		if err != nil {
			return err
		}
		result, err := doc.Get(args[0])
		if err != nil {
			return err
		}
		output, err := getOutput(result, config.UseRawOutput)
		if err != nil {
			return err
		}
		if config.OutputFile != "" {
			file, err := os.Create(config.OutputFile)
			if err != nil {
				return err
			}
			defer file.Close()
			fmt.Fprintln(file, output)
		} else {
			fmt.Println(output)
		}
		return nil
	},
}

GetCmd command

View Source
var GetKeysCmd = &cobra.Command{
	Use:   "keys <query>",
	Short: "retrieve keys matching <query>",
	Args:  cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		input, err := getInputReader()
		if err != nil {
			return err
		}
		doc, err := hclq.FromReader(input)
		if err != nil {
			return err
		}
		result, err := doc.GetKeys(args[0])
		if err != nil {
			return err
		}
		output, err := getOutput(result, config.UseRawOutput)
		if err != nil {
			return err
		}
		if config.OutputFile != "" {
			file, err := os.Create(config.OutputFile)
			if err != nil {
				return err
			}
			defer file.Close()
			fmt.Fprintln(file, output)
		} else {
			fmt.Println(output)
		}
		return nil
	},
}

GetKeysCmd is like get but returns the key name or names instead of value.

View Source
var PrependCmd = &cobra.Command{
	Use:   "prepend <query> <newValue>",
	Short: "prepend value(s) to a list or a string",
	Args:  cobra.ExactArgs(2),

	RunE: func(cmd *cobra.Command, args []string) error {
		newValue := args[1]
		return performSet(args[0],
			func(list *ast.ListType) error {
				node, err := hclq.HclFromJSON(newValue)
				if err != nil {
					return err
				}
				list.List = append(node.(*ast.ListType).List, list.List...)
				return nil
			}, func(tok *token.Token) error {
				tok.Text = `"` + newValue + trimToken(tok.Text) + `"`
				tok.Type = token.STRING
				return nil
			})
	},
}

PrependCmd cobra command

View Source
var ReplaceCmd = &cobra.Command{
	Use:   "replace <query> <oldSequence> <newSequence>",
	Short: "find and replace a subsequence of items (or chars for strings)",
	Args:  cobra.ExactArgs(3),
	RunE: func(cmd *cobra.Command, args []string) error {
		return performSet(args[0],
			func(list *ast.ListType) error {

				panic("replace on lists is not implemented yet")
			}, func(tok *token.Token) error {
				tok.Text = `"` + strings.Replace(trimToken(tok.Text), args[1], args[2], config.ReplaceNTimes) + `"`
				tok.Type = token.STRING
				return nil
			})
	},
}

ReplaceCmd cobra command

View Source
var RootCmd = &cobra.Command{
	Use:     "hclq [flags] [command]",
	Version: version,

	SilenceUsage: true,
	Short:        "Query and modify HashiCorp Configuration Language files. Like sed for HCL.",
	Long: `hclq is a tool for manipulating the config files used by HashiCorp tools.

hclq uses a "breadcrumb" or "path" style query. Given the HCL:
    data "foo" "bar" {
        id = "100"
        other = [1, 2, 3]
    }

A query for 'data.foo.bar.id' would return 100. Arrays/lists must be matched
with the [] suffix, e.g. 'data.foo.bar.other[]' or 'data.foo.bar.other[1]'.

Match types:
    literal     Match a literal value.
    list[]      Match a list and retrieve all items.
    list[1]     Match a list and retrieve a specific item.
    /regex/     Match anything according to the specified regex.
    /regex/[]   Match a list according to the regex and retrieve all items.
    /regex/[1]  Match a list according to the regex and retrieve a specific item.
    *           Match anything.

Queries can return either single or multiple values. If a query matches e.g.
multiple arrays across multiple objects, a list of arrays will be returned.
If this query is used with a set command, ALL of those matching arrays will be
set.
`,
}

RootCmd command

View Source
var RootFlags = RootCmd.PersistentFlags()

RootFlags flags

View Source
var SetCmd = &cobra.Command{
	Use:   "set <query> <newValue>",
	Short: "set matching value(s), specify a string, number, or JSON object or array",
	Args:  cobra.ExactArgs(2),

	RunE: func(cmd *cobra.Command, args []string) error {
		newValue := args[1]
		return performSet(args[0],
			func(list *ast.ListType) error {
				listNode, err := hclq.HclListFromJSON(newValue)
				if err != nil {
					return err
				}
				list.List = listNode.List
				return nil
			}, func(tok *token.Token) error {
				tok.Text = `"` + newValue + `"`
				tok.Type = getTokenType(newValue)
				return nil
			})
	},
}

SetCmd cobra command

Functions

func Execute

func Execute()

Execute - cobra entry point

Types

This section is empty.

Jump to

Keyboard shortcuts

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