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
},
}
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Command ¶
func (*Command) AddCommand ¶
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.
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
LoggerEnabled will add peristent flags from cobra-logger to the root command and enable a helper method for constructing a logger.
func WithBaseCommand ¶
WithBaseCommand allows you to completely swap out the root command for your own.
func WithVersion ¶
WithVersion passes in a vesion structure to use and wires up the version command.
Click to show internal directories.
Click to hide internal directories.