pmt: Write the function base
- Write the function manager. - Make minor changes to the function structure. - Add CLI11 license. - Added main function to libpmt.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Copyright 2025 Yağız Zengin
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <libpmt/lib.hpp>
|
||||
|
||||
namespace PartitionManager {
|
||||
|
||||
void basic_function_manager::registerFunction(std::unique_ptr<basic_function> _func, CLI::App& _app)
|
||||
{
|
||||
LOGN(PMTF, INFO) << "registering function: " << _func->name() << std::endl;
|
||||
if (!_func->init(_app)) throw Error("Cannot init function: %s\n", _func->name());
|
||||
_functions.push_back(std::move(_func));
|
||||
LOGN(PMTF, INFO) << _func->name() << " successfully registered." << std::endl;
|
||||
}
|
||||
|
||||
const char* basic_function_manager::whatIsRunnedCommandName()
|
||||
{
|
||||
for (auto &func : _functions) {
|
||||
if (func->cmd->parsed()) return func->name();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool basic_function_manager::handleAll()
|
||||
{
|
||||
LOGN(PMTF, INFO) << "running catched function commands in command-line." << std::endl;
|
||||
for (auto &func : _functions) {
|
||||
if (func->cmd->parsed()) {
|
||||
LOGN(PMTF, INFO) << "running function: " << func->name() << std::endl;
|
||||
return (func->run()) ? true : false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace PartitionManager
|
||||
|
||||
81
src/Lib.cpp
81
src/Lib.cpp
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
Copyright 2025 Yağız Zengin
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <libpmt/lib.hpp>
|
||||
|
||||
namespace PartitionManager {
|
||||
|
||||
VariableTable::VariableTable() : PartMap(),
|
||||
searchPath(""),
|
||||
onLogical(false),
|
||||
silentProcess(false),
|
||||
verboseMode(false),
|
||||
viewVersion(false)
|
||||
{}
|
||||
|
||||
VariableTable* Variables = new VariableTable();
|
||||
|
||||
int Main(int argc, char** argv)
|
||||
{
|
||||
try { // try-catch start
|
||||
CLI::App AppMain{"Partition Manager Tool"};
|
||||
FunctionManager FuncManager;
|
||||
|
||||
AppMain.add_option("-S,--search-path", Variables->searchPath, "Set partition search path");
|
||||
AppMain.add_flag("-l,--logical", Variables->onLogical, "Specify that the target partition is dynamic");
|
||||
AppMain.add_flag("-s,--silent", Variables->silentProcess, "The process is performed silently without any output.");
|
||||
AppMain.add_flag("-V,--verbose", Variables->verboseMode, "Detailed information is written on the screen while the transaction is being carried out");
|
||||
AppMain.add_flag("-v,--version", Variables->viewVersion, "Print version and exit");
|
||||
|
||||
FuncManager.registerFunction(std::make_unique<backupFunction>, AppMain);
|
||||
FuncManager.registerFunction(std::make_unique<flashFunction>, AppMain);
|
||||
FuncManager.registerFunction(std::make_unique<eraseFunction>, AppMain);
|
||||
FuncManager.registerFunction(std::make_unique<partitionSizeFunction>, AppMain);
|
||||
|
||||
CLI11_PARSE(AppMain, argc, argv);
|
||||
|
||||
const char* used = FuncManager.whatIsRunnedCommandName();
|
||||
if (used != nullptr)
|
||||
LOGN(PMTE, INFO) << "used command: " << used << std::endl;
|
||||
|
||||
if (!Variables->searchPath.empty())
|
||||
Variables->PartMap(searchPath);
|
||||
|
||||
if (!Variables->PartMap) {
|
||||
if (Variables->searchPath.empty())
|
||||
throw Error("No default search entries were found. Specify a search directory with -S (--search-path)");
|
||||
}
|
||||
|
||||
FuncManager.handleAll();
|
||||
|
||||
} catch (Helper::Error &error) { // catch Helper::Error
|
||||
|
||||
if (!Variables->silentProcess) fprintf(stde9rr, "%s: %s.\n", argv[0], error.what());
|
||||
delete Variables;
|
||||
return -1;
|
||||
|
||||
} catch (CLI::ParseError &error) { // catch CLI::ParseError
|
||||
|
||||
fprintf(stderr, "%s: FLAG PARSE ERROR: %s\n", argv[0], error.what());
|
||||
return -1;
|
||||
|
||||
} // try-catch block end
|
||||
}
|
||||
|
||||
} // namespace PartitionManager
|
||||
|
||||
23
src/Main.cpp
23
src/Main.cpp
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
Copyright 2025 Yağız Zengin
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include <libpmt/lib.hpp>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
// Call integrated main function in library
|
||||
return PartitionManager::Main(argc, argv);
|
||||
}
|
||||
|
||||
@@ -21,10 +21,9 @@ namespace PartitionManager {
|
||||
|
||||
// Back-up function
|
||||
class backupFunction : public PartitionManager::FunctionBase {
|
||||
private:
|
||||
CLI::App* _cmd = nullptr;
|
||||
|
||||
public:
|
||||
CLI::App* cmd = nullptr;
|
||||
|
||||
bool init(CLI::App& _app) override;
|
||||
bool run() override;
|
||||
const char* name() override;
|
||||
@@ -32,10 +31,9 @@ public:
|
||||
|
||||
// Image flasher function
|
||||
class flashFunction : public PartitionManager::FunctionBase {
|
||||
private:
|
||||
CLI::App* _cmd = nullptr;
|
||||
|
||||
public:
|
||||
CLI::App* cmd = nullptr;
|
||||
|
||||
bool init(CLI::App& _app) override;
|
||||
bool run() override;
|
||||
const char* name() override;
|
||||
@@ -43,10 +41,9 @@ public:
|
||||
|
||||
// Eraser function (only the partition content is cleared)
|
||||
class eraseFunction : public PartitionManager::FunctionBase {
|
||||
private:
|
||||
CLI::App* _cmd = nullptr;
|
||||
|
||||
public:
|
||||
CLI::App* cmd = nullptr;
|
||||
|
||||
bool init(CLI::App& _app) override;
|
||||
bool run() override;
|
||||
const char* name() override;
|
||||
@@ -54,10 +51,9 @@ public:
|
||||
|
||||
// Partition size getter function
|
||||
class partitionSizeFunction : public PartitionManager::FunctionBase {
|
||||
private:
|
||||
CLI::App* _cmd = nullptr;
|
||||
|
||||
public:
|
||||
CLI::App* cmd = nullptr;
|
||||
|
||||
bool init(CLI::App& _app) override;
|
||||
bool run() override;
|
||||
const char* name() override;
|
||||
|
||||
Reference in New Issue
Block a user