Skip to content

Commit

Permalink
fix: H: fixup command line usage and grpc log level setting. (#3334)
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Alpar <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
aaron-kasten and mergify[bot] authored Jan 18, 2025
1 parent 4ca609e commit 01c766c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
7 changes: 4 additions & 3 deletions pkg/kando/kando.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"github.com/kanisterio/kanister/pkg/version"
)

var logLevel string

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
Expand All @@ -43,10 +45,9 @@ func newRootCommand() *cobra.Command {
Version: version.VersionString(),
}

var v string
rootCmd.PersistentFlags().StringVarP(&v, "verbosity", "v", logrus.WarnLevel.String(), "Log level (debug, info, warn, error, fatal, panic)")
rootCmd.PersistentFlags().StringVarP(&logLevel, "verbosity", "v", logrus.WarnLevel.String(), "Log level (debug, info, warn, error, fatal, panic)")
rootCmd.PersistentPreRunE = func(*cobra.Command, []string) error {
return setLogLevel(v)
return setLogLevel(logLevel)
}

rootCmd.AddCommand(newLocationCommand())
Expand Down
33 changes: 33 additions & 0 deletions pkg/kando/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,56 @@
package kando

import (
"io"
"net"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/grpc/grpclog"
)

const (
processAddressFlagName = "address"
)

// grpclogLogger method taken from grpclog/loggerv2.go:newLoggerV2
// see also logging/logrus/grpclogger.go.
// grpclogger.go may be the best way to setup the loggers
// but kanister logging, while using logrus, does not seem to offer a straightforward
// interface for performing the interfacing in the grpclogger.go example.
func grpclogLogger(cmd *cobra.Command) grpclog.LoggerV2 {
var infow, warnw, errorw io.Writer
infow = io.Discard
warnw = io.Discard
errorw = io.Discard
switch {
case logrus.IsLevelEnabled(logrus.InfoLevel):
infow = cmd.ErrOrStderr()
case logrus.IsLevelEnabled(logrus.WarnLevel):
warnw = cmd.ErrOrStderr()
case logrus.IsLevelEnabled(logrus.ErrorLevel):
errorw = cmd.ErrOrStderr()
}
return grpclog.NewLoggerV2(infow, warnw, errorw)
}

func newProcessCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "process <command>",
Short: "Manage kando processes",
}

cmd.AddCommand(newProcessServerCommand())
cmd.AddCommand(newProcessClientCommand())
cmd.PersistentFlags().StringP(processAddressFlagName, "a", "/tmp/kanister.sock", "The path of a unix socket of the process server")
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
err := setLogLevel(logLevel)
if err != nil {
return err
}
grpclog.SetLoggerV2(grpclogLogger(cmd))
return nil
}
return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/kando/process_client_signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

func newProcessClientSignalCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "signal SIGNAL PID",
Use: "signal PID SIGNAL",
Short: "send a signal to a managed process",
Args: cobra.ExactArgs(2),
RunE: runProcessClientSignal,
Expand Down

0 comments on commit 01c766c

Please sign in to comment.