protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $names = StringTools::Delimit($input->getArgument('name'), ','); $user = $input->getArgument('user'); $group = $input->getArgument('group'); $parent = $input->getOption('parent'); // TODO support if (!empty($parent)) { var_dump($parent); } foreach ($names as $website) { // adding website //$website = $name['text']; LogCLI::Message('Adding website: ' . $website, 0); LogCLI::MessageResult('Group and user: '******'/' . $user, 2, LogCLI::INFO); $path = Paths::$db . Paths::$separator . $group . Paths::$separator . $user . Paths::$separator; if (file_exists($path)) { if (!file_exists($path . $website . '.yml') && Paths::getFullPath($website) === false) { FileOperation::CreateEmptyFile($path . $website . '.yml'); LogCLI::Result(LogCLI::OK); } else { LogCLI::Fail('Website ' . $website . ', under ' . $group . '/' . $user . ' already exists!'); LogCLI::Result(LogCLI::FAIL); } } else { LogCLI::Fail('Group and/or user ' . $group . '/' . $user . ' does not exist!'); LogCLI::Result(LogCLI::FAIL); } } }
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $names = StringTools::TypeList($input->getArgument('name'), '@', ','); $userorgroup = $input->getArgument('userorgroup'); // $group = $input->getArgument('group'); // $group = // $directories = $input->getArgument('directories'); // $files = array(); //ApplicationsDB::LoadAll(); foreach ($names as $name) { if ($name['exclamation'] !== false) { // adding user LogCLI::MessageResult('Exclamation: ' . $name['exclamation'], 2, LogCLI::INFO); $username = $name['text']; LogCLI::Message('Adding user: '******'Creating directory: ' . $structure, 2, LogCLI::INFO); if (@mkdir($structure, 0755, true)) { LogCLI::Result(LogCLI::OK); } else { LogCLI::Result(LogCLI::FAIL); LogCLI::Fail('User ' . $username . ' already exists!'); //LogCLI::Fail($e->getMessage()); } } else { // adding website $website = $name['text']; LogCLI::Message('Adding website: ' . $website, 0); $username = !empty($userorgroup) ? $userorgroup : Paths::$defaultUser; $group = Paths::$defaultGroup; LogCLI::MessageResult('User and group: ' . $username . '/' . $group, 2, LogCLI::INFO); $path = Paths::$db . Paths::$separator . $group . Paths::$separator . $username . Paths::$separator; if (file_exists($path)) { if (!file_exists($path . $website . '.yml') && Paths::getFullPath($website) === false) { FileOperation::CreateEmptyFile($path . $website . '.yml'); LogCLI::Result(LogCLI::OK); } else { LogCLI::Result(LogCLI::FAIL); LogCLI::Fail('Website ' . $website . ', under ' . $group . '/' . $username . ' already exists!'); } } else { LogCLI::Result(LogCLI::FAIL); LogCLI::Fail('Group and/or user ' . $group . '/' . $username . ' does not exist!'); } } } }
<?php /** * User: NIXin * Date: 23.09.2011 * Time: 00:30 */ require_once __DIR__ . '/autoload.php'; use Tools\LogCLI; //use Tools\Errors; //use HypoConf\Commands; use HypoConf\Paths; use Tools\XFormatterHelper; use Symfony\Component\Console; set_error_handler('\\Tools\\Errors::Handle'); Paths::$root = __DIR__; Paths::$db = __DIR__ . '/database'; LogCLI::SetVerboseLevel(6); $application = new Console\Application('NAPKIN', '0.9.1'); $application->setHelperSet(new Console\Helper\HelperSet(array(new Console\Helper\FormatterHelper(), new Console\Helper\DialogHelper(), new XFormatterHelper()))); $application->add(new HypoConf\ConsoleCommands\ListSettings()); $application->add(new HypoConf\ConsoleCommands\LoadSetAndSave()); $application->add(new HypoConf\ConsoleCommands\Generate()); $application->add(new HypoConf\ConsoleCommands\AddSite()); $application->add(new HypoConf\ConsoleCommands\AddUser()); $application->run(); //$shell = new Console\Shell($application); //$shell->run();
public static function Add($arguments) { foreach (StringTools::TypeList($arguments['name'], '@') as $argument) { $name = $argument['text']; if ($argument['exclamation'] !== false) { LogCLI::MessageResult('Exclamation: ' . $argument['exclamation'], 2, LogCLI::INFO); $username = $name; LogCLI::Message('Adding user: '******'name2']) ? $arguments['name2'] : Paths::$defaultGroup; $structure = Paths::$db . Paths::$separator . $group . Paths::$separator . $username; LogCLI::MessageResult('Creating directory: ' . $structure, 2, LogCLI::INFO); if (@mkdir($structure, 0755, true)) { LogCLI::Result(LogCLI::OK); } else { LogCLI::Result(LogCLI::FAIL); LogCLI::Fail('User ' . $username . ' already exists!'); //LogCLI::Fail($e->getMessage()); } } else { // adding website $website = $name; LogCLI::Message('Adding website: ' . $website, 0); $username = isset($arguments['name2']) ? $arguments['name2'] : Paths::$defaultUser; $group = Paths::$defaultGroup; LogCLI::MessageResult('Username and group: ' . $username . '/' . $group, 2, LogCLI::INFO); $path = Paths::$db . Paths::$separator . $group . Paths::$separator . $username . Paths::$separator; if (file_exists($path)) { if (!file_exists($path . $website . '.yml') && Paths::getFullPath($website) === false) { FileOperation::CreateEmptyFile($path . $website . '.yml'); LogCLI::Result(LogCLI::OK); } else { LogCLI::Result(LogCLI::FAIL); LogCLI::Fail('Website ' . $website . ', under ' . $group . '/' . $username . ' already exists!'); } } else { LogCLI::Result(LogCLI::FAIL); LogCLI::Fail('Group and/or user ' . $group . '/' . $username . ' does not exist!'); } } } }
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $name = $input->getArgument('name'); $settingPath = $input->getArgument('path'); $values = $input->getArgument('values'); // $multiline = $input->getOption('multiline'); if (empty($values)) { // enable multiline input $dialog = $this->getHelperSet()->get('dialog'); if (!$dialog->askConfirmation($output, '<question>Would you like to provide multiline input? Answering no, quits the command. (y/n) </question> ', false)) { return; } else { $input = array(); $output->writeln('Type <info>EOT</info> in a new line when you finished inputting all the options.'); while (1) { $input[] = $dialog->ask($output, null, 'foo'); if (end($input) == 'EOT') { array_pop($input); break; } } $values[] = implode(PHP_EOL, $input); // $values = $input; //var_dump($values); } } ApplicationsDB::LoadAll(); foreach (StringTools::TypeList($name) as $argument) { if ($argument['exclamation'] !== false) { // TODO: actually handle the exclamators LogCLI::MessageResult('Exclamation: ' . $argument['exclamation'], 4, LogCLI::INFO); } else { //siteYML $file = Paths::getFullPath($argument['text']); if ($file !== false) { $application = 'nginx'; $basicScope = 'server'; $settings = ApplicationsDB::GetSettingsList($application, $basicScope); $value = ArrayTools::dearraizeIfNotRequired($values); //$chain = StringTools::Delimit($chain, '.'); //$settingPath = implode('/', $chain); LogCLI::MessageResult('Path of the setting: ' . $settingPath, 4, LogCLI::INFO); if ($path = SettingsDB::findPathForSetting($settings, $settingPath, $basicScope)) { $settingsDB = new ConfigScopes\SettingsDB(); // load the original file first $settingsDB->mergeFromYAML($file); $currentSetting = $settingsDB->returnOneByPath($settingPath); /** * if there is a difference * TODO: probably dearraize not required here */ if (ArrayTools::dearraizeIfNotRequired($currentSetting) != $value) { $formatter = $this->getHelperSet()->get('xformatter'); $displayValues = array(); foreach ($values as $valueLine) { $displayValues = array_merge($displayValues, StringTools::multilineStringToArray($valueLine)); } $displayCurrentSetting = array(); foreach ((array) $currentSetting as $valueLine) { $displayCurrentSetting = array_merge($displayCurrentSetting, StringTools::multilineStringToArray($valueLine)); } $toFormat = array(array('messages' => $displayCurrentSetting, 'style' => 'error'), array('messages' => array('> >'), 'style' => 'fg=yellow;bg=black;other=blink;other=bold', 'large' => false), array('messages' => $displayValues, 'style' => 'fg=black;bg=yellow;other=bold')); //array_merge(array('With the following data:'), (array) $values) $output->writeln($formatter->formatMultipleBlocks($toFormat, ' ', true)); $dialog = $this->getHelperSet()->get('dialog'); if (!$dialog->askConfirmation($output, 'Are you sure that you want to make this change? (type "y" to confirm) ', false)) { return; } // make the tree $setting = Tree::addToTreeAndSet(explode('/', $path), $value); //var_dump($setting); // add/replace the setting $settingsDB->mergeFromArray($setting); // save the file with the new setting $settingsDB->returnYAML($file); } else { /** * nothing to do, it's all the same */ $output->writeln('<fg=yellow;other=bold>No need to change, the values are already identical!</fg=yellow;other=bold>'); } } else { // TODO } } else { LogCLI::Fail('Sorry, no site by name: ' . $argument['text']); } } } }