1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00

provider: parse command line options

This commit is contained in:
Falk Werner 2023-01-04 22:05:39 +01:00
parent ae668d47ba
commit b0376d4a2d
4 changed files with 157 additions and 15 deletions

View File

@ -7,7 +7,10 @@ find_package(PkgConfig REQUIRED)
pkg_check_modules(FUSE REQUIRED IMPORTED_TARGET fuse3) pkg_check_modules(FUSE REQUIRED IMPORTED_TARGET fuse3)
pkg_check_modules(LWS REQUIRED IMPORTED_TARGET libwebsockets) pkg_check_modules(LWS REQUIRED IMPORTED_TARGET libwebsockets)
configure_file(src/webfuse/version.cpp.in version.cpp)
add_library(webfuse_static STATIC add_library(webfuse_static STATIC
${CMAKE_CURRENT_BINARY_DIR}/version.cpp
src/webfuse/webfuse.cpp src/webfuse/webfuse.cpp
src/webfuse/provider.cpp src/webfuse/provider.cpp
src/webfuse/fuse.cpp src/webfuse/fuse.cpp

View File

@ -1,17 +1,115 @@
#include "webfuse/provider.hpp" #include "webfuse/provider.hpp"
#include "webfuse/version.hpp"
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <dirent.h> #include <dirent.h>
#include <getopt.h>
#include <csignal> #include <csignal>
#include <iostream> #include <iostream>
namespace namespace
{ {
static bool shutdown_requested = false; enum class command
{
run,
show_help,
show_version
};
class context
{
public:
context(int argc, char* argv[])
: base_path(".")
, url("")
, cmd(command::run)
, exit_code()
{
struct option const long_options[] =
{
{"path" , required_argument, nullptr, 'p'},
{"url" , required_argument, nullptr, 'u'},
{"version", no_argument , nullptr, 'v'},
{"help" , no_argument , nullptr, 'h'},
{nullptr , 0 , nullptr, 0 }
};
optind = 0;
opterr = 0;
bool finished = false;
while (!finished)
{
int option_index = 0;
const int c = getopt_long(argc, argv, "p:u:vh", long_options, &option_index);
switch (c)
{
case -1:
finished = true;
break;
case 'p':
base_path = optarg;
break;
case 'u':
url = optarg;
break;
case 'h':
cmd = command::show_help;
break;
case 'v':
cmd = command::show_version;
break;
default:
std::cerr << "error: unknown option" << std::endl;
cmd = command::show_help;
exit_code = EXIT_FAILURE;
finished = true;
break;
}
}
if ((cmd == command::run) && (url.empty()))
{
std::cerr << "error: missing url" << std::endl;
cmd = command::show_help;
exit_code = EXIT_FAILURE;
}
}
std::string base_path;
std::string url;
command cmd;
int exit_code;
};
void print_usage()
{
std::cout << R"(webfuse2 provider, (c) 2022 by Falk Werner
expose a local directory via webfuse2
Usage:
webfuse-provider -u <url> [-p <path>]
Options:
--url, -u set url of webfuse2 service
--path, -p set path of directory to expose (default: .)
--version, -v print version and quit
--help, -h print this message and quit
Examples:
webfuse-provider -u ws://localhost:8080/
webfuse-provider -u ws://localhost:8080/ -p /some/directory
)";
}
void print_version()
{
std::cout << webfuse::get_version() << std::endl;
}
static bool shutdown_requested = false;
void on_signal(int _) void on_signal(int _)
{ {
(void) _; (void) _;
@ -185,11 +283,17 @@ private:
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{
context ctx(argc, argv);
switch (ctx.cmd)
{
case command::run:
{ {
signal(SIGINT, &on_signal); signal(SIGINT, &on_signal);
signal(SIGTERM, &on_signal); signal(SIGTERM, &on_signal);
filesystem fs("."); filesystem fs(ctx.base_path);
webfuse::provider provider(fs); webfuse::provider provider(fs);
provider.set_connection_listener([](bool connected) { provider.set_connection_listener([](bool connected) {
if (!connected) if (!connected)
@ -197,10 +301,22 @@ int main(int argc, char* argv[])
shutdown_requested = true; shutdown_requested = true;
} }
}); });
provider.connect("ws://localhost:8080/"); provider.connect(ctx.url);
while (!shutdown_requested) while (!shutdown_requested)
{ {
provider.service(); provider.service();
} }
return EXIT_SUCCESS; }
break;
case command::show_version:
print_version();
break;
case command::show_help:
// fall-through
default:
print_usage();
break;
}
return ctx.exit_code;
} }

View File

@ -0,0 +1,11 @@
#include "webfuse/version.hpp"
namespace webfuse
{
char const * get_version()
{
return "@webfuse_VERSION@";
}
}

12
src/webfuse/version.hpp Normal file
View File

@ -0,0 +1,12 @@
#ifndef WEBFUSE_VERSION_HPP
#define WEBFUSE_VERSION_HPP
namespace webfuse
{
char const * get_version();
}
#endif