Move logging setup to be configurable

Signed-off-by: Matt Farina <matt.farina@suse.com>
pull/31018/head
Matt Farina 2 months ago
parent 17b3b5b19f
commit f0cf9c28f0
No known key found for this signature in database
GPG Key ID: 92C44A3D421FF7F9

@ -34,7 +34,7 @@ func main() {
// manager as picked up by the automated name detection.
kube.ManagedFieldsManager = "helm"
cmd, err := helmcmd.NewRootCmd(os.Stdout, os.Args[1:])
cmd, err := helmcmd.NewRootCmd(os.Stdout, os.Args[1:], helmcmd.SetupLogging)
if err != nil {
slog.Warn("command failed", slog.Any("error", err))
os.Exit(1)

@ -94,7 +94,7 @@ func executeActionCommandStdinC(store *storage.Storage, in *os.File, cmd string)
Capabilities: chartutil.DefaultCapabilities,
}
root, err := newRootCmdWithConfig(actionConfig, buf, args)
root, err := newRootCmdWithConfig(actionConfig, buf, args, SetupLogging)
if err != nil {
return nil, "", err
}

@ -98,9 +98,9 @@ By default, the default directories depend on the Operating System. The defaults
var settings = cli.New()
func NewRootCmd(out io.Writer, args []string) (*cobra.Command, error) {
func NewRootCmd(out io.Writer, args []string, logSetup func(bool)) (*cobra.Command, error) {
actionConfig := new(action.Configuration)
cmd, err := newRootCmdWithConfig(actionConfig, out, args)
cmd, err := newRootCmdWithConfig(actionConfig, out, args, logSetup)
if err != nil {
return nil, err
}
@ -117,7 +117,19 @@ func NewRootCmd(out io.Writer, args []string) (*cobra.Command, error) {
return cmd, nil
}
func newRootCmdWithConfig(actionConfig *action.Configuration, out io.Writer, args []string) (*cobra.Command, error) {
// SetupLogging sets up Helm logging used by the Helm client.
// This function is passed to the NewRootCmd function to enable logging. Any other
// application that uses the NewRootCmd function to setup all the Helm commands may
// use this function to setup logging or their own. Using a custom logging setup function
// enables applications using Helm commands to integrate with their existing logging
// system.
// The debug argument is the value if Helm is set for debugging (i.e. --debug flag)
func SetupLogging(debug bool) {
logger := logging.NewLogger(func() bool { return debug })
slog.SetDefault(logger)
}
func newRootCmdWithConfig(actionConfig *action.Configuration, out io.Writer, args []string, logSetup func(bool)) (*cobra.Command, error) {
cmd := &cobra.Command{
Use: "helm",
Short: "The Helm package manager for Kubernetes.",
@ -140,8 +152,14 @@ func newRootCmdWithConfig(actionConfig *action.Configuration, out io.Writer, arg
settings.AddFlags(flags)
addKlogFlags(flags)
logger := logging.NewLogger(func() bool { return settings.Debug })
slog.SetDefault(logger)
// We can safely ignore any errors that flags.Parse encounters since
// those errors will be caught later during the call to cmd.Execution.
// This call is required to gather configuration information prior to
// execution.
flags.ParseErrorsWhitelist.UnknownFlags = true
flags.Parse(args)
logSetup(settings.Debug)
// Setup shell completion for the namespace flag
err := cmd.RegisterFlagCompletionFunc("namespace", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
@ -190,13 +208,6 @@ func newRootCmdWithConfig(actionConfig *action.Configuration, out io.Writer, arg
log.Fatal(err)
}
// We can safely ignore any errors that flags.Parse encounters since
// those errors will be caught later during the call to cmd.Execution.
// This call is required to gather configuration information prior to
// execution.
flags.ParseErrorsWhitelist.UnknownFlags = true
flags.Parse(args)
registryClient, err := newDefaultRegistryClient(false, "", "")
if err != nil {
return nil, err

Loading…
Cancel
Save