Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #ifndef FACTORY_HPP
10 : #define FACTORY_HPP
11 :
12 : #include <map>
13 : #include <memory>
14 : #include <stdexcept>
15 : #include <string>
16 : #include <vector>
17 :
18 : #include "ansicolors.hpp"
19 : #include "coloredkdbio.hpp"
20 :
21 : #include <command.hpp>
22 : #include <external.hpp>
23 :
24 : // TODO: to add a new command, 1.) include your header here
25 : #include <cache.hpp>
26 : #include <check.hpp>
27 : #include <cmerge.hpp>
28 : #include <complete.hpp>
29 : #include <convert.hpp>
30 : #include <cp.hpp>
31 : #include <editor.hpp>
32 : #include <export.hpp>
33 : #include <file.hpp>
34 : #include <find.hpp>
35 : #include <fstab.hpp>
36 : #include <gen.hpp>
37 : #include <get.hpp>
38 : #include <globalmount.hpp>
39 : #include <globalumount.hpp>
40 : #include <import.hpp>
41 : #include <info.hpp>
42 : #include <list.hpp>
43 : #include <listcommands.hpp>
44 : #include <ls.hpp>
45 : #include <memory>
46 : #include <merge.hpp>
47 : #include <metaget.hpp>
48 : #include <metals.hpp>
49 : #include <metaremove.hpp>
50 : #include <metaset.hpp>
51 : #include <mount.hpp>
52 : #include <mv.hpp>
53 : #include <remount.hpp>
54 : #include <rm.hpp>
55 : #include <set.hpp>
56 : #include <sget.hpp>
57 : #include <shell.hpp>
58 : #include <specmount.hpp>
59 : #include <test.hpp>
60 : #include <umount.hpp>
61 : #include <validation.hpp>
62 :
63 : class Instancer
64 : {
65 : public:
66 : virtual std::unique_ptr<Command> get () = 0;
67 : virtual ~Instancer ()
68 : {
69 : }
70 : };
71 :
72 : template <class T>
73 279396 : class Cnstancer : public Instancer
74 : {
75 6544 : virtual std::unique_ptr<Command> get () override
76 : {
77 13088 : return std::unique_ptr<Command> (new T ());
78 : }
79 : };
80 :
81 7164 : class Factory
82 : {
83 : std::map<std::string, std::shared_ptr<Instancer>> m_factory;
84 :
85 : public:
86 7164 : Factory () : m_factory ()
87 : {
88 : // TODO: to add a new command, 2.) add a line here -> and you are done
89 14328 : m_factory.insert (std::make_pair ("get", std::make_shared<Cnstancer<GetCommand>> ()));
90 14328 : m_factory.insert (std::make_pair ("set", std::make_shared<Cnstancer<SetCommand>> ()));
91 14328 : m_factory.insert (std::make_pair ("rm", std::make_shared<Cnstancer<RemoveCommand>> ()));
92 14328 : m_factory.insert (std::make_pair ("ls", std::make_shared<Cnstancer<LsCommand>> ()));
93 10746 : m_factory.insert (std::make_pair ("cache", std::make_shared<Cnstancer<CacheCommand>> ()));
94 14328 : m_factory.insert (std::make_pair ("complete", std::make_shared<Cnstancer<CompleteCommand>> ()));
95 14328 : m_factory.insert (std::make_pair ("cp", std::make_shared<Cnstancer<CpCommand>> ()));
96 14328 : m_factory.insert (std::make_pair ("mv", std::make_shared<Cnstancer<MvCommand>> ()));
97 14328 : m_factory.insert (std::make_pair ("mount", std::make_shared<Cnstancer<MountCommand>> ()));
98 14328 : m_factory.insert (std::make_pair ("remount", std::make_shared<Cnstancer<RemountCommand>> ()));
99 14328 : m_factory.insert (std::make_pair ("shell", std::make_shared<Cnstancer<ShellCommand>> ()));
100 14328 : m_factory.insert (std::make_pair ("find", std::make_shared<Cnstancer<FindCommand>> ()));
101 14328 : m_factory.insert (std::make_pair ("getmeta", std::make_shared<Cnstancer<MetaGetCommand>> ()));
102 14328 : m_factory.insert (std::make_pair ("rmmeta", std::make_shared<Cnstancer<MetaRemoveCommand>> ()));
103 14328 : m_factory.insert (std::make_pair ("setmeta", std::make_shared<Cnstancer<MetaSetCommand>> ()));
104 14328 : m_factory.insert (std::make_pair ("lsmeta", std::make_shared<Cnstancer<MetaLsCommand>> ()));
105 14328 : m_factory.insert (std::make_pair ("info", std::make_shared<Cnstancer<InfoCommand>> ()));
106 14328 : m_factory.insert (std::make_pair ("test", std::make_shared<Cnstancer<TestCommand>> ()));
107 14328 : m_factory.insert (std::make_pair ("check", std::make_shared<Cnstancer<CheckCommand>> ()));
108 14328 : m_factory.insert (std::make_pair ("vset", std::make_shared<Cnstancer<ValidationCommand>> ()));
109 14328 : m_factory.insert (std::make_pair ("fstab", std::make_shared<Cnstancer<FstabCommand>> ()));
110 14328 : m_factory.insert (std::make_pair ("export", std::make_shared<Cnstancer<ExportCommand>> ()));
111 14328 : m_factory.insert (std::make_pair ("import", std::make_shared<Cnstancer<ImportCommand>> ()));
112 14328 : m_factory.insert (std::make_pair ("convert", std::make_shared<Cnstancer<ConvertCommand>> ()));
113 14328 : m_factory.insert (std::make_pair ("umount", std::make_shared<Cnstancer<UmountCommand>> ()));
114 14328 : m_factory.insert (std::make_pair ("file", std::make_shared<Cnstancer<FileCommand>> ()));
115 14328 : m_factory.insert (std::make_pair ("sget", std::make_shared<Cnstancer<ShellGetCommand>> ()));
116 14328 : m_factory.insert (std::make_pair ("merge", std::make_shared<Cnstancer<MergeCommand>> ()));
117 14328 : m_factory.insert (std::make_pair ("cmerge", std::make_shared<Cnstancer<CMergeCommand>> ()));
118 14328 : m_factory.insert (std::make_pair ("list", std::make_shared<Cnstancer<ListCommand>> ()));
119 14328 : m_factory.insert (std::make_pair ("editor", std::make_shared<Cnstancer<EditorCommand>> ()));
120 14328 : m_factory.insert (std::make_pair ("spec-mount", std::make_shared<Cnstancer<SpecMountCommand>> ()));
121 14328 : m_factory.insert (std::make_pair ("smount", std::make_shared<Cnstancer<SpecMountCommand>> ()));
122 14328 : m_factory.insert (std::make_pair ("global-mount", std::make_shared<Cnstancer<GlobalMountCommand>> ()));
123 14328 : m_factory.insert (std::make_pair ("global-umount", std::make_shared<Cnstancer<GlobalUmountCommand>> ()));
124 14328 : m_factory.insert (std::make_pair ("gmount", std::make_shared<Cnstancer<GlobalMountCommand>> ()));
125 14328 : m_factory.insert (std::make_pair ("gumount", std::make_shared<Cnstancer<GlobalUmountCommand>> ()));
126 14328 : m_factory.insert (std::make_pair ("list-commands", std::make_shared<Cnstancer<ListCommandsCommand>> ()));
127 14328 : m_factory.insert (std::make_pair ("gen", std::make_shared<Cnstancer<GenCommand>> ()));
128 3582 : }
129 :
130 78 : std::vector<std::string> getPrettyCommands () const
131 : {
132 78 : std::vector<std::string> ret;
133 3276 : for (auto & elem : m_factory)
134 : {
135 6084 : std::string text = getStdColor (ANSI_COLOR::BOLD);
136 6084 : text += elem.first;
137 9126 : text += getStdColor (ANSI_COLOR::RESET);
138 3042 : text += "\t";
139 6084 : CommandPtr cmd = elem.second->get ();
140 9126 : text += cmd->getShortHelpText ();
141 3042 : ret.push_back (text);
142 : }
143 546 : ret.push_back (getStdColor (ANSI_COLOR::BOLD) + "help" + getStdColor (ANSI_COLOR::RESET) + "\t" +
144 78 : "View the man page of a tool");
145 546 : ret.push_back (getStdColor (ANSI_COLOR::BOLD) + "list-tools" + getStdColor (ANSI_COLOR::RESET) + "\t" +
146 78 : "List all external tools");
147 78 : return ret;
148 : }
149 :
150 : /**Returns a list of available commands */
151 0 : std::vector<std::string> getCommands () const
152 : {
153 0 : std::vector<std::string> ret;
154 0 : for (auto & elem : m_factory)
155 : {
156 0 : ret.push_back (elem.first);
157 : }
158 0 : ret.push_back ("help");
159 0 : ret.push_back ("list-tools");
160 0 : return ret;
161 : }
162 :
163 3580 : CommandPtr get (std::string const & which)
164 : {
165 10740 : std::shared_ptr<Instancer> instancer = m_factory[which];
166 3580 : if (instancer)
167 : {
168 7004 : CommandPtr ret (instancer->get ());
169 3502 : return ret;
170 : }
171 : else
172 : {
173 156 : m_factory.erase (which);
174 78 : return CommandPtr (new ExternalCommand ());
175 : }
176 : }
177 : };
178 :
179 :
180 : #endif
|