Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion mgradm/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path"
"strings"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/mgradm/cmd/backup"
Expand Down Expand Up @@ -61,7 +62,7 @@ func NewUyuniadmCommand() (*cobra.Command, error) {
rootCmd.PersistentPreRun = func(cmd *cobra.Command, _ []string) {
// do not log if running the completion cmd as the output is redirected to create a file to source
if cmd.Name() != "completion" {
utils.LogInit(true)
utils.LogInit(shouldLogToConsole(cmd, globalFlags.LogLevel))
utils.SetLogLevel(globalFlags.LogLevel)
log.Info().Msgf(L("Starting %s"), strings.Join(os.Args, " "))
log.Info().Msgf(L("Use of this software implies acceptance of the End User License Agreement."))
Expand Down Expand Up @@ -97,3 +98,21 @@ func NewUyuniadmCommand() (*cobra.Command, error) {

return rootCmd, err
}

func shouldLogToConsole(cmd *cobra.Command, logLevel string) bool {
if !isInteractiveSupportSQL(cmd) {
return true
}

level, err := zerolog.ParseLevel(logLevel)
return err != nil || level > zerolog.DebugLevel
}

func isInteractiveSupportSQL(cmd *cobra.Command) bool {
if cmd.Name() != "sql" || cmd.Parent() == nil || cmd.Parent().Name() != "support" {
return false
}

interactive, err := cmd.Flags().GetBool("interactive")
return err == nil && interactive
}
52 changes: 52 additions & 0 deletions mgradm/cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-FileCopyrightText: 2026 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0

package cmd

import "testing"

func TestShouldLogToConsoleForInteractiveSQL(t *testing.T) {
rootCmd, err := NewUyuniadmCommand()
if err != nil {
t.Fatalf("failed to create command: %v", err)
}

sqlCmd, _, err := rootCmd.Find([]string{"support", "sql"})
if err != nil {
t.Fatalf("failed to find sql command: %v", err)
}

if err := sqlCmd.ParseFlags([]string{"--interactive"}); err != nil {
t.Fatalf("failed to parse flags: %v", err)
}

if shouldLogToConsole(sqlCmd, "debug") {
t.Error("interactive sql should not log to console in debug mode")
}
if shouldLogToConsole(sqlCmd, "trace") {
t.Error("interactive sql should not log to console in trace mode")
}
if !shouldLogToConsole(sqlCmd, "info") {
t.Error("interactive sql should still log to console in info mode")
}
if !shouldLogToConsole(sqlCmd, "") {
t.Error("interactive sql should still log to console with default log level")
}
}

func TestShouldLogToConsoleForNonInteractiveSQL(t *testing.T) {
rootCmd, err := NewUyuniadmCommand()
if err != nil {
t.Fatalf("failed to create command: %v", err)
}

sqlCmd, _, err := rootCmd.Find([]string{"support", "sql"})
if err != nil {
t.Fatalf("failed to find sql command: %v", err)
}

if shouldLogToConsole(sqlCmd, "debug") == false {
t.Error("non-interactive sql should keep console logging")
}
}