Skip to content

Commit

Permalink
Adding yaml:unset:key command.
Browse files Browse the repository at this point in the history
  • Loading branch information
grasmash committed Aug 16, 2016
1 parent d6a5499 commit d631a3f
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
5 changes: 5 additions & 0 deletions atlassian-ide-plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<atlassian-ide-plugin>
<project-configuration id="1">
<servers id="2" />
</project-configuration>
</atlassian-ide-plugin>
7 changes: 7 additions & 0 deletions config/translations/en/yaml.unset.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
key:
description: 'Unset a YAML key in a YAML file.'
arguments:
yaml-file: 'Path of YAML file to update'
yaml-key: 'YAML key to be unset'
messages:
unset: 'YAML file "%s" was unset successfully.'
94 changes: 94 additions & 0 deletions src/Command/Yaml/UnsetKeyCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/**
* @file
* Contains \Drupal\Console\Command\Yaml\GetValueCommand.
*/

namespace Drupal\Console\Command\Yaml;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Command\Shared\CommandTrait;
use Drupal\Console\Style\DrupalStyle;

class UnsetKeyCommand extends Command
{
use CommandTrait;

protected function configure()
{
$this
->setName('yaml:unset:key')
->setDescription($this->trans('commands.yaml.unset.key.description'))
->addArgument(
'yaml-file',
InputArgument::REQUIRED,
$this->trans('commands.yaml.unset.value.arguments.yaml-file')
)
->addArgument(
'yaml-key',
InputArgument::REQUIRED,
$this->trans('commands.yaml.unset.value.arguments.yaml-key')
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$yaml = new Parser();
$dumper = new Dumper();

$yaml_file = $input->getArgument('yaml-file');
$yaml_key = $input->getArgument('yaml-key');

try {
$yaml_parsed = $yaml->parse(file_get_contents($yaml_file));
} catch (\Exception $e) {
$io->error($this->trans('commands.yaml.merge.messages.error-parsing').': '.$e->getMessage());

return;
}

if (empty($yaml_parsed)) {
$io->info(
sprintf(
$this->trans('commands.yaml.merge.messages.wrong-parse'),
$yaml_file
)
);
}

$nested_array = $this->getApplication()->getNestedArrayHelper();
$parents = explode(".", $yaml_key);
$nested_array::unsetValue($yaml_parsed, $parents);

try {
$yaml = $dumper->dump($yaml_parsed, 10);
} catch (\Exception $e) {
$io->error($this->trans('commands.yaml.merge.messages.error-generating').': '.$e->getMessage());

return;
}

try {
file_put_contents($yaml_file, $yaml);
} catch (\Exception $e) {
$io->error($this->trans('commands.yaml.merge.messages.error-writing').': '.$e->getMessage());

return;
}

$io->info(
sprintf(
$this->trans('commands.yaml.unset.value.messages.unset'),
$yaml_file
)
);
}
}

0 comments on commit d631a3f

Please sign in to comment.