chore(log): use PersistentPreRunE to setup logging in a cleaner way

This commit is contained in:
2022-03-22 00:34:47 +00:00
parent bc69c016a3
commit dd5238f9ff
7 changed files with 15 additions and 17 deletions

View File

@@ -11,17 +11,13 @@ import (
type runEFunc func(cmd *cobra.Command, _ []string) error
func WithPrettyLogging(
f func(cmd *cobra.Command, args []string) error,
) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
err := SetupZerolog(cmd)
if err != nil {
return err
}
return f(cmd, args)
func PersistentPreRunE(cmd *cobra.Command, _ []string) error {
err := SetupZerolog(cmd)
if err != nil {
return err
}
return nil
}
func SetupZerolog(cmd *cobra.Command) error {

View File

@@ -11,7 +11,7 @@ func NewConfig(mgr *manager.Manager) (*cobra.Command, error) {
Short: "Show evm environment/setup details",
Aliases: []string{"env", "info"},
ValidArgs: []string{},
RunE: WithPrettyLogging(configRunE(mgr)),
RunE: configRunE(mgr),
}
cmd.Flags().StringP("format", "f", "", "output format (yaml or json)")

View File

@@ -7,8 +7,10 @@ import (
func NewEvm(mgr *manager.Manager) (*cobra.Command, error) {
cmd := &cobra.Command{
Use: "evm",
Short: "A simple and opinionated Emacs Version Manager and build tool",
Use: "evm",
Short: "A simple and opinionated Emacs Version Manager " +
"and build tool",
PersistentPreRunE: PersistentPreRunE,
}
cmd.PersistentFlags().StringP(

View File

@@ -22,7 +22,7 @@ func NewExec(mgr *manager.Manager) (*cobra.Command, error) {
DisableFlagsInUseLine: true,
Hidden: true,
ValidArgsFunction: execValidArgs(mgr),
RunE: WithPrettyLogging(execRunE(mgr)),
RunE: execRunE(mgr),
}
return cmd, nil

View File

@@ -15,7 +15,7 @@ func NewList(mgr *manager.Manager) (*cobra.Command, error) {
Aliases: []string{"ls", "versions"},
Args: cobra.ExactArgs(0),
ValidArgsFunction: noValidArgs,
RunE: WithPrettyLogging(listRunE(mgr)),
RunE: listRunE(mgr),
}
cmd.Flags().StringP("format", "f", "", "output format (yaml or json)")

View File

@@ -13,7 +13,7 @@ func NewRehash(mgr *manager.Manager) (*cobra.Command, error) {
Short: "Update shims for all or specific versions",
Aliases: []string{"reshim"},
ValidArgsFunction: rehashValidArgs(mgr),
RunE: WithPrettyLogging(rehashRunE(mgr)),
RunE: rehashRunE(mgr),
}
return cmd, nil

View File

@@ -14,7 +14,7 @@ func NewUse(mgr *manager.Manager) (*cobra.Command, error) {
Aliases: []string{"activate", "switch"},
Args: cobra.ExactArgs(1),
ValidArgsFunction: useValidArgs(mgr),
RunE: WithPrettyLogging(useRunE(mgr)),
RunE: useRunE(mgr),
}
return cmd, nil