From e9951d23db7b1cb6f82c89c370eda9f3bd6978b8 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 9 Dec 2015 00:00:20 +0000 Subject: Turn the client into an extensible app, starting with package search --- gentoobrowse-api/client/Jamfile.jam | 5 ++++ gentoobrowse-api/client/main.cpp | 51 +++++++++++++++++++++++-------------- gentoobrowse-api/client/main.h | 15 +++++++++++ gentoobrowse-api/client/search.cpp | 24 +++++++++++++++++ 4 files changed, 76 insertions(+), 19 deletions(-) create mode 100644 gentoobrowse-api/client/main.h create mode 100644 gentoobrowse-api/client/search.cpp diff --git a/gentoobrowse-api/client/Jamfile.jam b/gentoobrowse-api/client/Jamfile.jam index 66165d9..7c3edaa 100644 --- a/gentoobrowse-api/client/Jamfile.jam +++ b/gentoobrowse-api/client/Jamfile.jam @@ -1,7 +1,12 @@ +lib boost_program_options ; + exe gbcli : [ glob *.cpp ] : ../domain//gentoobrowse-domain + ..//adhocutil + ..//boost_system + boost_program_options ../domain//gentoobrowse-domain ../api//gentoobrowse-api ../api//gentoobrowse-api diff --git a/gentoobrowse-api/client/main.cpp b/gentoobrowse-api/client/main.cpp index 61c6575..d2f70bb 100644 --- a/gentoobrowse-api/client/main.cpp +++ b/gentoobrowse-api/client/main.cpp @@ -1,30 +1,43 @@ #include "stdio.h" +#include "main.h" #include +#include #include +#include + +namespace po = boost::program_options; +INSTANTIATEPLUGINOF(Module); int main(int c, char ** v) { - auto ic = Ice::initialize(c, v); - auto p = Gentoo::PortagePrx::checkedCast(ic->stringToProxy("portage:tcp -h localhost -p 9001")); -#if 0 - for (int x = 1; x < c; x += 1) { - auto cat = p->getCategory(atoi(v[x])); - fprintf(stdout, "%d: %s\n", cat->categoryid, cat->name.c_str()); - if (cat->summary) { - fprintf(stdout, "\t%s\n", cat->summary->c_str()); - } - fprintf(stdout, "\n"); - } - auto ps = p->getPackagesSearch(v[1]); - for (const auto & p : ps) { - fprintf(stderr, "%d / %s\n\t%s\n", p->categoryid, p->name.c_str(), p->description.c_str()); - } -#endif - auto fp = p->findPackage(v[1], v[2]); - if (fp) { - fprintf(stderr, "%d / %s\n\t%s\n", (*fp)->categoryid, (*fp)->name.c_str(), (*fp)->description.c_str()); + std::string endpoint; + std::string module; + std::vector args; + po::options_description opts("Gentoo Browse Client options"); + opts.add_options() + ("endpoint", po::value(&endpoint)->default_value("tcp -h gentoobrowse.randomdan.homeip.net -p 9001"), "Service endpoint") + ("module", po::value(&module), "Module") + ("args", po::value(&args), "Module options") + ("help,h", "Show help") + ; + po::positional_options_description pod; + pod.add("module", 1).add("args", -1); + po::variables_map vm; + po::store(po::command_line_parser(c, v).options(opts).positional(pod).run(), vm); + po::notify(vm); + + if (vm.count("help")) { + std::cerr << opts << std::endl; + exit(1); } + + auto ic = Ice::initialize(c, v); + auto p = Gentoo::PortagePrx::checkedCast(ic->stringToProxy("portage:" + endpoint)); + p->ice_ping(); + + AdHoc::PluginManager::getDefault()->get(module)->implementation()->run(p, args); + ic->destroy(); return 0; } diff --git a/gentoobrowse-api/client/main.h b/gentoobrowse-api/client/main.h new file mode 100644 index 0000000..9c7c9f7 --- /dev/null +++ b/gentoobrowse-api/client/main.h @@ -0,0 +1,15 @@ +#ifndef GENTOO_MAIN_H +#define GENTOO_MAIN_H + +#include +#include +#include +#include + +class Module : public AdHoc::AbstractPluginImplementation { + public: + virtual void run(Gentoo::PortagePrx, const std::vector &) const = 0; +}; + +#endif + diff --git a/gentoobrowse-api/client/search.cpp b/gentoobrowse-api/client/search.cpp new file mode 100644 index 0000000..dd4baf1 --- /dev/null +++ b/gentoobrowse-api/client/search.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include "main.h" + +class search : public Module { + public: + void run(Gentoo::PortagePrx p, const std::vector & a) const override + { + auto pkgs = p->getPackagesSearch(boost::algorithm::join(a, " ")); + std::map cats; + fprintbf(stdout, "Found %d packages:\n", pkgs.size()); + for (const auto & pkg : pkgs) { + auto ci = cats.find(pkg->categoryid); + if (ci == cats.end()) { + ci = cats.insert({pkg->categoryid, p->getCategory(pkg->categoryid)}).first; + } + fprintbf(stdout, "%s / %s\n\t%s\n", ci->second->name, pkg->name, pkg->description); + } + } +}; + +PLUGIN(search, Module); + -- cgit v1.2.3