Skip to content
This repository has been archived by the owner on Feb 11, 2019. It is now read-only.

Commit

Permalink
Merge 20f0bab into da088b3
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoodcock committed Nov 15, 2014
2 parents da088b3 + 20f0bab commit 701306b
Show file tree
Hide file tree
Showing 6 changed files with 717 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/Virtphp/Command/ActivateCommand.php
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of virtPHP.
*
* (c) Jordan Kasper <github @jakerella>
* Ben Ramsey <github @ramsey>
* Jacques Woodcock <github @jwoodcock>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Virtphp\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Virtphp\Virtphp;

class ActivateCommand extends Command
{

/**
* {@inheritDoc}
*/
protected function configure()
{
$this
->setName('activate')
->setDescription(
'Returns the activate file path to be sourced for '
. 'the specified environment.'
)
->addArgument(
'env',
InputArgument::REQUIRED,
'Which environment do you want to activate'
);
}

/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{

$this->output = $output;
$envName = $input->getArgument('env');
$envFile = $this->getEnvFile();
if (!$envName) {
$output->writeln('<error>No environment name was provided.</error>');
return false;
}

// Logic for getting the one environment and returning
$activator = $this->getWorker('Activator', array(
$output,
$envName,
$envFile
));
if ($activator->execute()) {
return true;
}

return false;
}
}
1 change: 1 addition & 0 deletions src/Virtphp/Console/Application.php
Expand Up @@ -132,6 +132,7 @@ protected function getDefaultCommands()
$commands[] = new Command\CloneCommand();
$commands[] = new Command\DestroyCommand();
$commands[] = new Command\ShowCommand();
$commands[] = new Command\ActivateCommand();

return $commands;
}
Expand Down
179 changes: 179 additions & 0 deletions src/Virtphp/Workers/Activator.php
@@ -0,0 +1,179 @@
<?php

/**
* This file is part of virtPHP.
*
* (c) Jordan Kasper <github @jakerella>
* Ben Ramsey <github @ramsey>
* Jacques Woodcock <github @jwoodcock>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Virtphp\Workers;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\TableHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Activator extends AbstractWorker
{

/**
* @var string
*/
protected $envName;

/**
* @var string
*/
protected $envPath;

/**
* @var string
*/
protected $envFile;

/**
* @var string
*/
protected $file = 'environments.json';

/**
* @var string
*/
protected $filePath;

/**
* @var string
*/
protected $tableHelper;

/**
* @var OutputInterface
*/
protected $output;

/**
* Constructs the shower worker
*
* @param OutputInterface $output
*/
public function __construct(OutputInterface $output, $envName, $envFile)
{
$this->envName = $envName;
$this->envFile = $envFile;
$this->envPath = getenv('HOME') . DIRECTORY_SEPARATOR . '.virtphp';
$this->filePath = $this->envPath . DIRECTORY_SEPARATOR. $this->file;
$this->output = $output;
$this->tableHelper = new TableHelper();
}

/**
* Function is the guts of the worker, reading the provided
* directory and copying those files over.
*
* @return boolean Whether or not the action was successful
*/
public function execute()
{
// If a resync was not requested, let's check to make sure we have
// a valid json file to read and read it
if ($this->getFilesystem()->exists($this->filePath)) {

// Do a search for the provided environment
$envFile = $this->envFile->checkForEnvironment($this->envName);

// make sure we found the environment
if (!$envFile) {
$this->output->writeln(
'<error>Could not find the environment you asked for.</error>'
);
return false;
}

// build source path
$path = $envFile['path'] . DIRECTORY_SEPARATOR . $envFile['name']
. DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'activate';

// Initiative copy to clipboard process
if (!$this->copyToClipboard('source ' . $path)) {
$this->output->writeln(
'<error>Could not copy the path to your clipboard. '
. 'Please copy the instructions below.</error>'
);
} else {
$this->output->writeln(
'<info>Congrats, we were able to save the source command to '
. 'your clipboard. </info>'
);
$this->output->writeln(
'<info>Just hit your paste command to activate this '
. 'environment or follow the instructions below.</info>'
);
$this->output->writeln('');
$this->output->writeln(
'<info>or</info>'
);
}

$this->output->writeln('');
$this->output->writeln(
'<info>Copy and paste this code to activate your environment.</info>'
);
$this->output->writeln('<comment>source ' . $path . '</comment>');
$this->output->writeln('');

} else {
$this->output->writeln(
'<error>either no environments have been created on this system'
. ' or the json file has been moved</error>'
);
return false;
}

return true;
}

/**
* Method for getting the current OS
*
*/
public function getOs()
{
// get the current os
$os = $this->getProcess('uname');

if ($os->run() == 0) {
return $os->getOutput();
} else {
return 'error';
}
}

/**
* Method for copying source to the clipboard
*/
public function copyToClipboard($source)
{
$os = trim($this->getOs());

if ($os === 'Darwin') {
$process = $this->getProcess('echo "' . $source . '" | pbcopy');
if ($process->run() == 0) {
return true;
}
} elseif ($os === 'Linux') {
$process = $this->getProcess('echo "' . $source . '" | xclip');
if ($process->run() == 0) {
return true;
}
}

return false;
}
}

0 comments on commit 701306b

Please sign in to comment.