forked from thebuggenie/thebuggenie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tbg_cli
executable file
·124 lines (115 loc) · 4.65 KB
/
tbg_cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env php
<?php
// Define The Bug Genie paths and related constants
define('TBG_CLI', true);
$path = realpath(getcwd());
defined('DS') || define('DS', DIRECTORY_SEPARATOR);
defined('THEBUGGENIE_SESSION_NAME') || define('THEBUGGENIE_SESSION_NAME', 'THEBUGGENIE');
defined('THEBUGGENIE_PATH') || define('THEBUGGENIE_PATH', realpath(getcwd() . DS) . DS);
defined('THEBUGGENIE_CORE_PATH') || define('THEBUGGENIE_CORE_PATH', THEBUGGENIE_PATH . 'core' . DS);
defined('THEBUGGENIE_MODULES_PATH') || define('THEBUGGENIE_MODULES_PATH', THEBUGGENIE_PATH . 'modules' . DS);
defined('THEBUGGENIE_PUBLIC_FOLDER_NAME') || define('THEBUGGENIE_PUBLIC_FOLDER_NAME', '');
defined('B2DB_BASEPATH') || define ('B2DB_BASEPATH', THEBUGGENIE_CORE_PATH . 'B2DB' . DS);
defined('B2DB_CACHEPATH') || define ('B2DB_CACHEPATH', THEBUGGENIE_CORE_PATH . 'cache' . DS . 'B2DB' . DS);
if( !defined('THEBUGGENIE_CONFIG_PATH'))
{
if(file_exists(getenv('HOME') . DS . '.remote_server'))
define('THEBUGGENIE_CONFIG_PATH', getenv('HOME') . DS);
else
define('THEBUGGENIE_CONFIG_PATH', THEBUGGENIE_PATH);
}
try
{
// Include the "engine" script, which initializes and sets up stuff
require THEBUGGENIE_CORE_PATH . 'bootstrap.php';
}
catch (Exception $e)
{
TBGCliCommand::cli_echo("An error occured when trying to initialize the command line client:\n", 'white', 'bold');
TBGCliCommand::cli_echo($e->getMessage() . "\n", 'red', 'bold');
die();
}
// Set up all available search paths for cli commands
$command_paths = array();
$command_paths['main'] = THEBUGGENIE_PATH . 'modules' . DS . 'main' . DS . 'classes' . DS . 'cli' . DS;
$command_paths['remote'] = THEBUGGENIE_PATH . 'modules' . DS . 'remote' . DS . 'classes' . DS . 'cli' . DS;
foreach (TBGContext::getModules() as $module_name => $module)
{
$module_cli_path = THEBUGGENIE_PATH . 'modules' . DS . $module_name . DS . 'classes' . DS . 'cli' . DS;
if (file_exists($module_cli_path))
{
$command_paths[$module_name] = $module_cli_path;
}
}
// Set up all cli commands
$commands = array('main' => array());
foreach ($command_paths as $module_name => $command_path)
{
TBGContext::addAutoloaderClasspath($command_path);
$_path_handle = opendir($command_path);
while ($command_class_file = readdir($_path_handle))
{
if (($classname = substr($command_class_file, 0, strpos($command_class_file, '.'))) != '')
{
$module = (TBGContext::isModuleLoaded($module_name)) ? TBGContext::getModule($module_name) : null;
$command = new $classname($module);
if ($command instanceof TBGCliCommand)
{
$commands[$module_name][$command->getCommandName()] = $command;
foreach ($command->getCommandAliases() as $alias)
{
$commands[$module_name][$alias] = $command;
}
}
}
}
}
TBGCliCommand::setAvailableCommands($commands);
if ($argc < 2)
{
// Show usage if no parameters are provided
TBGCliCommand::cli_echo("The Bug Genie command line tool\n\n");
TBGCliCommand::cli_echo("Usage: ", 'white', 'bold');
TBGCliCommand::cli_echo(TBGCliCommand::getCommandLineName() . " [");
TBGCliCommand::cli_echo('command', 'green', 'bold');
TBGCliCommand::cli_echo("]\n");
TBGCliCommand::cli_echo("Type " . TBGCliCommand::getCommandLineName() . ' ');
TBGCliCommand::cli_echo('help', 'green', 'bold');
TBGCliCommand::cli_echo(" for more information.\n\n");
}
else
{
// Process arguments and invoke command if available
try
{
TBGCliCommand::processArguments();
$module_command = explode(':', $argv[1]);
$module_name = (count($module_command) == 2) ? $module_command[0] : 'main';
$command = (count($module_command) == 2) ? $module_command[1] : $module_command[0];
TBGContext::reinitializeI18n();
if (array_key_exists($module_name, $commands) && array_key_exists($command, $commands[$module_name]))
{
$class = $commands[$module_name][$command];
TBGContext::setCLIRouting($module_name, $command);
$class->execute();
}
else
{
TBGCliCommand::cli_echo("\n");
TBGCliCommand::cli_echo("Unknown command\n", 'red', 'bold');
TBGCliCommand::cli_echo("Type " . TBGCliCommand::getCommandLineName() . ' ');
TBGCliCommand::cli_echo('help', 'green', 'bold');
TBGCliCommand::cli_echo(" for more information about the cli tool.\n\n");
}
}
catch (Exception $e)
{
TBGCliCommand::cli_echo("\n");
TBGCliCommand::cli_echo("The following error occured:\n", 'red', 'bold');
TBGCliCommand::cli_echo($e->getMessage()."\n\n", 'red');
TBGCliCommand::cli_echo("Type " . TBGCliCommand::getCommandLineName() . ' ');
TBGCliCommand::cli_echo('help', 'green', 'bold');
TBGCliCommand::cli_echo(" for more information about the cli tool.\n\n");
}
}
return true;