...
This commit is contained in:
140
src/main.cpp
140
src/main.cpp
@ -63,23 +63,28 @@ std::string safearg(int argc, char *argv[], int index)
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
try {
|
||||
// Load configuration
|
||||
if (!dropshell::load_config()) {
|
||||
std::cerr << "Error: Failed to load configuration" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// No arguments provided
|
||||
if (argc < 2) {
|
||||
dropshell::interactive_mode();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Handle commands
|
||||
std::string cmd = argv[1];
|
||||
std::string cmd;
|
||||
if (argc > 1) {
|
||||
cmd = argv[1];
|
||||
}
|
||||
|
||||
// don't load old config if we're initializing
|
||||
if (cmd == "init") {
|
||||
if (argc < 3) {
|
||||
std::cerr << "Error: init command requires a directory argument" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
try {
|
||||
dropshell::init_user_directory(argv[2]);
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
auto commands = dropshell::autocomplete_list_commands();
|
||||
|
||||
if (cmd == "help" || cmd == "-h" || cmd == "--help") {
|
||||
dropshell::print_help();
|
||||
return 0;
|
||||
@ -90,6 +95,70 @@ int main(int argc, char* argv[]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// silently attempt to load the config file.
|
||||
dropshell::load_config();
|
||||
|
||||
|
||||
// auto compeltion stuff.
|
||||
auto commands = dropshell::autocomplete_list_commands();
|
||||
if (cmd == "autocomplete_list_commands") {
|
||||
// add in standard commands.
|
||||
commands.insert(commands.end(), {
|
||||
"help",
|
||||
"version",
|
||||
"init"
|
||||
});
|
||||
if (dropshell::is_config_loaded()) { // these only work if the config is loaded.
|
||||
commands.insert(commands.end(), {
|
||||
"servers",
|
||||
"templates",
|
||||
"install",
|
||||
"backup"
|
||||
});
|
||||
}
|
||||
|
||||
for (const auto& command : commands) {
|
||||
std::cout << command << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd == "autocomplete_list_servers") {
|
||||
if (dropshell::is_config_loaded())
|
||||
{
|
||||
auto servers = dropshell::autocomplete_list_servers();
|
||||
for (const auto& server : servers)
|
||||
std::cout << server << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd == "autocomplete_list_services") {
|
||||
if (argc < 3) {
|
||||
std::cerr << "Error: autocomplete_list_services requires a server name" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
if (dropshell::is_config_loaded()) {
|
||||
auto services = dropshell::autocomplete_list_services(argv[2]);
|
||||
for (const auto& service : services)
|
||||
std::cout << service << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// from here we require the config file to be loaded.
|
||||
if (!dropshell::is_config_loaded()) {
|
||||
std::cerr << "Error: Failed to load configuration." << std::endl << "Please run 'dropshell init <path>' to initialise the user directory and create a configuration file." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// No arguments provided
|
||||
if (argc < 2) {
|
||||
dropshell::interactive_mode();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd == "servers") {
|
||||
if (argc > 2) {
|
||||
// Show details for specific server
|
||||
@ -106,47 +175,6 @@ int main(int argc, char* argv[]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd == "init") {
|
||||
if (argc < 3) {
|
||||
std::cerr << "Error: init command requires a directory argument" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
try {
|
||||
dropshell::init_user_directory(argv[2]);
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (cmd == "autocomplete_list_servers") {
|
||||
auto servers = dropshell::autocomplete_list_servers();
|
||||
for (const auto& server : servers) {
|
||||
std::cout << server << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd == "autocomplete_list_services") {
|
||||
if (argc < 3) {
|
||||
std::cerr << "Error: autocomplete_list_services requires a server name" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
auto services = dropshell::autocomplete_list_services(argv[2]);
|
||||
for (const auto& service : services) {
|
||||
std::cout << service << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd == "autocomplete_list_commands") {
|
||||
for (const auto& command : commands) {
|
||||
std::cout << command << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd == "install") {
|
||||
std::string server_name;
|
||||
std::vector<std::string> servicelist;
|
||||
@ -191,6 +219,8 @@ int main(int argc, char* argv[]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// handle running a command.
|
||||
for (const auto& command : commands) {
|
||||
if (cmd == command) {
|
||||
std::string server_name;
|
||||
|
Reference in New Issue
Block a user