-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLessThanCommand.php
More file actions
68 lines (54 loc) · 1.81 KB
/
LessThanCommand.php
File metadata and controls
68 lines (54 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
declare(strict_types=1);
namespace Syntatis\Version\CLI\Commands;
use Assert\Assertion;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
use Version\Version;
use function sprintf;
final class LessThanCommand extends Command
{
/**
* Configure the command options and arguments.
*/
protected function configure(): void
{
$this->setName('lt');
$this->setDescription('Compare if a version is less than another');
$this->addArgument('version-a', InputArgument::REQUIRED, 'First version to compare');
$this->addArgument('version-b', InputArgument::REQUIRED, 'Second version to compare against the first');
$this->setHelp(<<<'HELP'
This command compares two versions and checks if the first version is less than the second.
Usage:
<info>version lt 0.9.0 1.0.0</info>
<info>version lt 2.1.0 2.1.0</info>
HELP);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$style = new SymfonyStyle($input, $output);
$versionA = $input->getArgument('version-a');
$versionB = $input->getArgument('version-b');
try {
Assertion::string($versionA);
Assertion::string($versionB);
/** @var Version $a */
$a = Version::fromString($versionA);
/** @var Version $b */
$b = Version::fromString($versionB);
if ($a->isLessThan($b)) {
$style->success(sprintf("Version '%s' is less than '%s'.", $a, $b));
return Command::SUCCESS;
}
$style->error(sprintf("Version '%s' is not less than '%s'.", $a, $b));
return Command::FAILURE;
} catch (Throwable $th) {
$style->error($th->getMessage());
return Command::FAILURE;
}
}
}