parent
e9826fd0ef
commit
412c1f9a51
@ -0,0 +1,56 @@
|
||||
#include "webfuse/util/commandline_args.hpp"
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace webfuse
|
||||
{
|
||||
|
||||
commandline_args::commandline_args(char const * prog_name, int capacity)
|
||||
: capacity_(capacity)
|
||||
, argc(0)
|
||||
{
|
||||
if (capacity < 1)
|
||||
{
|
||||
throw std::runtime_error("too few commandline args");
|
||||
}
|
||||
|
||||
argv = new char*[capacity_ + 1];
|
||||
argv[0] = nullptr;
|
||||
push(prog_name);
|
||||
}
|
||||
|
||||
commandline_args::~commandline_args()
|
||||
{
|
||||
for(int i = 0; i < argc; i++)
|
||||
{
|
||||
free(argv[i]);
|
||||
}
|
||||
delete[] argv;
|
||||
}
|
||||
|
||||
void commandline_args::push(char const * arg)
|
||||
{
|
||||
if (argc < capacity_)
|
||||
{
|
||||
argv[argc] = strdup(arg);
|
||||
argv[argc + 1] = nullptr;
|
||||
argc++;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::runtime_error("capacity exceeded");
|
||||
}
|
||||
}
|
||||
|
||||
int commandline_args::get_argc() const
|
||||
{
|
||||
return argc;
|
||||
}
|
||||
|
||||
char ** commandline_args::get_argv()
|
||||
{
|
||||
return argv;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
#ifndef WEBFUSE_COMMANDLINE_ARGS_HPP
|
||||
#define WEBFUSE_COMMANDLINE_ARGS_HPP
|
||||
|
||||
namespace webfuse
|
||||
{
|
||||
|
||||
class commandline_args
|
||||
{
|
||||
commandline_args (commandline_args const &) = delete;
|
||||
commandline_args& operator=(commandline_args const &) = delete;
|
||||
commandline_args (commandline_args &&) = delete;
|
||||
commandline_args& operator=(commandline_args &&) = delete;
|
||||
public:
|
||||
commandline_args(char const * prog_name, int capacity);
|
||||
~commandline_args();
|
||||
|
||||
void push(char const * arg);
|
||||
int get_argc() const;
|
||||
char ** get_argv();
|
||||
|
||||
private:
|
||||
int capacity_;
|
||||
int argc;
|
||||
char ** argv;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,30 @@
|
||||
#include "webfuse/util/commandline_reader.hpp"
|
||||
namespace webfuse
|
||||
{
|
||||
|
||||
commandline_reader::commandline_reader(int argc, char * argv[])
|
||||
: current_(0)
|
||||
, argc_(argc)
|
||||
, argv_(argv)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool commandline_reader::next()
|
||||
{
|
||||
if (current_ < argc_)
|
||||
{
|
||||
current_++;
|
||||
}
|
||||
|
||||
bool const has_next = (current_ < argc_);
|
||||
return has_next;
|
||||
}
|
||||
|
||||
char const * commandline_reader::current() const
|
||||
{
|
||||
return argv_[current_];
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
#ifndef WEBFUSE_COMMANDLINE_READER_HPP
|
||||
#define WEBFUSE_COMMANDLINE_READER_HPP
|
||||
|
||||
namespace webfuse
|
||||
{
|
||||
|
||||
class commandline_reader
|
||||
{
|
||||
public:
|
||||
commandline_reader(int argc, char * argv[]);
|
||||
~commandline_reader() = default;
|
||||
bool next();
|
||||
char const * current() const;
|
||||
private:
|
||||
int current_;
|
||||
int argc_;
|
||||
char ** argv_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -1,19 +1,99 @@
|
||||
#include "webfuse/ws/config.hpp"
|
||||
#include "webfuse/util/commandline_reader.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
constexpr int const default_port = 8081;
|
||||
constexpr char const default_vhost_name[] = "localhost";
|
||||
|
||||
void verify(webfuse::ws_config & config)
|
||||
{
|
||||
if (config.cmd == webfuse::command::run)
|
||||
{
|
||||
if ((config.use_tls) && ((config.key_path.empty()) || (config.cert_path.empty())))
|
||||
{
|
||||
std::cerr << "error: use of TLS requires both, key ander certificate path" << std::endl;
|
||||
config.cmd = webfuse::command::show_help;
|
||||
config.exit_code = EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool get_arg(webfuse::ws_config & config, webfuse::commandline_reader& reader, std::string & value, std::string const error_message)
|
||||
{
|
||||
const bool has_next = reader.next();
|
||||
if (has_next)
|
||||
{
|
||||
value = reader.current();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "error: " << error_message << std::endl;
|
||||
config.cmd = webfuse::command::show_help;
|
||||
config.exit_code = EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return has_next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace webfuse
|
||||
{
|
||||
|
||||
ws_config::ws_config()
|
||||
: port(default_port)
|
||||
ws_config::ws_config(int argc, char * argv[])
|
||||
: exit_code(EXIT_SUCCESS)
|
||||
, args(argv[0], argc)
|
||||
, cmd(command::run)
|
||||
, port(default_port)
|
||||
, vhost_name(default_vhost_name)
|
||||
, use_tls(false)
|
||||
{
|
||||
commandline_reader reader(argc, argv);
|
||||
|
||||
while ((exit_code == EXIT_SUCCESS) && (reader.next()))
|
||||
{
|
||||
std::string const arg = reader.current();
|
||||
if ((arg == "-h") || (arg == "--help"))
|
||||
{
|
||||
cmd = command::show_help;
|
||||
}
|
||||
else if (arg == "--wf-port")
|
||||
{
|
||||
std::string value;
|
||||
if (get_arg(*this, reader, value, "missing PORT"))
|
||||
{
|
||||
port = static_cast<uint16_t>(std::stoi(value));
|
||||
}
|
||||
}
|
||||
else if (arg == "--wf-vhost")
|
||||
{
|
||||
get_arg(*this, reader, vhost_name, "missing VHOST");
|
||||
}
|
||||
else if (arg == "--wf-key")
|
||||
{
|
||||
if (get_arg(*this, reader, key_path, "missing KEY_PATH"))
|
||||
{
|
||||
use_tls = true;
|
||||
}
|
||||
}
|
||||
else if (arg == "--wf-cert")
|
||||
{
|
||||
if (get_arg(*this, reader, cert_path, "missing CERT_PATH"))
|
||||
{
|
||||
use_tls = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
args.push(arg.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
verify(*this);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in new issue