Skip to content

Commit

Permalink
Merge pull request #10 from rebuy-de/add-newcommand
Browse files Browse the repository at this point in the history
add cmdutil.NewCommand
  • Loading branch information
svenwltr authored Nov 6, 2018
2 parents e5c5d46 + 1910cbf commit 335b544
Showing 1 changed file with 48 additions and 39 deletions.
87 changes: 48 additions & 39 deletions cmdutil/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,54 @@ func NewRootCommand(app interface{}) *cobra.Command {
verbose bool
)

cmd := NewCommand(app)
cmd.Use = BuildName

cmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
logrus.SetLevel(logrus.InfoLevel)

if verbose {
logrus.SetLevel(logrus.DebugLevel)
}

if gelfAddress != "" {
labels := map[string]interface{}{
"facility": BuildName,
"version": BuildVersion,
"commit-sha": BuildHash,
}
hook := graylog.NewGraylogHook(gelfAddress, labels)
hook.Level = logrus.DebugLevel
logrus.AddHook(hook)
}

logrus.WithFields(logrus.Fields{
"Version": BuildVersion,
"Date": BuildDate,
"Commit": BuildHash,
}).Infof("%s started", BuildName)
}

cmd.PersistentPostRun = func(cmd *cobra.Command, args []string) {
logrus.Infof("%s stopped", BuildName)
}

cmd.PersistentFlags().BoolVarP(
&verbose, "verbose", "v", false,
`Show debug logs.`)
cmd.PersistentFlags().StringVar(
&gelfAddress, "gelf-address", "",
`Address to Graylog for logging (format: "ip:port").`)

cmd.AddCommand(NewVersionCommand())

return cmd
}

// NewCommand creates a Cobra command, which reflects our current best
// practices. The provided app might implement ApplicationRunner and
// ApplicationBinder.
func NewCommand(app interface{}) *cobra.Command {
var run func(cmd *cobra.Command, args []string)

// Note: since ApplicationRunnerWithContext and ApplicationRunner require
Expand All @@ -60,52 +108,13 @@ func NewRootCommand(app interface{}) *cobra.Command {
}

cmd := &cobra.Command{
Use: BuildName,
Run: run,

PersistentPreRun: func(cmd *cobra.Command, args []string) {
logrus.SetLevel(logrus.InfoLevel)

if verbose {
logrus.SetLevel(logrus.DebugLevel)
}

if gelfAddress != "" {
labels := map[string]interface{}{
"facility": BuildName,
"version": BuildVersion,
"commit-sha": BuildHash,
}
hook := graylog.NewGraylogHook(gelfAddress, labels)
hook.Level = logrus.DebugLevel
logrus.AddHook(hook)
}

logrus.WithFields(logrus.Fields{
"Version": BuildVersion,
"Date": BuildDate,
"Commit": BuildHash,
}).Infof("%s started", BuildName)
},

PersistentPostRun: func(cmd *cobra.Command, args []string) {
logrus.Infof("%s stopped", BuildName)
},
}

cmd.PersistentFlags().BoolVarP(
&verbose, "verbose", "v", false,
`Show debug logs.`)
cmd.PersistentFlags().StringVar(
&gelfAddress, "gelf-address", "",
`Address to Graylog for logging (format: "ip:port").`)

binder, ok := app.(ApplicationBinder)
if ok {
binder.Bind(cmd)
}

cmd.AddCommand(NewVersionCommand())

return cmd
}

0 comments on commit 335b544

Please sign in to comment.