Skip to content

Commit 567c8e9

Browse files
Add and use nonf variants of Infof and Warnf functions
1 parent 398d68a commit 567c8e9

File tree

10 files changed

+50
-23
lines changed

10 files changed

+50
-23
lines changed

cmd/modern/root/config/add-user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,5 @@ func (c *AddUser) run() {
168168
}
169169

170170
uniqueUserName := config.AddUser(user)
171-
output.Infof(localizer.Sprintf("User '%v' added", uniqueUserName))
171+
output.Info(localizer.Sprintf("User '%v' added", uniqueUserName))
172172
}

cmd/modern/root/config/connection-strings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (c *ConnectionStrings) run() {
120120
output.Infof("%-8s %s", k+":", v)
121121
}
122122
} else {
123-
output.Infof(localizer.Sprintf("Connection Strings only supported for %s Auth type", localizer.ModernAuthTypeBasic))
123+
output.Info(localizer.Sprintf("Connection Strings only supported for %s Auth type", localizer.ModernAuthTypeBasic))
124124
}
125125
}
126126

cmd/modern/root/config/delete-context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (c *DeleteContext) run() {
7878

7979
config.DeleteContext(c.name)
8080

81-
output.Infof(localizer.Sprintf("Context '%v' deleted", c.name))
81+
output.Info(localizer.Sprintf("Context '%v' deleted", c.name))
8282
} else {
8383
output.FatalfWithHintExamples([][]string{
8484
{localizer.Sprintf("View available contexts"), "sqlcmd config get-contexts"},

cmd/modern/root/config/delete-endpoint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,5 @@ func (c *DeleteEndpoint) run() {
6060
localizer.Sprintf("Endpoint '%v' does not exist", c.name))
6161
}
6262

63-
output.Infof(localizer.Sprintf("Endpoint '%v' deleted", c.name))
63+
output.Info(localizer.Sprintf("Endpoint '%v' deleted", c.name))
6464
}

cmd/modern/root/config/delete-user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ func (c *DeleteUser) run() {
5757
localizer.Sprintf("User %q does not exist", c.name))
5858
}
5959

60-
output.Infof(localizer.Sprintf("User %q deleted", c.name))
60+
output.Info(localizer.Sprintf("User %q deleted", c.name))
6161
}

cmd/modern/root/install/mssql-base.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
290290
c.downloadImage(imageName, output, controller)
291291
}
292292

293-
output.Infof(localizer.Sprintf("Starting %v", imageName))
293+
output.Info(localizer.Sprintf("Starting %v", imageName))
294294
containerId := controller.ContainerRun(
295295
imageName,
296296
env,
@@ -319,15 +319,15 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
319319
c.passwordEncryption,
320320
)
321321

322-
output.Infof(
322+
output.Info(
323323
localizer.Sprintf("Created context %q in \"%s\", configuring user account...",
324324
config.CurrentContextName(),
325325
config.GetConfigFileUsed()))
326326

327327
controller.ContainerWaitForLogEntry(
328328
containerId, c.errorLogEntryToWaitFor)
329329

330-
output.Infof(
330+
output.Info(
331331
localizer.Sprintf("Disabled %q account (and rotated %q password). Creating user %q",
332332
"sa",
333333
"sa",
@@ -452,7 +452,7 @@ func (c *MssqlBase) createNonSaUser(
452452
defaultDatabase = c.defaultDatabase
453453

454454
// Create the default database, if it isn't a downloaded database
455-
output.Infof(localizer.Sprintf("Creating default database [%s]", defaultDatabase))
455+
output.Info(localizer.Sprintf("Creating default database [%s]", defaultDatabase))
456456
c.query(fmt.Sprintf("CREATE DATABASE [%s]", defaultDatabase))
457457
}
458458

@@ -532,7 +532,7 @@ func (c *MssqlBase) downloadAndRestoreDb(
532532
_, file := filepath.Split(databaseUrl)
533533

534534
// Download file from URL into container
535-
output.Infof(localizer.Sprintf("Downloading %s", file))
535+
output.Info(localizer.Sprintf("Downloading %s", file))
536536

537537
temporaryFolder := "/var/opt/mssql/backup"
538538

@@ -543,7 +543,7 @@ func (c *MssqlBase) downloadAndRestoreDb(
543543
)
544544

545545
// Restore database from file
546-
output.Infof(localizer.Sprintf("Restoring database %s", databaseName))
546+
output.Info(localizer.Sprintf("Restoring database %s", databaseName))
547547

548548
dbNameAsIdentifier := getDbNameAsIdentifier(databaseName)
549549
dbNameAsNonIdentifier := getDbNameAsNonIdentifier(databaseName)
@@ -603,7 +603,7 @@ func (c *MssqlBase) downloadImage(
603603
output *output.Output,
604604
controller *container.Controller,
605605
) {
606-
output.Infof(localizer.Sprintf("Downloading %v", imageName))
606+
output.Info(localizer.Sprintf("Downloading %v", imageName))
607607
err := controller.EnsureImage(imageName)
608608
if err != nil || c.unitTesting {
609609
output.FatalfErrorWithHints(

cmd/modern/root/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (c *Start) run() {
4242
id := config.ContainerId()
4343
endpoint, _ := config.CurrentContext()
4444

45-
output.Infof(localizer.Sprintf("Starting %q for context %q", endpoint.ContainerDetails.Image, config.CurrentContextName()))
45+
output.Info(localizer.Sprintf("Starting %q for context %q", endpoint.ContainerDetails.Image, config.CurrentContextName()))
4646
err := controller.ContainerStart(id)
4747
c.CheckErr(err)
4848
} else {

cmd/modern/root/stop.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (c *Stop) run() {
4242
id := config.ContainerId()
4343
endpoint, _ := config.CurrentContext()
4444

45-
output.Infof(localizer.Sprintf("Stopping %q for context %q", endpoint.ContainerDetails.Image, config.CurrentContextName()))
45+
output.Info(localizer.Sprintf("Stopping %q for context %q", endpoint.ContainerDetails.Image, config.CurrentContextName()))
4646
err := controller.ContainerStop(id)
4747
c.CheckErr(err)
4848
} else {

cmd/modern/root/uninstall.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (c *Uninstall) run() {
9191

9292
var input string
9393
if !c.yes {
94-
output.Infof(
94+
output.Info(
9595
localizer.Sprintf("Current context is %q. Do you want to continue? (Y/N)",
9696
config.CurrentContextName(),
9797
))
@@ -103,7 +103,7 @@ func (c *Uninstall) run() {
103103
}
104104
}
105105
if controller.ContainerExists(id) && !c.force {
106-
output.Infof(localizer.Sprintf("Verifying no user (non-system) database (.mdf) files"))
106+
output.Info(localizer.Sprintf("Verifying no user (non-system) database (.mdf) files"))
107107
if !controller.ContainerRunning(id) {
108108
output.FatalfWithHintExamples([][]string{
109109
{localizer.Sprintf("To start the container"), "sqlcmd start"},
@@ -113,16 +113,16 @@ func (c *Uninstall) run() {
113113
c.userDatabaseSafetyCheck(controller, id)
114114
}
115115

116-
output.Infof(localizer.Sprintf("Removing context %s", config.CurrentContextName()))
116+
output.Info(localizer.Sprintf("Removing context %s", config.CurrentContextName()))
117117

118118
if controller.ContainerExists(id) {
119-
output.Infof(localizer.Sprintf("Stopping %s", endpoint.ContainerDetails.Image))
119+
output.Info(localizer.Sprintf("Stopping %s", endpoint.ContainerDetails.Image))
120120
err := controller.ContainerStop(id)
121121
c.CheckErr(err)
122122
err = controller.ContainerRemove(id)
123123
c.CheckErr(err)
124124
} else {
125-
output.Warnf(localizer.Sprintf("Container %q no longer exists, continuing to remove context...", id))
125+
output.Warn(localizer.Sprintf("Container %q no longer exists, continuing to remove context...", id))
126126
}
127127
}
128128

@@ -131,9 +131,9 @@ func (c *Uninstall) run() {
131131

132132
newContextName := config.CurrentContextName()
133133
if newContextName != "" {
134-
output.Infof(localizer.Sprintf("Current context is now %s", newContextName))
134+
output.Info(localizer.Sprintf("Current context is now %s", newContextName))
135135
} else {
136-
output.Infof(localizer.Sprintf("%v", "Operation completed successfully"))
136+
output.Info(localizer.Sprintf("%v", "Operation completed successfully"))
137137
}
138138
}
139139
}

internal/output/output.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ package output
1818

1919
import (
2020
"fmt"
21+
"regexp"
22+
"strings"
23+
2124
"github.com/microsoft/go-sqlcmd/internal/output/verbosity"
2225
"github.com/microsoft/go-sqlcmd/internal/pal"
2326
"github.com/pkg/errors"
24-
"regexp"
25-
"strings"
2627
)
2728

2829
func (o Output) Debugf(format string, a ...any) {
@@ -73,6 +74,16 @@ func (o Output) FatalWithHints(hints []string, a ...any) {
7374
o.fatal(hints, a...)
7475
}
7576

77+
func (o Output) Info(msg string) {
78+
if o.loggingLevel >= verbosity.Info {
79+
msg = o.ensureEol(msg)
80+
if o.loggingLevel >= verbosity.Debug {
81+
msg = "INFO: " + msg
82+
}
83+
o.print(msg)
84+
}
85+
}
86+
7687
func (o Output) Infof(format string, a ...any) {
7788
o.infofWithHints([]string{}, format, a...)
7889
}
@@ -126,6 +137,16 @@ func (o Output) Warnf(format string, a ...any) {
126137
}
127138
}
128139

140+
func (o Output) Warn(msg string) {
141+
if o.loggingLevel >= verbosity.Warn {
142+
msg = o.ensureEol(msg)
143+
if o.loggingLevel >= verbosity.Debug {
144+
msg = "WARN: " + msg
145+
}
146+
o.print(msg)
147+
}
148+
}
149+
129150
// displayHintExamples takes an array of hint examples and displays them in
130151
// a formatted way. It first calculates the maximum length of the description
131152
// in the hint examples, and then creates a string for each hint example with
@@ -223,3 +244,9 @@ func (o Output) printf(format string, a ...any) {
223244
_, err := o.standardWriteCloser.Write([]byte(text))
224245
o.errorCallback(err)
225246
}
247+
248+
func (o Output) print(msg string) {
249+
text := o.maskSecrets(msg)
250+
_, err := o.standardWriteCloser.Write([]byte(text))
251+
o.errorCallback(err)
252+
}

0 commit comments

Comments
 (0)