busted
This commit is contained in:
parent
9e6281c846
commit
cf8a7db01d
28
build.sh
28
build.sh
@ -9,6 +9,17 @@ GREEN='\033[0;32m'
|
|||||||
YELLOW='\033[1;33m'
|
YELLOW='\033[1;33m'
|
||||||
NC='\033[0m' # No Color
|
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
|
# Function to print status messages
|
||||||
print_status() {
|
print_status() {
|
||||||
echo -e "${GREEN}[*] $1${NC}"
|
echo -e "${GREEN}[*] $1${NC}"
|
||||||
@ -79,9 +90,19 @@ else
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if user wants to install
|
# Check if user wants to install
|
||||||
read -p "Do you want to install the program? (y/n) " -n 1 -r
|
if [ $AUTO_INSTALL = true ]; then
|
||||||
echo
|
print_status "Auto-installing dropshell..."
|
||||||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
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..."
|
print_status "Installing dropshell..."
|
||||||
sudo make install
|
sudo make install
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
@ -90,6 +111,7 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
|
|||||||
print_error "Installation failed!"
|
print_error "Installation failed!"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Return to original directory
|
# Return to original directory
|
||||||
|
36
src/autocomplete.cpp
Normal file
36
src/autocomplete.cpp
Normal 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
16
src/autocomplete.hpp
Normal 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
|
@ -1,61 +1,14 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
_dropshell_completions() {
|
_dropshell_completions() {
|
||||||
local cur prev opts
|
local cur
|
||||||
COMPREPLY=()
|
COMPREPLY=()
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
||||||
root="${COMP_WORDS[1]}"
|
|
||||||
|
|
||||||
# add all commands to opts
|
# call dropshell to get the list of possiblities for the current argument. Supply all previous arguments.
|
||||||
local commands=($(dropshell autocomplete_list_commands))
|
local completions=($(dropshell autocomplete "${COMP_WORDS[@]:1:${COMP_CWORD}-1}"))
|
||||||
# echo "commands: ${commands[*]}"
|
COMPREPLY=( $(compgen -W "${completions[*]}" -- ${cur}) )
|
||||||
opts="${opts} ${commands[*]}"
|
|
||||||
|
|
||||||
# If we're completing the first argument, show all commands
|
|
||||||
if [[ ${COMP_CWORD} -eq 1 ]] ; then
|
|
||||||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
||||||
return 0
|
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
|
# Register the completion function
|
||||||
|
113
src/main.cpp
113
src/main.cpp
@ -7,6 +7,8 @@
|
|||||||
#include "templates.hpp"
|
#include "templates.hpp"
|
||||||
#include "utils/utils.hpp"
|
#include "utils/utils.hpp"
|
||||||
#include "utils/readmes.hpp"
|
#include "utils/readmes.hpp"
|
||||||
|
#include "autocomplete.hpp"
|
||||||
|
#include "main_commands.hpp"
|
||||||
#include <boost/filesystem.hpp>
|
#include <boost/filesystem.hpp>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -74,42 +76,23 @@ int main(int argc, char* argv[]) {
|
|||||||
// silently attempt to load the config file.
|
// silently attempt to load the config file.
|
||||||
cfg->load_config();
|
cfg->load_config();
|
||||||
|
|
||||||
if (argc == 1) {
|
if (argc < 2) {
|
||||||
dropshell::print_help();
|
dropshell::print_help();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
std::string cmd = argv[1];
|
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) {
|
if (cmd == "autocomplete") {
|
||||||
std::cerr << "Error: init command requires a directory argument" << std::endl;
|
dropshell::autocomplete(argvec);
|
||||||
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;
|
|
||||||
}
|
|
||||||
return 0;
|
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") {
|
if (cmd == "help" || cmd == "-h" || cmd == "--help" || cmd== "h" || cmd=="halp") {
|
||||||
@ -117,69 +100,6 @@ int main(int argc, char* argv[]) {
|
|||||||
return 0;
|
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.
|
// from here we require the config file to be loaded.
|
||||||
if (!cfg->is_config_set()) {
|
if (!cfg->is_config_set()) {
|
||||||
@ -187,17 +107,11 @@ int main(int argc, char* argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string lcd;
|
|
||||||
const std::vector<std::string> & local_config_directories = cfg->get_local_config_directories();
|
const std::vector<std::string> & local_config_directories = cfg->get_local_config_directories();
|
||||||
std::cout << "Config directories: ";
|
std::cout << "Config directories: ";
|
||||||
for (auto & dir : local_config_directories)
|
for (auto & dir : local_config_directories)
|
||||||
std::cout << "["<< dir << "] " << std::endl;
|
std::cout << "["<< dir << "] " << std::endl;
|
||||||
std::cout << std::endl;;
|
std::cout << std::endl;
|
||||||
// No arguments provided
|
|
||||||
if (argc < 2) {
|
|
||||||
dropshell::print_help();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cmd == "server" || cmd == "servers" || cmd == "view" || cmd == "views" || cmd == "v")
|
if (cmd == "server" || cmd == "servers" || cmd == "view" || cmd == "views" || cmd == "v")
|
||||||
switch (argc)
|
switch (argc)
|
||||||
@ -268,7 +182,8 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// handle running a command.
|
// 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.
|
commands.merge(std::set<std::string>{"ssh","edit"}); // handled by service_runner, but not in template_shell_commands.
|
||||||
for (const auto& command : commands) {
|
for (const auto& command : commands) {
|
||||||
if (cmd == command) {
|
if (cmd == command) {
|
||||||
|
48
src/main_commands.cpp
Normal file
48
src/main_commands.cpp
Normal 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
17
src/main_commands.hpp
Normal 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
|
@ -186,4 +186,16 @@ void create_server(const std::string &server_name)
|
|||||||
std::cout << std::endl;
|
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
|
} // namespace dropshell
|
@ -22,6 +22,8 @@ namespace dropshell {
|
|||||||
|
|
||||||
void create_server(const std::string& server_name);
|
void create_server(const std::string& server_name);
|
||||||
|
|
||||||
|
void get_all_used_commands(std::set<std::string> &commands);
|
||||||
|
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
||||||
|
|
||||||
#endif // SERVERS_HPP
|
#endif // SERVERS_HPP
|
||||||
|
Loading…
x
Reference in New Issue
Block a user