valkey-io / valkey-doc

Content for website and man pages
Other
21 stars 31 forks source link

Insert command replies in CLI examples where missing #77

Closed zuiderkwast closed 5 months ago

zuiderkwast commented 5 months ago

Replace code blocks without replies, such as ...

```cli
EXISTS mykey
APPEND mykey "Hello"
APPEND mykey " World"
GET mykey
```

... with proper valkey-cli sessions including prompt and reply, like this:

```valkey-cli
127.0.0.1:6379> EXISTS mykey
(integer) 0
127.0.0.1:6379> APPEND mykey "Hello"
(integer) 5
127.0.0.1:6379> APPEND mykey " World"
(integer) 11
127.0.0.1:6379> GET mykey
"Hello World"
```

This was automated with a script actually running valkey-cli for each command with flushall between the examples to make sure each example starts clean.

Additionally: Replace @examples with a proper markdown heading ## Examples.

zuiderkwast commented 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`;