This commit is contained in:
Your Name 2025-04-26 09:09:20 +12:00
parent 9e6281c846
commit cf8a7db01d
9 changed files with 178 additions and 157 deletions

View File

@ -9,6 +9,17 @@ GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Parse command line arguments
AUTO_INSTALL=false
for arg in "$@"; do
case $arg in
--auto-install)
AUTO_INSTALL=true
;;
esac
done
# Function to print status messages
print_status() {
echo -e "${GREEN}[*] $1${NC}"
@ -79,9 +90,19 @@ else
fi
# Check if user wants to install
read -p "Do you want to install the program? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
if [ $AUTO_INSTALL = true ]; then
print_status "Auto-installing dropshell..."
sudo make install
if [ $? -eq 0 ]; then
print_status "Installation successful!"
else
print_error "Installation failed!"
exit 1
fi
else
read -p "Do you want to install the program? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
print_status "Installing dropshell..."
sudo make install
if [ $? -eq 0 ]; then
@ -90,6 +111,7 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
print_error "Installation failed!"
exit 1
fi
fi
fi
# Return to original directory

36
src/autocomplete.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "autocomplete.hpp"
#include "servers.hpp"
#include "config.hpp"
#include <iostream>
void dropshell::autocomplete(const std::vector<std::string> &args)
{
// std::cerr << "[ "<<args.size()<<" ] : ";
// for (const auto& arg : args)
// std::cerr << arg << " ";
// std::cerr << std::endl;
if (args.size() < 3) // dropshell autocomplete ???
autocomplete_list_commands();
}
void dropshell::autocomplete_list_commands()
{
std::set<std::string> commands;
dropshell::get_all_used_commands(commands);
// add in commmands hard-coded and handled in main
commands.merge(std::set<std::string>{
"help","init" // these are always available.
});
if (dropshell::get_global_config()->is_config_set())
commands.merge(std::set<std::string>{
"server","templates","create-service","create-template","create-server","edit","ssh",
"view" // only if we have a config.
});
for (const auto& command : commands) {
std::cout << command << std::endl;
}
}

16
src/autocomplete.hpp Normal file
View File

@ -0,0 +1,16 @@
#ifndef AUTOCOMPLETE_HPP
#define AUTOCOMPLETE_HPP
#include <string>
#include <vector>
namespace dropshell {
void autocomplete(const std::vector<std::string> &args);
void autocomplete_list_commands();
} // namespace dropshell
#endif

View File

@ -1,61 +1,14 @@
#!/bin/bash
_dropshell_completions() {
local cur prev opts
local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
root="${COMP_WORDS[1]}"
# add all commands to opts
local commands=($(dropshell autocomplete_list_commands))
# echo "commands: ${commands[*]}"
opts="${opts} ${commands[*]}"
# If we're completing the first argument, show all commands
if [[ ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
# call dropshell to get the list of possiblities for the current argument. Supply all previous arguments.
local completions=($(dropshell autocomplete "${COMP_WORDS[@]:1:${COMP_CWORD}-1}"))
COMPREPLY=( $(compgen -W "${completions[*]}" -- ${cur}) )
return 0
fi
# Command-specific completions
case "${root}" in
templates|autocomplete_list_servers|autocomplete_list_services|autocomplete_list_commands)
# No additional completions for these commands
COMPREPLY=()
return 0
;;
*)
if [[ ! " ${commands[*]} " =~ " ${root} " ]]; then
COMPREPLY=()
return 0
fi
# Handle completion for template commands
if [[ ${COMP_CWORD} -eq 2 ]]; then
# Second argument is server name
local servers=($(dropshell autocomplete_list_servers))
COMPREPLY=( $(compgen -W "${servers[*]}" -- ${cur}) )
return 0
elif [[ ${COMP_CWORD} -eq 3 ]]; then
# Third argument is service name
local server_name="${COMP_WORDS[2]}"
local services=($(dropshell autocomplete_list_services "$server_name"))
COMPREPLY=( $(compgen -W "${services[*]}" -- ${cur}) )
return 0
elif [[ ${COMP_CWORD} -eq 4 ]]; then
if [[ ${root} == "restore" ]]; then
# Fourth argument is backup file name
local server_name="${COMP_WORDS[2]}"
local service_name="${COMP_WORDS[3]}"
local backup_files=($(dropshell autocomplete_list_backups "$server_name" "$service_name"))
COMPREPLY=( $(compgen -W "${backup_files[*]}" -- ${cur}) )
return 0
fi
COMPREPLY=()
return 0
fi
;;
esac
}
# Register the completion function

View File

@ -7,6 +7,8 @@
#include "templates.hpp"
#include "utils/utils.hpp"
#include "utils/readmes.hpp"
#include "autocomplete.hpp"
#include "main_commands.hpp"
#include <boost/filesystem.hpp>
#include <iostream>
@ -74,42 +76,23 @@ int main(int argc, char* argv[]) {
// silently attempt to load the config file.
cfg->load_config();
if (argc == 1) {
if (argc < 2) {
dropshell::print_help();
return 0;
}
std::string cmd = argv[1];
std::vector<std::string> argvec;
for (int i=0; i<argc; i++)
argvec.push_back(argv[i]);
// don't load old config if we're initializing
if (cmd == "init") {
std::string lcd;
if (argc < 3) {
std::cerr << "Error: init command requires a directory argument" << std::endl;
return 1;
}
try {
if (!cfg->add_local_config_directory(argv[2]))
return 1; // error already reported
cfg->save_config();
std::cout << "Config directory added: " << cfg->get_local_config_directories().back() << std::endl;
dropshell::create_readme_local_config_dir(cfg->get_local_config_directories().back());
if (cfg->get_local_config_directories().size() ==1)
std::cout << "DropShell is now initialised and you can add a server with 'dropshell create-server <server-name>'" << std::endl;
else
{
std::cout << "DropShell will now use all of the following directories for configuration:" << std::endl;
for (const auto& dir : cfg->get_local_config_directories()) {
std::cout << " " << dir << std::endl;
}
std::cout << "You can edit the config file manually at: " << dropshell::get_local_dropshell_config_path() << std::endl;
}
if (cmd == "autocomplete") {
dropshell::autocomplete(argvec);
return 0;
} catch (const std::exception& e) {
std::cerr << "Error in init: " << e.what() << std::endl;
return 1;
}
if (cmd == "init") {
return dropshell::main_commands::init(argvec);
}
if (cmd == "help" || cmd == "-h" || cmd == "--help" || cmd== "h" || cmd=="halp") {
@ -117,69 +100,6 @@ int main(int argc, char* argv[]) {
return 0;
}
// auto completion stuff.
std::set<std::string> template_shell_commands, full_command_set;
std::vector<dropshell::ServerInfo> servers = dropshell::get_configured_servers();
for (const auto& server : servers)
{
std::vector<dropshell::ServiceInfo> services = dropshell::get_server_services_info(server.name);
for (const auto& service : services)
template_shell_commands.merge(dropshell::get_used_commands(server.name, service.service_name));
}
if (cmd == "autocomplete_list_commands") { // add in commands handled here, not by the template shell scripts.
std::set<std::string> full_command_set = template_shell_commands;
full_command_set.merge(std::set<std::string>{
"help","init" // these are always available.
});
if (cfg->is_config_set())
full_command_set.merge(std::set<std::string>{
"server","templates","create-service","create-template","create-server","edit","ssh",
"view" // only if we have a config.
});
for (const auto& command : full_command_set) {
std::cout << command << std::endl;
}
return 0;
}
if (cmd == "autocomplete_list_servers") {
if (cfg->is_config_set())
{
auto servers = dropshell::get_configured_servers();
for (const auto& server : servers)
std::cout << server.name << 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 (cfg->is_config_set()) {
auto services = dropshell::get_server_services_info(argv[2]);
for (const auto& service : services)
std::cout << service.service_name << std::endl;
}
return 0;
}
if (cmd == "autocomplete_list_backups") {
if (argc < 4) {
std::cerr << "Error: autocomplete_list_backups requires a server name and service name" << std::endl;
return 1;
}
if (cfg->is_config_set()) {
auto backups = dropshell::list_backups(argv[2], argv[3]);
for (const auto& backup : backups)
std::cout << backup << std::endl;
}
return 0;
}
// ------------------------------------------------------------
// from here we require the config file to be loaded.
if (!cfg->is_config_set()) {
@ -187,17 +107,11 @@ int main(int argc, char* argv[]) {
return 1;
}
std::string lcd;
const std::vector<std::string> & local_config_directories = cfg->get_local_config_directories();
std::cout << "Config directories: ";
for (auto & dir : local_config_directories)
std::cout << "["<< dir << "] " << std::endl;
std::cout << std::endl;;
// No arguments provided
if (argc < 2) {
dropshell::print_help();
return 0;
}
std::cout << std::endl;
if (cmd == "server" || cmd == "servers" || cmd == "view" || cmd == "views" || cmd == "v")
switch (argc)
@ -268,7 +182,8 @@ int main(int argc, char* argv[]) {
}
// handle running a command.
std::set<std::string> commands = template_shell_commands;
std::set<std::string> commands;
dropshell::get_all_used_commands(commands);
commands.merge(std::set<std::string>{"ssh","edit"}); // handled by service_runner, but not in template_shell_commands.
for (const auto& command : commands) {
if (cmd == command) {

48
src/main_commands.cpp Normal file
View File

@ -0,0 +1,48 @@
#include "main_commands.hpp"
#include "utils/directories.hpp"
#include "utils/utils.hpp"
#include "utils/readmes.hpp"
#include "config.hpp"
#include <iostream>
namespace dropshell {
namespace main_commands {
int init(const std::vector<std::string> &args)
{
dropshell::config *cfg = dropshell::get_global_config();
std::string lcd;
if (args.size() < 3) {
std::cerr << "Error: init command requires a directory argument" << std::endl;
return 1;
}
try {
if (!cfg->add_local_config_directory(args[2]))
return 1; // error already reported
cfg->save_config();
std::cout << "Config directory added: " << cfg->get_local_config_directories().back() << std::endl;
dropshell::create_readme_local_config_dir(cfg->get_local_config_directories().back());
if (cfg->get_local_config_directories().size() ==1)
std::cout << "DropShell is now initialised and you can add a server with 'dropshell create-server <server-name>'" << std::endl;
else
{
std::cout << "DropShell will now use all of the following directories for configuration:" << std::endl;
for (const auto& dir : cfg->get_local_config_directories()) {
std::cout << " " << dir << std::endl;
}
std::cout << "You can edit the config file manually at: " << dropshell::get_local_dropshell_config_path() << std::endl;
}
return 0;
} catch (const std::exception& e) {
std::cerr << "Error in init: " << e.what() << std::endl;
return 1;
}
}
} // namespace main_commands
} // namespace dropshell

17
src/main_commands.hpp Normal file
View File

@ -0,0 +1,17 @@
#ifndef MAIN_COMMANDS_HPP
#define MAIN_COMMANDS_HPP
#include <string>
#include <vector>
namespace dropshell {
namespace main_commands {
int init(const std::vector<std::string> &args);
} // namespace main_commands
} // namespace dropshell
#endif

View File

@ -186,4 +186,16 @@ void create_server(const std::string &server_name)
std::cout << std::endl;
}
void get_all_used_commands(std::set<std::string> &commands)
{
std::set<std::string> template_shell_commands;
std::vector<ServerInfo> servers = get_configured_servers();
for (const auto& server : servers)
{
std::vector<dropshell::ServiceInfo> services = dropshell::get_server_services_info(server.name);
for (const auto& service : services)
template_shell_commands.merge(dropshell::get_used_commands(server.name, service.service_name));
}
}
} // namespace dropshell

View File

@ -22,6 +22,8 @@ namespace dropshell {
void create_server(const std::string& server_name);
void get_all_used_commands(std::set<std::string> &commands);
} // namespace dropshell
#endif // SERVERS_HPP