Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Update edit command replace vim command
Browse files Browse the repository at this point in the history
  • Loading branch information
panlatent committed May 9, 2017
1 parent 28e6b4e commit c626a54
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ $ site-cli [command] [argment]
```

Command:
+ **init** Init your site-cli configure
+ **config** Setting your .site-cli.yml
+ **disable** Disable a site or a group sites
+ **enable** Enable a site or a group sites
+ **help** Displays help for a command
+ **init** Init your site-cli configure
+ **edit** Edit site configuration using editor
+ **list** Lists sites or groups or servers
+ **vim** Edit site configuration using Vim
+ **help** Displays help for a command

Optional
---------
Expand Down
4 changes: 2 additions & 2 deletions bin/site-cli
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use Panlatent\SiteCli\Commands\EnableCommand;
use Panlatent\SiteCli\Commands\InitCommand;
use Panlatent\SiteCli\Commands\ListCommand;
use Panlatent\SiteCli\Application;
use Panlatent\SiteCli\Commands\VimCommand;
use Panlatent\SiteCli\Commands\EditCommand;
use Panlatent\SiteCli\Configure;
use Panlatent\SiteCli\Site\Manager;

Expand Down Expand Up @@ -88,7 +88,7 @@ $app->add(new ConfigCommand());
$app->add(new ListCommand());
$app->add(new EnableCommand());
$app->add(new DisableCommand());
$app->add(new VimCommand());
$app->add(new EditCommand());

/* ----------------------------------------------------------------------------
* Run the console application.
Expand Down
10 changes: 10 additions & 0 deletions src/Commands/CompletionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ function () {
return $names;
}
));

$handler->addHandler(new Completion(
'edit',
'editor',
Completion::TYPE_OPTION,
[
'vim',
'sublime'
]
));
}

/**
Expand Down
34 changes: 28 additions & 6 deletions src/Commands/VimCommand.php → src/Commands/EditCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

use Panlatent\SiteCli\Site\Manager;
use Panlatent\SiteCli\Site\NotFoundException;
use Panlatent\SiteCli\Support\Vim;
use Panlatent\SiteCli\Support\Editor;
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 VimCommand extends Command
class EditCommand extends Command
{
/**
* @var \Panlatent\SiteCli\Site\Manager
Expand All @@ -30,8 +31,8 @@ public function register(Manager $manager)

protected function configure()
{
$this->setName('vim')
->setDescription('Edit site configuration using Vim')
$this->setName('edit')
->setDescription('Edit site configuration using editor')
->addArgument(
'group',
InputArgument::REQUIRED,
Expand All @@ -41,26 +42,47 @@ protected function configure()
'site',
InputArgument::OPTIONAL,
'Site name in the group'
)
->addOption(
'editor',
'e',
InputOption::VALUE_REQUIRED,
'use editor',
'vim'
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$groupName = $input->getArgument('group');
$siteName = $input->getArgument('site');
$editor = $input->getOption('editor');
if (false === ($group = $this->manager->getGroup($groupName))) {
throw new NotFoundException("Not found site group \"$groupName\"");
}

if (empty($siteName)) {
Vim::open($group->getPath());
$this->openEditor($editor, $group->getPath());
return;
}

if (false === ($site = $group->getSite($siteName))) {
throw new NotFoundException("Not found site \"$siteName\" in $groupName group");
}

Vim::open($site->getPath());
$this->openEditor($editor, $site->getPath());
}

protected function openEditor($editor, $path)
{
switch ($editor) {
case 'vi':
case 'vim':
Editor::vim($path);
break;
case 'subl':
case 'sublime':
Editor::sublime($path);
}
}
}
4 changes: 2 additions & 2 deletions src/Commands/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Panlatent\SiteCli\Configure;
use Panlatent\SiteCli\Exception;
use Panlatent\SiteCli\Support\Util;
use Panlatent\SiteCli\Support\Vim;
use Panlatent\SiteCli\Support\Editor;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -54,7 +54,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

file_put_contents($filename, Yaml::dump($config, 8));
Vim::open($filename);
Editor::vim($filename);

try {
new Configure($filename);
Expand Down
11 changes: 8 additions & 3 deletions src/Support/Vim.php → src/Support/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@

use Panlatent\SiteCli\Exception;

class Vim
class Editor
{
protected static $descriptors = [
['file', '/dev/tty', 'r'],
['file', '/dev/tty', 'w'],
['file', '/dev/tty', 'w'],
];

public static function open($filename)
public static function vim($filename)
{
$process = proc_open('vim ' . $filename, static::$descriptors, $pipes);
$process = proc_open('vim ' . escapeshellarg($filename), static::$descriptors, $pipes);
if ( ! is_resource($process)) {
throw new Exception('Open vim failed');
}
Expand All @@ -33,4 +33,9 @@ public static function open($filename)
usleep(100);
}
}

public static function sublime($filename)
{
exec('subl ' . escapeshellarg($filename) . ' >/dev/null 2>&1');
}
}

0 comments on commit c626a54

Please sign in to comment.