Skip to content

Commit

Permalink
Replace calls to log.Panic{|f} with panic
Browse files Browse the repository at this point in the history
  • Loading branch information
ellismg committed Dec 17, 2024
1 parent 4360c0b commit d61ad30
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 28 deletions.
5 changes: 2 additions & 3 deletions cli/azd/cmd/cobra_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"log"
"slices"
"strconv"
"strings"
Expand Down Expand Up @@ -175,7 +174,7 @@ func (df *docsFlag) Set(value string) error {
// Setting help to true will make cobra to stop and call the HelpFunc
if err = df.command.Flag("help").Value.Set("true"); err != nil {
// dev-issue: help flag should be already been added when
log.Panic("tried to set help after docs parameter: %w", err)
panic(fmt.Sprintf("tried to set help after docs parameter: %v", err))
}

// keeping the default help function allows to set --help with higher priority and use it
Expand Down Expand Up @@ -213,7 +212,7 @@ func (cb *CobraBuilder) bindCommand(cmd *cobra.Command, descriptor *actions.Acti
consoleFn: func() input.Console {
var console input.Console
if err := cb.container.Resolve(&console); err != nil {
log.Panic("creating docs flag: %w", err)
panic(fmt.Sprintf("creating docs flag: %v", err))
}
return console
},
Expand Down
5 changes: 2 additions & 3 deletions cli/azd/internal/cmd/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"fmt"
"io"
"log"
"log/slog"
"os"
"path/filepath"
Expand Down Expand Up @@ -123,12 +122,12 @@ func (a *AddAction) Run(ctx context.Context) (*actions.ActionResult, error) {
}

if _, exists := prjConfig.Resources[resourceToAdd.Name]; exists {
log.Panicf("unhandled validation: resource with name %s already exists", resourceToAdd.Name)
panic(fmt.Sprintf("unhandled validation: resource with name %s already exists", resourceToAdd.Name))
}

if serviceToAdd != nil {
if _, exists := prjConfig.Services[serviceToAdd.Name]; exists {
log.Panicf("unhandled validation: service with name %s already exists", serviceToAdd.Name)
panic(fmt.Sprintf("unhandled validation: service with name %s already exists", serviceToAdd.Name))
}
}

Expand Down
3 changes: 1 addition & 2 deletions cli/azd/internal/repository/detect_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"log"
"maps"
"os"
"path/filepath"
Expand Down Expand Up @@ -401,7 +400,7 @@ func (d *detectConfirm) add(ctx context.Context) error {
d.modified = true
return nil
default:
log.Panic("unhandled entry type")
panic("unhandled entry type")
}

msg := fmt.Sprintf("Enter file path of the directory that uses '%s'", projectDisplayName(s))
Expand Down
3 changes: 1 addition & 2 deletions cli/azd/pkg/alpha/alpha_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package alpha

import (
"fmt"
"log"
"strings"

"github.com/azure/azure-dev/cli/azd/resources"
Expand Down Expand Up @@ -35,7 +34,7 @@ var allFeatures []Feature
func init() {
err := yaml.Unmarshal(resources.AlphaFeatures, &allFeatures)
if err != nil {
log.Panic("Can't marshall alpha features!! %w", err)
panic(fmt.Sprintf("Can't marshall alpha features!! %v", err))
}
}

Expand Down
10 changes: 4 additions & 6 deletions cli/azd/pkg/alpha/alpha_feature_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package alpha
import (
"context"
"fmt"
"log"
"log/slog"
"os"
"strconv"
Expand Down Expand Up @@ -70,7 +69,7 @@ func (m *FeatureManager) initConfigCache() {
if m.userConfigCache == nil {
config, err := m.configManager.Load()
if err != nil {
log.Panic("Can't load user config!! %w", err)
panic(fmt.Sprintf("Can't load user config!! %v", err))
}
m.userConfigCache = config
}
Expand Down Expand Up @@ -146,18 +145,17 @@ func isEnabled(config config.Config, id FeatureId) bool {
// need to check the cast here in case the config is manually updated
stringValue, castResult := value.(string)
if !castResult {
log.Panicf("Invalid configuration value for '%s': %s", longKey, value)
panic(fmt.Sprintf("Invalid configuration value for '%s': %s", longKey, value))
}
stringValue = strings.ToLower(stringValue)

if stringValue != disabledValue && stringValue != enabledValue {
log.Panicf(
"invalid configuration value for '%s': %s. Valid options are '%s' or '%s'.",
panic(fmt.Sprintf("invalid configuration value for '%s': %s. Valid options are '%s' or '%s'.",
longKey,
stringValue,
enabledValue,
disabledValue,
)
))
}

// previous condition ensured that stringValue is either `enabledValue` or `disabledValue`
Expand Down
9 changes: 4 additions & 5 deletions cli/azd/pkg/infra/provisioning/bicep/bicep_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"log/slog"
"maps"
"math"
Expand Down Expand Up @@ -1061,7 +1060,7 @@ func (p *BicepProvider) destroyDeploymentWithConfirmation(
func itemsCountAsText(items []itemToPurge) string {
count := len(items)
if count < 1 {
log.Panic("calling itemsCountAsText() with empty list.")
panic("calling itemsCountAsText() with empty list.")
}

var tokens []string
Expand Down Expand Up @@ -1968,17 +1967,17 @@ func mustSetParamAsConfig(key string, value any, config config.Config, isSecured

if !isSecured {
if err := config.Set(configKey, value); err != nil {
log.Panicf("failed setting config value: %v", err)
panic(fmt.Sprintf("failed setting config value: %v", err))
}
return
}

secretString, castOk := value.(string)
if !castOk {
log.Panic("tried to set a non-string as secret. This is not supported.")
panic("tried to set a non-string as secret. This is not supported.")
}
if err := config.SetSecret(configKey, secretString); err != nil {
log.Panicf("failed setting a secret in config: %v", err)
panic(fmt.Sprintf("failed setting a secret in config: %v", err))
}
}

Expand Down
4 changes: 1 addition & 3 deletions cli/azd/pkg/input/console_previewer_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

package input

import "log"

// ConsolePreviewerWriter implements io.Writer and is used to wrap a progress log
// and panic if the writer is used after the previewer is stopped.
type consolePreviewerWriter struct {
Expand All @@ -17,7 +15,7 @@ func (cp *consolePreviewerWriter) Write(logBytes []byte) (int, error) {
writer := *cp.previewer
if writer == nil {
//dev-bug - tried to write to a closed console previewer
log.Panic("tried to write to a closed console previewer.")
panic("tried to write to a closed console previewer.")
}

return writer.Write(logBytes)
Expand Down
3 changes: 1 addition & 2 deletions cli/azd/pkg/input/progress_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package input
import (
"bytes"
"fmt"
"log"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -228,7 +227,7 @@ func offset(src, regex string) int {
var qty int
qty, err := strconv.Atoi(string(match[1]))
if err != nil {
log.Panic("converting string to int: %w", err)
panic(fmt.Sprintf("converting string to int: %v", err))
}
return qty
}
Expand Down
3 changes: 1 addition & 2 deletions cli/azd/pkg/output/ux/list_as_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package ux

import (
"fmt"
"log"
"strings"
)

Expand All @@ -17,7 +16,7 @@ import (
func ListAsText(items []string) string {
count := len(items)
if count < 1 {
log.Panic("calling ListAsText() with empty list.")
panic("calling ListAsText() with empty list.")
}

if count == 1 {
Expand Down

0 comments on commit d61ad30

Please sign in to comment.