-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis-del.c
77 lines (69 loc) · 2.24 KB
/
redis-del.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "hiredis/hiredis.h"
int main( int argc, char *argv[] ) {
redisReply *reply;
long int i;
char buffer[256];
char* keyString;
char* host;
char* password;
int port;
if (argc < 2) {
printf("Usage: redis-del \"some:key:*\" (host) (port)\n");
exit(1);
} else {
if (argc == 2) {
keyString = argv[1];
host = "127.0.0.1";
port = 6379;
} else if (argc == 3) {
keyString = argv[1];
host = argv[2];
port = 6379;
} else if (argc == 4) {
keyString = argv[1];
host = argv[2];
port = atoi(argv[3]);
}
redisContext *c = redisConnect(host, port);
if (c != NULL && c->err) {
printf("Error: %s\n", c->errstr);
exit(1);
}
char * password = getpass("Password (enter for none): ");
if (strlen(password) > 0) {
reply = redisCommand(c, "AUTH %s", password);
if ( reply->type == REDIS_REPLY_ERROR ) {
printf( "Error: %s\n", reply->str );
freeReplyObject(reply);
exit(1);
}
freeReplyObject(reply);
}
reply = redisCommand(c, "KEYS %s", keyString);
if ( reply->type == REDIS_REPLY_ERROR )
printf( "Error: %s\n", reply->str );
else if ( reply->type != REDIS_REPLY_ARRAY )
printf( "Unexpected type: %d\n", reply->type );
else if (reply->elements > 0) {
printf("%zu matched keys. Delete them? [y/N]: ", reply->elements);
scanf ("%s", buffer);
printf("\n");
if (strcmp(buffer, "y") == 0 || strcmp(buffer, "Y") == 0) {
for ( i=0; i<reply->elements; ++i ){
redisCommand(c, "DEL %s", reply->element[i]->str);
printf( "%lu: %s\n", i, reply->element[i]->str );
}
printf("%li objects removed\n", i);
} else {
printf("Cancelled!\n");
}
} else
printf("No keys matched!\n");
freeReplyObject(reply);
exit(0);
}
}