Fix Post->PreRun, add more comments

This commit is contained in:
Travis DePrato
2022-04-18 13:50:32 -07:00
parent e515847bc5
commit 84a341980d
2 changed files with 25 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
package main
import (
"fmt"
"github.com/aviator-co/av/internal/config"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@@ -12,14 +13,24 @@ var rootFlags struct {
}
var rootCmd = &cobra.Command{
Use: "av",
Use: "av",
// Don't automatically print errors or usage information (we handle that ourselves).
// Cobra still prints usage if you return cmd.Usage() from RunE.
SilenceErrors: true,
SilenceUsage: true,
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
// Don't show "completion" command in help menu
CompletionOptions: cobra.CompletionOptions{
HiddenDefaultCmd: true,
},
// Run setup before invoking any child commands.
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if rootFlags.Debug {
logrus.SetLevel(logrus.DebugLevel)
logrus.WithField("av_version", config.Version).Debug("enabled debug logging")
}
logrus.Debugf("av version: %s", config.Version)
return nil
},
}
@@ -33,6 +44,15 @@ func init() {
func main() {
if err := rootCmd.Execute(); err != nil {
format := "error: %s\n"
// In debug mode, show more detailed information about the error
// (including the stack trace if using pkg/errors).
if rootFlags.Debug {
format = "error: %#+v\n"
}
_, _ = fmt.Fprintf(os.Stderr, format, err)
os.Exit(1)
}
}

View File

@@ -9,7 +9,8 @@ import (
var versionCmd = &cobra.Command{
Use: "version",
Short: "print the version information",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println(config.Version)
return nil
},
}