Skip to content

Commit

Permalink
#606 cli compare command (#22)
Browse files Browse the repository at this point in the history
* #606 cli compare command

* #606 update doc

* #606 add options

* #606 delete options
  • Loading branch information
gregurcom authored Mar 3, 2024
1 parent 5514ad9 commit 8231cb4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ or in case of custom environment with basic auth credentials
diffy project:compare PROJECT_ID prod custom --env2Url="https://custom.example.com" --env2User="user" --env2Pass="password"
```

or with existing screenshots

```shell script
diffy project:compare PROJECT_ID existing existing --screenshot1=100 --screenshot2=101
```

Allowed environments are: prod, stage, dev, custom (long options: production, staging, development).

#### Update project(s)
Expand Down
37 changes: 32 additions & 5 deletions src/Commands/ProjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Diffy\Diff;
use Diffy\Diffy;
use Diffy\InvalidArgumentsException;
use Diffy\Project;
use Diffy\Screenshot;
use DiffyCli\Config;
use GuzzleHttp\Exception\InvalidArgumentException;
use Robo\Tasks;
Expand Down Expand Up @@ -57,7 +59,7 @@ private function isValidJsonConfig(string $configurationPath): array
* @param string $env2 Second environment to compare
* @param array $options
*
* @throws \Diffy\InvalidArgumentsException
* @throws InvalidArgumentsException
*
* @option env1Url Url of the first environment if custom environment
* @option env1User Basic auth user for env1Url
Expand All @@ -68,6 +70,8 @@ private function isValidJsonConfig(string $configurationPath): array
* @option wait Wait for the diff to be completed
* @option max-wait Maximum number of seconds to wait for the diff to be completed.
* @option commit-sha GitHub commit SHA.
* @option screenshot1 First existing screenshot
* @option screenshot2 Second existing screenshot
*
* @usage project:compare 342 prod stage
* Compare production and stage environments.
Expand All @@ -81,8 +85,9 @@ public function createCompare(
string $env1,
string $env2,
array $options = [
'wait' => false, 'max-wait' => 1200, 'commit-sha' => null, 'env1Url' => '', 'env1User' => null, 'env1Pass' => null,
'env2Url' => '', 'env2User' => null, 'env2Pass' => null, 'name' => ''
'wait' => false, 'max-wait' => 1200, 'commit-sha' => null, 'env1Url' => '', 'env1User' => null,
'env1Pass' => null, 'env2Url' => '', 'env2User' => null, 'env2Pass' => null, 'name' => '',
'screenshot1' => null, 'screenshot2' => null,
]
) {
Diffy::setApiKey(Config::getConfig()['key']);
Expand All @@ -95,7 +100,7 @@ public function createCompare(
'env1Pass' => $options['env1Pass'],
'env2Url' => $options['env2Url'],
'env2User' => $options['env2User'],
'env2Pass' => $options['env2Pass'],
'env2Pass' => $options['env2Pass']
];

if (!empty($options['commit-sha']) && $options['commit-sha']) {
Expand All @@ -118,7 +123,29 @@ public function createCompare(
$params['env2'] = 'stage';
}

$diffId = Project::compare($projectId, $params);
if ($env1 === 'existing' || $env2 === 'existing') {
if ($env1 === 'existing' && empty($options['screenshot1'])) {
throw new InvalidArgumentsException('Set screenshot1 value');
} elseif ($env2 === 'existing' && empty($options['screenshot2'])) {
throw new InvalidArgumentsException('Set screenshot2 value');
}

if ($env1 === 'existing') {
$screenshot1 = $options['screenshot1'];
} else {
$screenshot1 = Screenshot::create($projectId, $env1);
}

if ($env2 === 'existing') {
$screenshot2 = $options['screenshot2'];
} else {
$screenshot2 = Screenshot::create($projectId, $env2);
}

$diffId = Diff::create($projectId, $screenshot1, $screenshot2, $options['name']);
} else {
$diffId = Project::compare($projectId, $params);
}

if (!empty($options['name'])) {
Diff::updateName($diffId, $options['name']);
Expand Down

0 comments on commit 8231cb4

Please sign in to comment.