enabling <coroutine> from std library for folly when c++ version is 20
[hiphop-php.git] / hphp / runtime / test / watchman-cli-test.cpp
blob0668af308c1f1209bf9805d570bd65250488535a
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source path is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the path LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include <exception>
18 #include <iostream>
19 #include <string>
21 #include <folly/json/dynamic.h>
22 #include <folly/init/Init.h>
23 #include <folly/json/json.h>
25 #include "hphp/runtime/base/watchman.h"
27 #include "hphp/util/optional.h"
29 using namespace HPHP;
31 int main(int argc, char** argv) {
32 folly::Init _{&argc, &argv};
34 if (argc < 2) {
35 std::cout << argv[0] << " <root> [sock]" << std::endl;
36 return 2;
39 std::string root = argv[1];
40 HPHP::Optional<std::string> sock;
41 if (argc > 2) {
42 sock = {{argv[2]}};
45 std::string clock;
47 std::string line;
48 folly::dynamic queryObj;
49 std::cout << "Type a query, or 'b' for a basic query" << std::endl
50 << "Typing 's' will subscribe to the last query sent" << std::endl;
51 while (!std::cin.eof()) {
52 std::getline(std::cin, line);
53 if (line.empty()) {
54 continue;
56 if (line == "b") {
57 queryObj = folly::dynamic::object(
58 "fields", folly::dynamic::array("name", "exists"));
59 } else if (line == "s") {
60 if (!queryObj.isObject()) {
61 std::cout << "No query to subscribe to." << std::endl;
62 continue;
64 Watchman::get(root, sock)
65 ->subscribe(queryObj, [](folly::Try<folly::dynamic>&& results) {
66 if (results.hasValue()) {
67 std::cout << "Subscription got: "
68 << folly::toPrettyJson(results.value()) << std::endl;
69 } else if (results.hasException()) {
70 std::cout << "Subscription error: " << results.exception()
71 << std::endl;
73 });
74 std::cout << "Subscribed using query " << folly::toPrettyJson(queryObj)
75 << std::endl;
76 continue;
77 } else {
78 try {
79 queryObj = folly::parseJson(line);
80 } catch (const std::runtime_error& e) {
81 std::cout << "Couldn't parse " << line << ": " << e.what() << std::endl;
82 continue;
85 if (!clock.empty()) {
86 queryObj["since"] = clock;
88 if (!queryObj.isObject()) {
89 continue;
91 try {
92 folly::dynamic res = Watchman::get(root, sock)->query(queryObj).get();
93 std::cout << folly::toPrettyJson(res) << std::endl;
94 clock = res["clock"].asString();
95 } catch (const folly::AsyncSocketException& e) {
96 std::cout << e.what() << std::endl;