Closed zuiderkwast closed 5 months ago
I invoked the script with the filenames of all files containing "```cli" and passed them to stdin of the following script:
#!/usr/bin/perl
# Pipe the filenames to this script, e.g.
# git grep -l -P '```cli$' | tmp/replace-cli-blocks.pl
use warnings;
use strict;
my $cli = "/home/viktor/repos/valkey/src/valkey-cli";
my $prompt = "127.0.0.1:6379> ";
while (<>) {
next unless m!(?:commands|topics)/[^/]*\.md$!;
chomp;
my $filename = $_;
my $in_session = 0;
open(my $in, "<", $filename) or die "Can't open $filename: $!";
open(my $out, ">", "$filename~~~") or die "Can't create $filename~~~: $!";
while (<$in>) {
if ($in_session) {
if (/^```\s*$/) {
$in_session = 0;
print $out "```\n";
} else {
chomp;
my $command = $_;
s/'/'\\''/;
my $response = `echo '$_' | $cli --no-raw`;
if ($?) {
print "Error $?\n Command: $_\n Reply: $response\n";
}
print $out "$prompt$command\n$response";
}
} elsif (/^```cli\s*$/) {
$in_session = 1;
print $out "```valkey-cli\n";
`$cli flushall`;
} else {
print $out $_;
}
}
close $in;
close $out;
rename "$filename~~~", $filename;
}
`$cli flushall`;
Replace code blocks without replies, such as ...
... with proper valkey-cli sessions including prompt and reply, like this:
This was automated with a script actually running
valkey-cli
for each command withflushall
between the examples to make sure each example starts clean.Additionally: Replace
@examples
with a proper markdown heading## Examples
.