mirror of
https://github.com/aviator-co/av.git
synced 2026-01-06 04:09:57 -06:00
Fix Post->PreRun, add more comments
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user