move repository from m6w6 to awesomized
[m6w6/libmemcached] / src / bin / memrm.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020-2021 Michael Wallner https://awesome.co/ |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "mem_config.h"
17
18 #define PROGRAM_NAME "memrm"
19 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
20 #define PROGRAM_VERSION "1.1"
21
22 #include "common/options.hpp"
23 #include "common/checks.hpp"
24
25 int main(int argc, char *argv[]) {
26 client_options opt{PROGRAM_NAME, PROGRAM_VERSION, PROGRAM_DESCRIPTION, "key [key ...]"};
27
28 for (const auto &def : opt.defaults) {
29 opt.add(def);
30 }
31
32 opt.add("expire", 'e', required_argument, "Remove based on expire time. DEPRECATED: has no effect.");
33
34 char **argp = nullptr;
35 if (!opt.parse(argc, argv, &argp)) {
36 exit(EXIT_FAILURE);
37 }
38
39 memcached_st memc;
40 if (!check_memcached(opt, memc)) {
41 exit(EXIT_FAILURE);
42 }
43
44 if (!opt.apply(&memc)) {
45 memcached_free(&memc);
46 exit(EXIT_FAILURE);
47 }
48
49 if (!check_argp(opt, argp, "No key(s) provided.")){
50 memcached_free(&memc);
51 exit(EXIT_FAILURE);
52 }
53
54 time_t expire = 0;
55 if (auto exp_str = opt.argof("expire")) {
56 expire = std::stoul(exp_str);
57 }
58
59 auto exit_code = EXIT_SUCCESS;
60 for (auto arg = argp; *arg; ++arg) {
61 auto key = *arg;
62 if (*key) {
63 if (!check_return(opt, memc, key, memcached_delete(&memc, key, strlen(key), expire))) {
64 exit_code = EXIT_FAILURE;
65 }
66 }
67 }
68
69 if (!check_buffering(opt, memc)) {
70 exit_code = EXIT_FAILURE;
71 }
72
73 memcached_free(&memc);
74 exit(exit_code);
75 }