Skip to content

Commit bf9b9f4

Browse files
Merge pull request #203 from codecrafters-io/eddie/redis-cli-patch
Patch: redis-cli logging
2 parents e316be2 + d8893c1 commit bf9b9f4

File tree

6 files changed

+906
-845
lines changed

6 files changed

+906
-845
lines changed

internal/instrumented_resp_connection/instrumented_resp_connection.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ func defaultCallbacks(logger *logger.Logger) resp_connection.RespConnectionCallb
2626
commandPrefix = "$ redis-cli"
2727
}
2828

29-
if len(args) > 0 {
30-
logger.Infof("%s %s %s", commandPrefix, command, strings.Join(args, " "))
31-
} else {
32-
logger.Infof("%s %s", commandPrefix, command)
33-
}
29+
commandWithArgs := append([]string{command}, args...)
30+
logger.Infof("%s %s", commandPrefix, quoteCLICommand(commandWithArgs))
3431
},
3532
BeforeSendValue: func(value resp_value.Value) {
3633
logger.Infof("Sent %s", value.FormattedString())
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package instrumented_resp_connection
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// quoteIfHasSpaceOrEscapeSequence quotes a string if it contains escapable characters or spaces
9+
func quoteIfHasSpaceOrEscapeSequence(s string) string {
10+
quoted := fmt.Sprintf("%q", s)
11+
12+
// if the string does not change on trimming double quotes, no escapable chracter was present
13+
if (s == strings.Trim(quoted, "\"")) && (!strings.Contains(s, " ")) {
14+
return s
15+
}
16+
17+
return quoted
18+
}
19+
20+
// quoteCLICommand applies quoteIfHasSpaceOrEscapeSequence to each argument in commandWithArgs,
21+
// then joins them into a single space-separated string.
22+
func quoteCLICommand(commandWithArgs []string) string {
23+
result := make([]string, len(commandWithArgs))
24+
for i, a := range commandWithArgs {
25+
result[i] = quoteIfHasSpaceOrEscapeSequence(a)
26+
}
27+
return strings.Join(result, " ")
28+
}

internal/test_cases/subscriber_group_test_case.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (t *SubscriberGroupTestCase) RunSubscribe(logger *logger.Logger) error {
8484
func (t *SubscriberGroupTestCase) RunAssertionForPublishedMessage(channel string, message string, logger *logger.Logger) error {
8585
for _, subscriber := range t.subscribers {
8686
if slices.Contains(subscriber.Channels, channel) {
87-
subscriber.Client.GetLogger().Infof("Expecting published message: (%s)", message)
87+
subscriber.Client.GetLogger().Infof("Expecting published message: %q", message)
8888
receiveTestCase := ReceiveValueTestCase{
8989
Assertion: resp_assertions.NewPublishedMessageAssertion(channel, message),
9090
}

0 commit comments

Comments
 (0)