Skip to content

Commit

Permalink
Add option to specify manual port (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatriceVignola authored Feb 15, 2023
1 parent 5628231 commit c5d0fc4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/arg_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define ARG_PARSER_HPP

#include <absl/strings/string_view.h>
#include <absl/types/optional.h>
#include <string>
#include <vector>

Expand All @@ -25,6 +26,7 @@ class ArgParser {
absl::string_view get_help_message() const { return help_message_; };
bool signing_disabled() const { return signing_disabled_; }
std::vector<uint8_t> get_entropy() const;
const absl::optional<uint16_t>& get_port() const { return port_; }

private:
AuthProvider auth_provider_;
Expand All @@ -33,6 +35,7 @@ class ArgParser {
bool signing_disabled_;
bool help_wanted_;
EntropyType entropy_type_;
absl::optional<uint16_t> port_;
};

#endif // ARG_PARSER_HPP
9 changes: 9 additions & 0 deletions src/arg_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ ArgParser::ArgParser(int argc, const char* const* argv) {
"addresses listed.",
cxxopts::value<bool>()->default_value("false"), "");

options.add_option("", "p", "port",
"Port to run the authentication server on. If not "
"provided, a random port will be chosen.",
cxxopts::value<uint16_t>(), "");

options.add_option("", "h", "help", "Print usage", cxxopts::value<bool>(),
"");

Expand Down Expand Up @@ -64,6 +69,10 @@ ArgParser::ArgParser(int argc, const char* const* argv) {
throw std::runtime_error(
absl::StrCat("invalid entropy type `", entropy_type, "`"));
}

if (parse_result.count("port") > 0) {
port_ = parse_result["port"].as<uint16_t>();
}
} catch (const cxxopts::option_not_exists_exception& ex) {
throw std::runtime_error(absl::StrCat(
"error when parsing arguments: ", ex.what(), "\n", help_message_));
Expand Down
2 changes: 2 additions & 0 deletions src/arg_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ static constexpr const char* usage_message = R"(
recommended to verify that the contributions listed
in the transcript are actually generated by the
addresses listed.
-p, --port arg Port to run the authentication server on. If not
provided, a random port will be chosen.
-h, --help Print usage
)";

Expand Down
9 changes: 8 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <cpr/cpr.h>
#include <iostream>

// NOLINTNEXTLINE(readability-function-cognitive-complexity)
int main(int argc, char** argv) {
try {
const ArgParser arg_parser(argc, argv);
Expand All @@ -28,7 +29,13 @@ int main(int argc, char** argv) {
SecretGenerator<> secret_generator(arg_parser.get_entropy(), num_secrets);

const auto& auth_provider = arg_parser.get_auth_provider();
const auto port = port_picker::pick_unused_port();

uint16_t port = 0;
if (arg_parser.get_port().has_value()) {
port = *arg_parser.get_port();
} else {
port = port_picker::pick_unused_port();
}

std::promise<AuthInfo> auth_info_promise;
auto auth_callback = [&auth_info_promise](AuthInfo&& auth_info) {
Expand Down

0 comments on commit c5d0fc4

Please sign in to comment.