proxygen
FizzCommandCommon.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
10 #include <folly/String.h>
11 
12 using namespace folly;
13 
14 namespace fizz {
15 namespace tool {
16 
18  std::vector<std::string> argv,
19  FizzArgHandlerMap handlers,
20  std::function<void()> usageFunc) {
21  for (size_t idx = 2; idx < argv.size(); idx++) {
22  auto& argument = argv[idx];
23  auto handlerIter = handlers.find(argument);
24 
25  if (handlerIter != handlers.end()) {
26  auto& handlerInfo = handlerIter->second;
27  std::string variable;
28  if (handlerInfo.hasVariable) {
29  if (idx + 1 >= argv.size()) {
30  std::cerr << "Argument " << argument << " requires an parameter."
31  << std::endl;
32  usageFunc();
33  return 1;
34  } else {
35  idx++;
36  variable = argv[idx];
37  }
38  }
39  handlerInfo.handler(variable);
40  } else {
41  std::cerr << "Unknown argument: " << argument << std::endl;
42  usageFunc();
43  return 1;
44  }
45  }
46  return 0;
47 }
48 
49 TerminalInputHandler::TerminalInputHandler(
50  EventBase* evb,
52  : EventHandler(evb, 0), cb_(cb), evb_(evb) {
53  registerHandler(EventHandler::READ | EventHandler::PERSIST);
54 }
55 
57  // Handle read ready on stdin, but only once we're connected.
58  if (events & EventHandler::READ && cb_->connected()) {
59  std::array<char, 512> buf;
60  int result = read(0, buf.data(), buf.size());
61 
62  if (result > 0) {
63  cb_->write(IOBuf::wrapBuffer(buf.data(), result));
64  } else {
65  if (result < 0) {
66  LOG(ERROR) << "Error on terminal read: " << folly::errnoStr(errno);
67  }
68  hitEOF();
69  }
70  }
71 }
72 
74  evb_->runInLoop([cb_ = cb_]() { cb_->close(); });
75 }
76 
77 } // namespace tool
78 } // namespace fizz
virtual bool connected() const =0
void handlerReady(uint16_t events) noexceptoverride
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
int parseArguments(std::vector< std::string > argv, FizzArgHandlerMap handlers, std::function< void()> usageFunc)
char ** argv
void runInLoop(LoopCallback *callback, bool thisIteration=false)
Definition: EventBase.cpp:520
EventBase * evb_
std::unique_ptr< AsyncFizzServer::HandshakeCallback > cb_
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
Definition: Actions.h:16
virtual void write(std::unique_ptr< folly::IOBuf > msg)=0
fbstring errnoStr(int err)
Definition: String.cpp:463
const char * string
Definition: Conv.cpp:212
std::map< std::string, FizzCommandArgHandlerInfo > FizzArgHandlerMap
bool registerHandler(uint16_t events)
Definition: EventHandler.h:100