Merge pull request #20 from lucaspalomodevelop/dev

Dev
This commit is contained in:
Lucas Palomo Develop 2023-12-12 20:55:47 +01:00 committed by GitHub
commit 8a554311d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 152 additions and 13 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "libs/json"]
path = libs/json
url = git@github.com:nlohmann/json.git

1
libs/json Submodule

@ -0,0 +1 @@
Subproject commit 3780b41dd070436f3f55327b0a88f27a52e2dfa8

View File

@ -31,11 +31,36 @@ void input(int argc, char *argv[])
command.addCommandAlias("remove", "r");
command.addCommand("show", "[script] - Shows a script", showScript);
command.addCommandAlias("show", "s");
command.addCommand("config", "<command> - Configures autom", config);
command.addDefaultCommand(runScript);
command.runCommand(argv[1], argc, argv);
}
std::string scriptBuilder(std::string script, std::string args, json script_settings)
{
std::string builded_script = "";
builded_script = script;
if (script_settings.contains("sudo") && script_settings.at("sudo").get<bool>())
builded_script = "sudo " + script;
if (script_settings.contains("pre_script") && script_settings.at("pre_script").size() > 0)
builded_script = script_settings.at("pre_script").get<std::string>() + " && " + builded_script;
if (script_settings.contains("args") && script_settings.at("args").size() > 0)
builded_script = builded_script + " " + script_settings.at("args").get<std::string>();
if (script_settings.contains("background") && script_settings.at("background").get<bool>())
builded_script = builded_script + " &";
std::cout
<< "script: " << builded_script << std::endl;
return builded_script;
}
// run a script with is in the autom directory
void runScript(int argc, char *argv[])
{
@ -89,7 +114,8 @@ void runScript(int argc, char *argv[])
args += argv[i];
args += " ";
}
std::string script = pre_script + dir + "/" + argv[1] + " " + args;
// std::string script = pre_script + dir + "/" + argv[1] + " " + args;
script = scriptBuilder(script, args, script_settings);
std::cout << "executing: " << (dir + "/" + argv[1] + " " + args) << std::endl;
// if (script_settings["sudo"])
@ -98,8 +124,8 @@ void runScript(int argc, char *argv[])
// if (script_settings["background"])
// script = script + " &";
if (script_settings["pre_script"].size() > 0)
system(script_settings["pre_script"].get<std::string>().c_str());
// if (script_settings["pre_script"].size() > 0)
// system(script_settings["pre_script"].get<std::string>().c_str());
system(script.c_str());
return;
@ -107,6 +133,82 @@ void runScript(int argc, char *argv[])
// }
}
void config(int argc, char *argv[])
{
if (argc < 1)
{
std::cout << "Usage: autom config <command>" << std::endl;
return;
}
if (std::string(argv[1]) == "show")
{
std::cout << "Settings:" << std::endl;
std::cout << settings.getSettingsAsString() << std::endl;
}
else if (std::string(argv[1]) == "edit")
{
if (argc > 2)
{
if (std::string(argv[2]) == "editor")
{
std::string editor;
if (argc > 3)
editor = argv[3];
else
{
std::cout << "Enter editor: ";
std::cin >> editor;
}
settings.value["editor"] = editor;
settings.writeSettings();
return;
}
return;
}
else
{
system((std::string(settings.value["editor"]) + " " + settings.filepath).c_str());
}
}
// if (argv[2] == "editor")
// {
// std::cout << "Enter editor: ";
// std::string editor;
// std::cin >> editor;
// settings.value["editor"] = editor;
// settings.save();
// return;
// }
// if (argv[2] == "search_dirs")
// {
// std::cout << "Enter search dirs: ";
// std::string search_dirs;
// std::cin >> search_dirs;
// settings.value["search_dirs"] = search_dirs;
// settings.save();
// return;
// }
// if (argv[2] == "scripts")
// {
// std::cout << "Enter scripts: ";
// std::string scripts;
// std::cin >> scripts;
// settings.value["scripts"] = scripts;
// settings.save();
// return;
// }
// std::cout << "Command " << argv[2] << " does not exist" << std::endl;
}
void showScript(int argc, char *argv[])
{
@ -148,7 +250,6 @@ void showScript(int argc, char *argv[])
std::cout << "Showing script: " << script << std::endl;
if (std::filesystem::exists(script))
{
std::cout << "Showing script: " << argv[1] << std::endl;
std::ifstream file(script);
std::string line;
int line_number = 0;

View File

@ -20,8 +20,12 @@ Command command;
// input function for parsing arguments and creating commands and running them
void input(int argc, char *argv[]);
// build a script
std::string scriptBuilder(std::string pre_script, std::string script_name, std::string args, json script_settings);
// run a script with is in the autom directory
void runScript(int argc, char *argv[]);
// config function for configuring autom
void config(int argc, char *argv[]);
// list all scripts in the autom directory
void listScripts(int argc, char *argv[]);
// add a script in the autom directory

View File

@ -7,6 +7,7 @@ Setup setup;
Settings::Settings(void)
{
setup = Setup();
filepath = setup.home + "/.automconfig.json";
readSettings();
}
@ -16,7 +17,7 @@ Settings::~Settings(void)
void Settings::readSettings()
{
std::ifstream file(setup.home + "/.automconfig.json");
std::ifstream file(filepath);
if (!file.is_open())
{
std::cout << "Error:" + setup.home + "/.automconfig.json not found" << std::endl;
@ -25,3 +26,32 @@ void Settings::readSettings()
}
this->value = json::parse(file);
}
std::string Settings::getSettingsAsString()
{
return this->value.dump(4);
}
void Settings::writeSettings()
{
std::ofstream file(setup.home + "/.automconfig.json");
file << this->value.dump(4);
file.close();
}
// void Settings::set(std::string key, std::string value)
// {
// this->value[key] = value;
// writeSettings();
// }
// std::string Settings::get(std::string key)
// {
// return this->value[key];
// }
// void Settings::set(std::string key, int value)
// {
// this->value[key] = value;
// writeSettings();
// }

View File

@ -3,7 +3,7 @@
#ifndef SETTINGS_H
#define SETTINGS_H
#include <nlohmann/json.hpp>
#include "../libs/json/single_include/nlohmann/json.hpp"
using json = nlohmann::json;
@ -26,9 +26,12 @@ private:
public:
json value;
std::string filepath;
Settings(void);
~Settings(void);
void readSettings();
void writeSettings();
std::string getSettingsAsString();
Settings &operator=(const Settings &) = default;
};

View File

@ -9,7 +9,7 @@
#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
#include "../libs/json/single_include/nlohmann/json.hpp"
using json = nlohmann::json;
class Setup
@ -48,19 +48,16 @@ public:
{
std::replace(home.begin(), home.end(), '\\', '/');
std::ofstream file(home + "/.automconfig.json");
json j = {
{"editor", editor},
{"search_dirs", {home}},
{"autom_home_dir", home}
};
{"autom_home_dir", home}};
file << j.dump(4);
file.close();
}
void runSetup()