root

package module
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2025 License: Apache-2.0 Imports: 13 Imported by: 3

Documentation

Overview

Example
package main

import (
	"os"
	"sync"

	ver "github.com/jasonhancock/cobra-version"
	cr "github.com/jasonhancock/cobraflags/root"
	"github.com/spf13/cobra"
)

// These variables are populated by goreleaser when the binary is built.
var (
	version = "dev"
	commit  = "none"
	date    = "unknown"
)

func main() {
	r := cr.New(
		"myapp",
		cr.WithVersion(ver.New(version, commit, date)),
		cr.LoggerEnabled(true),
	)

	r.AddCommand(NewCmd(r))

	r.Execute()
}

func NewCmd(root *cr.Command) *cobra.Command {
	return &cobra.Command{
		Use:          "server",
		Short:        "Starts the server.",
		SilenceUsage: true,
		Args:         cobra.NoArgs,
		RunE: func(cmd *cobra.Command, args []string) error {
			var wg sync.WaitGroup

			wg.Add(1)
			go func() {
				defer wg.Done()
				root.Logger(os.Stdout).Info("hello world")
				<-cmd.Context().Done()
			}()

			wg.Wait()

			return nil
		},
	}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenDocs added in v0.0.11

func GenDocs(r *Command) *cobra.Command

GenDocs generates markdown documentation from your command definitions into the specified output directory.

Types

type Command

type Command struct {
	Version *ver.Info
	// contains filtered or unexported fields
}

func New

func New(use string, opts ...Option) *Command

func (*Command) AddCommand

func (c *Command) AddCommand(cmds ...*cobra.Command)

func (*Command) Execute

func (c *Command) Execute()

func (*Command) LogLevelHandler added in v0.0.10

func (c *Command) LogLevelHandler() func(http.ResponseWriter, *http.Request)

LogLevelHandler returns an HTTP handler that is capable of changing the log level.

func (*Command) Logger added in v0.0.2

func (c *Command) Logger(dest io.Writer, opts ...LoggerOption) *logger.L

func (*Command) UserAgent added in v0.0.8

func (c *Command) UserAgent(cmd *cobra.Command) string

UserAgent formats and returns a string based on the binary name and command path. If the binary's name is "foo" and invoked the "bar baz" subcommand, the useragent would be "foo-bar-baz / <version>".

type ExitCoder added in v0.0.3

type ExitCoder interface {
	ExitCode() int
}

ExitCoder allows for customization of the exit code when an error is encountered.

type LoggerOption added in v0.0.6

type LoggerOption func(*loggerOptions)

LoggerOption is used to customize the logger.

func WithKeyVals added in v0.0.6

func WithKeyVals(keyvals ...any) LoggerOption

WithKeyVals adds key/value pairs to the logger.

func WithName added in v0.0.6

func WithName(name string) LoggerOption

WithName sets the logger name.

type Option

type Option func(*options)

Option is used to customize the command.

func LoggerEnabled added in v0.0.2

func LoggerEnabled(enabled bool) Option

LoggerEnabled will add peristent flags from cobra-logger to the root command and enable a helper method for constructing a logger.

func WithBaseCommand

func WithBaseCommand(cmd *cobra.Command) Option

WithBaseCommand allows you to completely swap out the root command for your own.

func WithCommand

func WithCommand(cmd ...*cobra.Command) Option

WithCommand adds commands.

func WithLong

func WithLong(str string) Option

WithLong sets the command's long description.

func WithShort

func WithShort(str string) Option

WithShort sets the command's short description.

func WithVersion

func WithVersion(info *ver.Info) Option

WithVersion passes in a vesion structure to use and wires up the version command.

Jump to

Keyboard shortcuts

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