Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use strings.Replacer and strings.ReplaceAll where appropriate #667

Merged
Merged
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
14 changes: 9 additions & 5 deletions alerts/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func joinMonitorsAndHosts(client *mackerel.Client, alerts []*mackerel.Alert) []*
return alertSets
}

var statusRegexp = regexp.MustCompile("^[2-5][0-9][0-9]$")

func formatJoinedAlert(alertSet *alertSet, colorize bool) string {
const layout = "2006-01-02 15:04:05"

Expand Down Expand Up @@ -165,7 +167,6 @@ func formatJoinedAlert(alertSet *alertSet, colorize bool) string {
monitorMsg = fmt.Sprintf("%s %s %.2f", m.Service, m.Metric, alert.Value)
}
case *mackerel.MonitorExternalHTTP:
statusRegexp, _ := regexp.Compile("^[2345][0-9][0-9]$")
switch alert.Status {
case "CRITICAL":
if statusRegexp.MatchString(alert.Message) && m.ResponseTimeCritical != nil {
Expand Down Expand Up @@ -225,11 +226,14 @@ func formatJoinedAlert(alertSet *alertSet, colorize bool) string {
return fmt.Sprintf("%s %s %s %s%s", alert.ID, time.Unix(alert.OpenedAt, 0).Format(layout), statusMsg, monitorMsg, hostMsg)
}

var expressionNewlinePattern = regexp.MustCompile(`\s*[\r\n]+\s*`)
var (
expressionNewlinePattern = regexp.MustCompile(`\s*[\r\n]+\s*`)
expressionParenthesisReplacer = strings.NewReplacer("( ", "(", " )", ")")
)

func formatExpressionOneline(expr string) string {
expr = strings.Trim(expressionNewlinePattern.ReplaceAllString(expr, " "), " ")
return strings.Replace(strings.Replace(expr, "( ", "(", -1), " )", ")", -1)
expr = strings.TrimSpace(expressionNewlinePattern.ReplaceAllString(expr, " "))
return expressionParenthesisReplacer.Replace(expr)
}

func formatCheckMessage(msg string) string {
Expand All @@ -243,7 +247,7 @@ func formatCheckMessage(msg string) string {
truncated = true
}
if truncated {
msg = msg + "..."
msg += "..."
}
return msg
}
Expand Down
2 changes: 1 addition & 1 deletion checks/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (re *result) tapFormat(num int) string {
}
b, _ := yaml.Marshal(re)
// indent
yamlStr := " " + strings.Replace(strings.TrimSpace(string(b)), "\n", "\n ", -1)
yamlStr := " " + strings.ReplaceAll(strings.TrimSpace(string(b)), "\n", "\n ")
return fmt.Sprintf("%s %d - %s\n ---\n%s\n ...",
okOrNot, num, re.Name, yamlStr)
}
Expand Down
9 changes: 3 additions & 6 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,13 @@ func PrettyPrintJSON(outStream io.Writer, src interface{}, query string) error {
return jq.FilterJSON(outStream, src, query)
}

var angleBracketReplacer = strings.NewReplacer(`\u003c`, "<", `\u003e`, ">")

// JSONMarshalIndent call json.MarshalIndent and replace encoded angle brackets
func JSONMarshalIndent(src interface{}, prefix, indent string) string {
dataRaw, err := json.MarshalIndent(src, prefix, indent)
logger.DieIf(err)
return replaceAngleBrackets(string(dataRaw))
}

func replaceAngleBrackets(s string) string {
s = strings.Replace(s, "\\u003c", "<", -1)
return strings.Replace(s, "\\u003e", ">", -1)
return angleBracketReplacer.Replace(string(dataRaw))
}

// ISO8601Extended format
Expand Down