Skip to content

Commit e0df6df

Browse files
fix: avoid console logs in interactive sql
Signed-off-by: Mridankan Mandal <xerontitan90@gmail.com>
1 parent 18a527a commit e0df6df

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

mgradm/cmd/cmd.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"path"
1010
"strings"
1111

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

9899
return rootCmd, err
99100
}
101+
102+
func shouldLogToConsole(cmd *cobra.Command, logLevel string) bool {
103+
if !isInteractiveSupportSQL(cmd) {
104+
return true
105+
}
106+
107+
level, err := zerolog.ParseLevel(logLevel)
108+
return err != nil || level > zerolog.DebugLevel
109+
}
110+
111+
func isInteractiveSupportSQL(cmd *cobra.Command) bool {
112+
if cmd.Name() != "sql" || cmd.Parent() == nil || cmd.Parent().Name() != "support" {
113+
return false
114+
}
115+
116+
interactive, err := cmd.Flags().GetBool("interactive")
117+
return err == nil && interactive
118+
}

mgradm/cmd/cmd_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-FileCopyrightText: 2026 SUSE LLC
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package cmd
6+
7+
import "testing"
8+
9+
func TestShouldLogToConsoleForInteractiveSQL(t *testing.T) {
10+
rootCmd, err := NewUyuniadmCommand()
11+
if err != nil {
12+
t.Fatalf("failed to create command: %v", err)
13+
}
14+
15+
sqlCmd, _, err := rootCmd.Find([]string{"support", "sql"})
16+
if err != nil {
17+
t.Fatalf("failed to find sql command: %v", err)
18+
}
19+
20+
if err := sqlCmd.ParseFlags([]string{"--interactive"}); err != nil {
21+
t.Fatalf("failed to parse flags: %v", err)
22+
}
23+
24+
if shouldLogToConsole(sqlCmd, "debug") {
25+
t.Error("interactive sql should not log to console in debug mode")
26+
}
27+
if shouldLogToConsole(sqlCmd, "trace") {
28+
t.Error("interactive sql should not log to console in trace mode")
29+
}
30+
if !shouldLogToConsole(sqlCmd, "info") {
31+
t.Error("interactive sql should still log to console in info mode")
32+
}
33+
if !shouldLogToConsole(sqlCmd, "") {
34+
t.Error("interactive sql should still log to console with default log level")
35+
}
36+
}
37+
38+
func TestShouldLogToConsoleForNonInteractiveSQL(t *testing.T) {
39+
rootCmd, err := NewUyuniadmCommand()
40+
if err != nil {
41+
t.Fatalf("failed to create command: %v", err)
42+
}
43+
44+
sqlCmd, _, err := rootCmd.Find([]string{"support", "sql"})
45+
if err != nil {
46+
t.Fatalf("failed to find sql command: %v", err)
47+
}
48+
49+
if shouldLogToConsole(sqlCmd, "debug") == false {
50+
t.Error("non-interactive sql should keep console logging")
51+
}
52+
}

0 commit comments

Comments
 (0)