From b0376d4a2db3f59a024b923a7e1773e40364672e Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Wed, 4 Jan 2023 22:05:39 +0100 Subject: [PATCH] provider: parse command line options --- CMakeLists.txt | 3 + src/provider_main.cpp | 146 +++++++++++++++++++++++++++++++++---- src/webfuse/version.cpp.in | 11 +++ src/webfuse/version.hpp | 12 +++ 4 files changed, 157 insertions(+), 15 deletions(-) create mode 100644 src/webfuse/version.cpp.in create mode 100644 src/webfuse/version.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index de6f94a..209bdda 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,10 @@ find_package(PkgConfig REQUIRED) pkg_check_modules(FUSE REQUIRED IMPORTED_TARGET fuse3) pkg_check_modules(LWS REQUIRED IMPORTED_TARGET libwebsockets) +configure_file(src/webfuse/version.cpp.in version.cpp) + add_library(webfuse_static STATIC + ${CMAKE_CURRENT_BINARY_DIR}/version.cpp src/webfuse/webfuse.cpp src/webfuse/provider.cpp src/webfuse/fuse.cpp diff --git a/src/provider_main.cpp b/src/provider_main.cpp index d8fa8eb..c869e12 100644 --- a/src/provider_main.cpp +++ b/src/provider_main.cpp @@ -1,17 +1,115 @@ #include "webfuse/provider.hpp" +#include "webfuse/version.hpp" #include #include #include +#include #include #include 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 [-p ] + +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) _; @@ -186,21 +284,39 @@ private: int main(int argc, char* argv[]) { - signal(SIGINT, &on_signal); - signal(SIGTERM, &on_signal); + context ctx(argc, argv); - filesystem fs("."); - webfuse::provider provider(fs); - provider.set_connection_listener([](bool connected) { - if (!connected) - { - shutdown_requested = true; - } - }); - provider.connect("ws://localhost:8080/"); - while (!shutdown_requested) + switch (ctx.cmd) { - provider.service(); + case command::run: + { + signal(SIGINT, &on_signal); + signal(SIGTERM, &on_signal); + + filesystem fs(ctx.base_path); + webfuse::provider provider(fs); + provider.set_connection_listener([](bool connected) { + if (!connected) + { + shutdown_requested = true; + } + }); + provider.connect(ctx.url); + while (!shutdown_requested) + { + provider.service(); + } + } + break; + case command::show_version: + print_version(); + break; + case command::show_help: + // fall-through + default: + print_usage(); + break; } - return EXIT_SUCCESS; + + return ctx.exit_code; } \ No newline at end of file diff --git a/src/webfuse/version.cpp.in b/src/webfuse/version.cpp.in new file mode 100644 index 0000000..b9f7137 --- /dev/null +++ b/src/webfuse/version.cpp.in @@ -0,0 +1,11 @@ +#include "webfuse/version.hpp" + +namespace webfuse +{ + +char const * get_version() +{ + return "@webfuse_VERSION@"; +} + +} \ No newline at end of file diff --git a/src/webfuse/version.hpp b/src/webfuse/version.hpp new file mode 100644 index 0000000..ad540a8 --- /dev/null +++ b/src/webfuse/version.hpp @@ -0,0 +1,12 @@ +#ifndef WEBFUSE_VERSION_HPP +#define WEBFUSE_VERSION_HPP + +namespace webfuse +{ + +char const * get_version(); + + +} + +#endif