Skip to content

Commit

Permalink
add -c <name>=<value> option
Browse files Browse the repository at this point in the history
pass a configuration parameter to the command.

configuration parameters are defined in utility options.

- docker.socket.path '/var/run/docker.sock'
- docker.client.path '/usr/bin/docker'

written documentation is missing as use is spare so far.
  • Loading branch information
ktomk committed Jun 21, 2020
1 parent 793393c commit eae3a14
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [unreleased]
### Added
- Add `-c <name>=<value>` option to pass a configuration parameter to the
command.
- Support for `BITBUCKET_STEP_RUN_NUMBER` environment parameter: defaults
to `1` and set to `1` after first successful step.

Expand Down
9 changes: 6 additions & 3 deletions src/Runner/RunOpts.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ class RunOpts
* Static factory method
*
* @param string $prefix [optional]
* @param string $binaryPackage package name or path to binary (string)
* @param string $binaryPackage [optional] package name or path to binary (string)
* @param Options $options [optional]
*
* @return RunOpts
*/
public static function create($prefix = null, $binaryPackage = null)
public static function create($prefix = null, $binaryPackage = null, Options $options = null)
{
return new self($prefix, Options::create(), $binaryPackage);
null === $options && $options = Options::create();

return new self($prefix, $options, $binaryPackage);
}

/**
Expand Down
85 changes: 85 additions & 0 deletions src/Utility/ConfigOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/* this file is part of pipelines */

namespace Ktomk\Pipelines\Utility;

use Ktomk\Pipelines\Cli\Args;

/**
* handle configuration related options:
*
* -c <name>=<value>
*
* @see Options
*
* @package Ktomk\Pipelines\Utility\Args
*/
class ConfigOptions extends Options implements Runnable
{
/**
* @var Args
*/
private $args;

/**
* @var Options
*/
private $options;

/**
* @param Args $args
* @param null|Options $options
*
* @return ConfigOptions
*/
public static function bind(Args $args, Options $options = null)
{
return new self($args, $options ?: Options::create());
}

/**
* @param Args $args
*/
public function __construct(Args $args, Options $options)
{
$this->args = $args;
$this->options = $options;

parent::__construct(array('internal'));
}

/**
* @throws \Ktomk\Pipelines\Cli\ArgsException
*
* @return Options
*/
public function run()
{
$this->parse($this->args);

return $this->options;
}

/**
* Parse keep arguments
*
* @param Args $args
*
* @throws \Ktomk\Pipelines\Cli\ArgsException
*
* @return void
*
*/
public function parse(Args $args)
{
$options = $this->options;

while (null !== $setting = $args->getOptionArgument('c')) {
list($name, $value) = explode('=', $setting, 2) + array('undef', null);
$default = $options->get($name);
$type = null === $default ? 'string' : gettype($default);
settype($value, $type) && $options->definition[$name] = array($value);
}
}
}
1 change: 1 addition & 0 deletions src/Utility/Help.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function showHelp()
--dry-run do not execute commands, e.g. invoke docker or
run containers, with --verbose show the commands
that would have run w/o --dry-run
-c <name>=<value> pass a configuration parameter to the command
Pipeline runner options
--basename <basename> set basename for pipelines file, defaults to
Expand Down
7 changes: 6 additions & 1 deletion src/Utility/RunnerOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ public function __construct(Args $args, Streams $streams)
*/
public function run()
{
$runOpts = RunOpts::create();
$runOpts = RunOpts::create(
null,
null,
ConfigOptions::bind($this->args)->run()
);

$this->parse($this->args, $runOpts);

return $runOpts;
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/Utility/ConfigOptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/* this file is part of pipelines */

namespace Ktomk\Pipelines\Utility;

use Ktomk\Pipelines\Cli\Args;
use Ktomk\Pipelines\TestCase;

/**
* Class ConfigOptionsTest
*
* @package Ktomk\Pipelines\Utility
*
* @covers \Ktomk\Pipelines\Utility\ConfigOptions
*/
class ConfigOptionsTest extends TestCase
{
public function testBindAndRun()
{
$args = new Args(array('c', '-c', 'foo=flax'));
$options = ConfigOptions::bind($args)->run();
$this->assertNotNull($options);
}
}

0 comments on commit eae3a14

Please sign in to comment.