1
0
mirror of https://github.com/rancher/steve.git synced 2025-04-28 19:24:42 +00:00
steve/pkg/debug/cli.go
2025-04-09 04:39:36 +00:00

63 lines
1.0 KiB
Go

package debug
import (
"flag"
"fmt"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v3"
"k8s.io/klog"
)
type Config struct {
Debug bool
DebugLevel int
SQLCache bool
}
func (c *Config) MustSetupDebug() {
err := c.SetupDebug()
if err != nil {
panic("failed to setup debug logging: " + err.Error())
}
}
func (c *Config) SetupDebug() error {
logging := flag.NewFlagSet("", flag.PanicOnError)
klog.InitFlags(logging)
if c.Debug {
logrus.SetLevel(logrus.DebugLevel)
if err := logging.Parse([]string{
fmt.Sprintf("-v=%d", c.DebugLevel),
}); err != nil {
return err
}
} else {
if err := logging.Parse([]string{
"-v=0",
}); err != nil {
return err
}
}
return nil
}
func Flags(config *Config) []cli.Flag {
return []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Destination: &config.Debug,
},
&cli.IntFlag{
Name: "debug-level",
Value: 7,
Destination: &config.DebugLevel,
},
&cli.BoolFlag{
Name: "sql-cache",
Destination: &config.SQLCache,
},
}
}