cmd

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DeleteCmd = cobra.Command{
	Use:   "delete <table> <key>",
	Short: "Delete data from Regatta store",
	Long: "Deletes data from Regatta store using DeleteRange query as defined in API (https://engineering.jamf.com/regatta/api/#deleterange).\n" +
		"You can delete single item in Regatta by providing item's key.\n" +
		"Or you can delete items with given prefix, by providing the given prefix and adding the asterisk (*) to the prefix.\n" +
		"Or you can delete all items in the table by specifying only asterisk (*).\n" +
		"When key or prefix is provided, it needs to be valid UTF-8 string.",
	Example: "regatta-client delete table key\n" +
		"regatta-client delete table 'prefix*'\n" +
		"regatta-client delete table '*'",
	Args: cobra.ExactArgs(2),
	ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		var comps []string
		if len(args) == 0 {
			comps = cobra.AppendActiveHelp(comps, "You must specify a Regatta table name")
		} else if len(args) == 1 {
			comps = cobra.AppendActiveHelp(comps, "You must specify a key or prefix to delete")
		}
		return comps, cobra.ShellCompDirectiveNoFileComp
	},
	PreRunE: connect,
	Run: func(cmd *cobra.Command, args []string) {
		ctx, cancel := context.WithTimeout(cmd.Context(), timeout)
		defer cancel()
		key, opts := keyAndOptsForDelete(args)
		resp, err := regatta.Table(args[0]).Delete(ctx, key, opts...)
		if err != nil {
			handleRegattaError(cmd, err)
			return
		}
		count := color.New(color.FgBlue).Sprint(resp.Deleted)
		cmd.Println(count)
	},
}

DeleteCmd is a subcommand used for deleting records in a table.

View Source
var ManCmd = cobra.Command{
	Use:     "man",
	Short:   "Generates man pages",
	Example: "regatta-client man .",
	Args:    cobra.MatchAll(cobra.ExactArgs(1)),
	RunE: func(cmd *cobra.Command, args []string) error {
		return doc.GenManTree(&RootCmd, nil, args[0])
	},
}

ManCmd is a subcommand used for printing man pages.

View Source
var PutCmd = cobra.Command{
	Use:     "put <table> <key> <value>",
	Short:   "Put data into Regatta store",
	Long:    "Put data into Regatta store using Put query as defined in API (https://engineering.jamf.com/regatta/api/#put).",
	Example: "regatta-client put table key value",
	Args:    cobra.ExactArgs(3),
	ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		var comps []string
		if len(args) == 0 {
			comps = cobra.AppendActiveHelp(comps, "You must specify a Regatta table name")
		} else if len(args) == 1 {
			comps = cobra.AppendActiveHelp(comps, "You must specify a key for the value to be inserted")
		} else if len(args) == 2 {
			comps = cobra.AppendActiveHelp(comps, "You must specify a value to be inserted")
		}
		return comps, cobra.ShellCompDirectiveNoFileComp
	},
	PreRunE: connect,
	Run: func(cmd *cobra.Command, args []string) {
		ctx, cancel := context.WithTimeout(cmd.Context(), timeout)
		defer cancel()
		key, value, err := keyAndValueForPut(args)
		if err != nil {
			cmd.PrintErrln("There was an error while decoding parameters.", err)
			return
		}
		_, err = regatta.Table(args[0]).Put(ctx, key, value)
		if err != nil {
			handleRegattaError(cmd, err)
		}
	},
}

PutCmd is a subcommand used for creating/updating records in a table.

View Source
var RangeCmd = cobra.Command{
	Use:   "range <table> [key]",
	Short: "Retrieve data from Regatta store",
	Long: "Retrieves data from Regatta store using Range query as defined in API (https://engineering.jamf.com/regatta/api/#range).\n" +
		"You can either retrieve all items from the Regatta by providing no key.\n" +
		"Or you can query for a single item in Regatta by providing item's key.\n" +
		"Or you can query for all items with given prefix, by providing the given prefix and adding the asterisk (*) to the prefix.\n" +
		"When key or prefix is provided, it needs to be valid UTF-8 string.",
	Example: "regatta-client range table\n" +
		"regatta-client range table key\n" +
		"regatta-client range table 'prefix*'",
	Args: cobra.MatchAll(cobra.MinimumNArgs(1), cobra.MaximumNArgs(2)),
	ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		var comps []string
		if len(args) == 0 {
			comps = cobra.AppendActiveHelp(comps, "You must specify a Regatta table name")
		} else if len(args) == 1 {
			comps = cobra.AppendActiveHelp(comps, "You can provide a key or prefix to search for. If not provided all items from the table is returned.")
		}
		return comps, cobra.ShellCompDirectiveNoFileComp
	},
	PreRunE: connect,
	Run: func(cmd *cobra.Command, args []string) {
		ctx, cancel := context.WithTimeout(cmd.Context(), timeout)
		defer cancel()
		key, opts := keyAndOptsForRange(args)

		var resps []*client.GetResponse
		bufferAll := rangeOutput == jsonFormat
		iterator, err := regatta.Table(args[0]).Iterate(ctx, key, opts...)
		if err != nil {
			handleRegattaError(cmd, err)
			return
		}

		iterator(func(resp *client.GetResponse, err error) bool {
			if err != nil {
				handleRegattaError(cmd, err)
				return false
			}
			if bufferAll {

				resps = append(resps, resp)
			} else {

				switch rangeOutput {
				case plainFormat:
					plainPrint(cmd, resp)
				case jsonLineFormat:
					jsonLinePrint(cmd, resp)
				}
			}
			return true
		})

		if bufferAll {
			switch rangeOutput {
			case jsonFormat:
				jsonPrint(cmd, resps)
			}
		}
	},
}

RangeCmd is a subcommand used for retrieving records from a table.

View Source
var RootCmd = cobra.Command{
	Use:   "regatta-client",
	Short: "Client for Regatta store",
	Long: "Command-line tool wrapping API calls to Regatta (https://engineering.jamf.com/regatta/).\n" +
		"Simplifies querying for data in Regatta store and other operations.",
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
		color.NoColor = noColor
		return nil
	},
	Version:      Version,
	SilenceUsage: true,
}

RootCmd is a root command for all the subcommands of regatta-client.

View Source
var TableCmd = cobra.Command{
	Use:     "table",
	Short:   "Print available tables",
	Long:    "Print available tables using Status API (https://engineering.jamf.com/regatta/api/#status).",
	Example: "regatta-client table",
	PreRunE: connect,
	Run: func(cmd *cobra.Command, args []string) {
		ctx, cancel := context.WithTimeout(cmd.Context(), timeout)
		defer cancel()

		resp, err := regatta.Status(ctx, endpointOption)
		if err != nil {
			handleRegattaError(cmd, err)
			return
		}

		var sortedTables []string
		for tableName := range resp.Tables {
			sortedTables = append(sortedTables, tableName)
		}

		sort.Strings(sortedTables)

		for _, tableName := range sortedTables {
			coloredTableName := color.New(color.FgGreen).Sprint(tableName)
			cmd.Println(coloredTableName)
		}
	},
}

TableCmd is a subcommand used for printing tables.

View Source
var Version string

Version is set during release of project.

View Source
var VersionCmd = cobra.Command{
	Use:     "version",
	Short:   "Get current version of regatta-client and a Regatta server",
	Long:    "Get current version of regatta-client and a Regatta server using Status API (https://engineering.jamf.com/regatta/api/#status).",
	Example: "regatta-client version",
	Run: func(cmd *cobra.Command, args []string) {
		printClientVersion(cmd, RootCmd.Version)
		err := connect(cmd, args)
		if err != nil {
			printServerVersion(cmd, unknownVersion, color.FgRed)
			cmd.PrintErrln(err)
			return
		}
		ctx, cancel := context.WithTimeout(cmd.Context(), timeout)
		defer cancel()
		resp, err := regatta.Status(ctx, endpointOption)
		if err != nil {
			printServerVersion(cmd, unknownVersion, color.FgRed)
			cmd.PrintErrln(err)
			return
		}
		printServerVersion(cmd, resp.Version, color.FgGreen)
	},
}

VersionCmd is a subcommand used for printing client and server version.

Functions

func Execute

func Execute()

Execute executes root command of regatta-client.

Types

This section is empty.

Jump to

Keyboard shortcuts

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