Skip to content

Commit

Permalink
Add and use nonf variants of Infof and Warnf functions
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvdeshmukh committed Jul 7, 2023
1 parent 398d68a commit 567c8e9
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/modern/root/config/add-user.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,5 @@ func (c *AddUser) run() {
}

uniqueUserName := config.AddUser(user)
output.Infof(localizer.Sprintf("User '%v' added", uniqueUserName))
output.Info(localizer.Sprintf("User '%v' added", uniqueUserName))
}
2 changes: 1 addition & 1 deletion cmd/modern/root/config/connection-strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (c *ConnectionStrings) run() {
output.Infof("%-8s %s", k+":", v)
}
} else {
output.Infof(localizer.Sprintf("Connection Strings only supported for %s Auth type", localizer.ModernAuthTypeBasic))
output.Info(localizer.Sprintf("Connection Strings only supported for %s Auth type", localizer.ModernAuthTypeBasic))
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/modern/root/config/delete-context.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (c *DeleteContext) run() {

config.DeleteContext(c.name)

output.Infof(localizer.Sprintf("Context '%v' deleted", c.name))
output.Info(localizer.Sprintf("Context '%v' deleted", c.name))
} else {
output.FatalfWithHintExamples([][]string{
{localizer.Sprintf("View available contexts"), "sqlcmd config get-contexts"},
Expand Down
2 changes: 1 addition & 1 deletion cmd/modern/root/config/delete-endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ func (c *DeleteEndpoint) run() {
localizer.Sprintf("Endpoint '%v' does not exist", c.name))
}

output.Infof(localizer.Sprintf("Endpoint '%v' deleted", c.name))
output.Info(localizer.Sprintf("Endpoint '%v' deleted", c.name))
}
2 changes: 1 addition & 1 deletion cmd/modern/root/config/delete-user.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ func (c *DeleteUser) run() {
localizer.Sprintf("User %q does not exist", c.name))
}

output.Infof(localizer.Sprintf("User %q deleted", c.name))
output.Info(localizer.Sprintf("User %q deleted", c.name))
}
14 changes: 7 additions & 7 deletions cmd/modern/root/install/mssql-base.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
c.downloadImage(imageName, output, controller)
}

output.Infof(localizer.Sprintf("Starting %v", imageName))
output.Info(localizer.Sprintf("Starting %v", imageName))
containerId := controller.ContainerRun(
imageName,
env,
Expand Down Expand Up @@ -319,15 +319,15 @@ func (c *MssqlBase) createContainer(imageName string, contextName string) {
c.passwordEncryption,
)

output.Infof(
output.Info(
localizer.Sprintf("Created context %q in \"%s\", configuring user account...",
config.CurrentContextName(),
config.GetConfigFileUsed()))

controller.ContainerWaitForLogEntry(
containerId, c.errorLogEntryToWaitFor)

output.Infof(
output.Info(
localizer.Sprintf("Disabled %q account (and rotated %q password). Creating user %q",
"sa",
"sa",
Expand Down Expand Up @@ -452,7 +452,7 @@ func (c *MssqlBase) createNonSaUser(
defaultDatabase = c.defaultDatabase

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

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

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

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

Expand All @@ -543,7 +543,7 @@ func (c *MssqlBase) downloadAndRestoreDb(
)

// Restore database from file
output.Infof(localizer.Sprintf("Restoring database %s", databaseName))
output.Info(localizer.Sprintf("Restoring database %s", databaseName))

dbNameAsIdentifier := getDbNameAsIdentifier(databaseName)
dbNameAsNonIdentifier := getDbNameAsNonIdentifier(databaseName)
Expand Down Expand Up @@ -603,7 +603,7 @@ func (c *MssqlBase) downloadImage(
output *output.Output,
controller *container.Controller,
) {
output.Infof(localizer.Sprintf("Downloading %v", imageName))
output.Info(localizer.Sprintf("Downloading %v", imageName))
err := controller.EnsureImage(imageName)
if err != nil || c.unitTesting {
output.FatalfErrorWithHints(
Expand Down
2 changes: 1 addition & 1 deletion cmd/modern/root/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *Start) run() {
id := config.ContainerId()
endpoint, _ := config.CurrentContext()

output.Infof(localizer.Sprintf("Starting %q for context %q", endpoint.ContainerDetails.Image, config.CurrentContextName()))
output.Info(localizer.Sprintf("Starting %q for context %q", endpoint.ContainerDetails.Image, config.CurrentContextName()))
err := controller.ContainerStart(id)
c.CheckErr(err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion cmd/modern/root/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *Stop) run() {
id := config.ContainerId()
endpoint, _ := config.CurrentContext()

output.Infof(localizer.Sprintf("Stopping %q for context %q", endpoint.ContainerDetails.Image, config.CurrentContextName()))
output.Info(localizer.Sprintf("Stopping %q for context %q", endpoint.ContainerDetails.Image, config.CurrentContextName()))
err := controller.ContainerStop(id)
c.CheckErr(err)
} else {
Expand Down
14 changes: 7 additions & 7 deletions cmd/modern/root/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (c *Uninstall) run() {

var input string
if !c.yes {
output.Infof(
output.Info(
localizer.Sprintf("Current context is %q. Do you want to continue? (Y/N)",
config.CurrentContextName(),
))
Expand All @@ -103,7 +103,7 @@ func (c *Uninstall) run() {
}
}
if controller.ContainerExists(id) && !c.force {
output.Infof(localizer.Sprintf("Verifying no user (non-system) database (.mdf) files"))
output.Info(localizer.Sprintf("Verifying no user (non-system) database (.mdf) files"))
if !controller.ContainerRunning(id) {
output.FatalfWithHintExamples([][]string{
{localizer.Sprintf("To start the container"), "sqlcmd start"},
Expand All @@ -113,16 +113,16 @@ func (c *Uninstall) run() {
c.userDatabaseSafetyCheck(controller, id)
}

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

if controller.ContainerExists(id) {
output.Infof(localizer.Sprintf("Stopping %s", endpoint.ContainerDetails.Image))
output.Info(localizer.Sprintf("Stopping %s", endpoint.ContainerDetails.Image))
err := controller.ContainerStop(id)
c.CheckErr(err)
err = controller.ContainerRemove(id)
c.CheckErr(err)
} else {
output.Warnf(localizer.Sprintf("Container %q no longer exists, continuing to remove context...", id))
output.Warn(localizer.Sprintf("Container %q no longer exists, continuing to remove context...", id))
}
}

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

newContextName := config.CurrentContextName()
if newContextName != "" {
output.Infof(localizer.Sprintf("Current context is now %s", newContextName))
output.Info(localizer.Sprintf("Current context is now %s", newContextName))
} else {
output.Infof(localizer.Sprintf("%v", "Operation completed successfully"))
output.Info(localizer.Sprintf("%v", "Operation completed successfully"))
}
}
}
Expand Down
31 changes: 29 additions & 2 deletions internal/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package output

import (
"fmt"
"regexp"
"strings"

"github.com/microsoft/go-sqlcmd/internal/output/verbosity"
"github.com/microsoft/go-sqlcmd/internal/pal"
"github.com/pkg/errors"
"regexp"
"strings"
)

func (o Output) Debugf(format string, a ...any) {
Expand Down Expand Up @@ -73,6 +74,16 @@ func (o Output) FatalWithHints(hints []string, a ...any) {
o.fatal(hints, a...)
}

func (o Output) Info(msg string) {
if o.loggingLevel >= verbosity.Info {
msg = o.ensureEol(msg)
if o.loggingLevel >= verbosity.Debug {
msg = "INFO: " + msg
}
o.print(msg)
}
}

func (o Output) Infof(format string, a ...any) {
o.infofWithHints([]string{}, format, a...)
}
Expand Down Expand Up @@ -126,6 +137,16 @@ func (o Output) Warnf(format string, a ...any) {
}
}

func (o Output) Warn(msg string) {
if o.loggingLevel >= verbosity.Warn {
msg = o.ensureEol(msg)
if o.loggingLevel >= verbosity.Debug {
msg = "WARN: " + msg
}
o.print(msg)
}
}

// displayHintExamples takes an array of hint examples and displays them in
// a formatted way. It first calculates the maximum length of the description
// in the hint examples, and then creates a string for each hint example with
Expand Down Expand Up @@ -223,3 +244,9 @@ func (o Output) printf(format string, a ...any) {
_, err := o.standardWriteCloser.Write([]byte(text))
o.errorCallback(err)
}

func (o Output) print(msg string) {
text := o.maskSecrets(msg)
_, err := o.standardWriteCloser.Write([]byte(text))
o.errorCallback(err)
}

0 comments on commit 567c8e9

Please sign in to comment.