example

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package example provides an example cli sub-command.

Index

Constants

This section is empty.

Variables

View Source
var Command = &cobra.Command{
	Use:        "example",
	Aliases:    []string{},
	SuggestFor: nil,
	Short:      "The example's command short-description",
	Long:       "The example's command long-description -- value should be in full sentences, and can span multiple lines.",
	Example: strings.Join([]string{
		fmt.Sprintf("  %s", "# General command usage"),
		fmt.Sprintf("  %s", fmt.Sprintf("%s example --name \"test-value\"", constants.Name())),
		"",
		fmt.Sprintf("  %s", "# Extended usage demonstrating configuration of default(s)"),
		fmt.Sprintf("  %s", fmt.Sprintf("%s example --name \"test-value\" --output json", constants.Name())),
		"",
		fmt.Sprintf("  %s", "# Display help information and command usage"),
		fmt.Sprintf("  %s", fmt.Sprintf("%s example --help", constants.Name())),
	}, "\n"),
	ValidArgs:              nil,
	ValidArgsFunction:      nil,
	Args:                   nil,
	ArgAliases:             nil,
	BashCompletionFunction: "",
	Deprecated:             "",
	Annotations:            nil,
	Version:                "",
	PreRun:                 func(cmd *cobra.Command, args []string) {},
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx := cmd.Context()

		slog.Log(ctx, log.Debug, "Example Log Message", slog.Group("command",
			slog.String("name", cmd.Name()),
			slog.Group("flags",
				slog.String("name", name),
				slog.String("output", format.String()),
			),
		))

		var datum = map[string]string{
			"name":   name,
			"output": format.String(),
		}

		switch format {
		case output.JSON:
			buffer, e := marshalers.JSON(datum)
			if e != nil {
				return fmt.Errorf("unable to marshal structure to json: %w", e)
			}

			fmt.Printf("%s", string(buffer))
		case output.YAML:
			buffer, e := marshalers.YAML(datum)
			if e != nil {
				return fmt.Errorf("unable to marshal structure to yaml: %w", e)
			}

			fmt.Printf("%s", string(buffer))
		}

		return nil
	},
	PostRun:           nil,
	CompletionOptions: cobra.CompletionOptions{},
	TraverseChildren:  false,
	Hidden:            false,
	SilenceErrors:     true,
	SilenceUsage:      false,
}

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