Skip to content

Commit 619a336

Browse files
committed
diff command
1 parent 9912c42 commit 619a336

6 files changed

Lines changed: 690 additions & 27 deletions

File tree

src/Commands/BaseCommand.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,18 @@ protected function enhanceExceptionWithCommandContext(KeepException $exception):
101101
);
102102
}
103103
}
104+
105+
public function error($string, $verbosity = null)
106+
{
107+
parent::error($string, $verbosity);
108+
109+
return false;
110+
}
111+
112+
public function info($string, $verbosity = null)
113+
{
114+
parent::info($string, $verbosity);
115+
116+
return true;
117+
}
104118
}

src/Commands/DiffCommand.php

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
use function Laravel\Prompts\spin;
1212
use function Laravel\Prompts\table;
1313

14-
class DiffCommand extends AbstractCommand
14+
class DiffCommand extends BaseCommand
1515
{
16-
public $signature = 'keep:diff
16+
public $signature = 'diff
1717
{--context= : Comma-separated list of contexts to compare (e.g., "vault1:stage1,vault2:stage2")}
1818
{--stage= : Comma-separated list of stages to compare (defaults to all configured stages)}
1919
{--vault= : Comma-separated list of vaults to compare (defaults to current/default vault)}'
2020
.self::UNMASK_SIGNATURE;
2121

2222
public $description = 'Compare secrets across multiple stages and vaults in a matrix view';
2323

24-
public function process(): int
24+
public function process()
2525
{
2626
// If --context is provided, use it to get vaults and stages
2727
if ($this->option('context')) {
@@ -32,29 +32,19 @@ public function process(): int
3232
}
3333

3434
if (empty($vaults)) {
35-
$this->error('No vaults available for comparison.');
36-
37-
return self::FAILURE;
35+
return $this->error('No vaults available for comparison.');
3836
}
3937

4038
if (empty($stages)) {
41-
$this->error('No stages available for comparison.');
42-
43-
return self::FAILURE;
39+
return $this->error('No stages available for comparison.');
4440
}
4541

4642
$diffService = new DiffService;
4743
$diffs = spin(fn () => $diffService->compare($vaults, $stages), 'Gathering secrets for comparison...');
4844

49-
if ($diffs->isEmpty()) {
50-
$this->info('No secrets found in any of the specified vault/stage combinations.');
51-
52-
return self::SUCCESS;
53-
}
54-
55-
$this->displayTable($diffs, $vaults, $stages, $diffService);
56-
57-
return self::SUCCESS;
45+
return $diffs->isNotEmpty()
46+
? $this->displayTable($diffs, $vaults, $stages, $diffService)
47+
: $this->info('No secrets found in any of the specified vault/stage combinations.');
5848
}
5949

6050
protected function getVaultsToCompare(): array
@@ -64,12 +54,12 @@ protected function getVaultsToCompare(): array
6454
if ($vaultOption) {
6555
$requestedVaults = array_map('trim', explode(',', $vaultOption));
6656

67-
$invalidVaults = array_diff($requestedVaults, Keep::available());
57+
$invalidVaults = array_diff($requestedVaults, Keep::getConfiguredVaults());
6858
if (! empty($invalidVaults)) {
6959
$this->warn('Warning: Unknown vaults specified: '.implode(', ', $invalidVaults));
7060
}
7161

72-
return array_intersect($requestedVaults, Keep::available());
62+
return array_intersect($requestedVaults, Keep::getConfiguredVaults());
7363
}
7464

7565
// Default to current/default vault only
@@ -83,7 +73,7 @@ protected function getStagesToCompare(): array
8373
if ($stagesOption) {
8474
// Parse comma-separated stages and validate them
8575
$requestedStages = array_map('trim', explode(',', $stagesOption));
86-
$availableStages = Keep::stages();
76+
$availableStages = Keep::getStages();
8777

8878
$invalidStages = array_diff($requestedStages, $availableStages);
8979
if (! empty($invalidStages)) {
@@ -94,7 +84,7 @@ protected function getStagesToCompare(): array
9484
}
9585

9686
// Default to all configured stages
97-
return Keep::stages();
87+
return Keep::getStages();
9888
}
9989

10090
protected function displayTable(Collection $diffs, array $vaults, array $stages, DiffService $diffService): bool
@@ -169,17 +159,17 @@ protected function parseContextsToVaultsAndStages(): array
169159
$stages = $contexts->pluck('stage')->unique()->values()->toArray();
170160

171161
// Validate vaults exist
172-
$invalidVaults = array_diff($vaults, Keep::available());
162+
$invalidVaults = array_diff($vaults, Keep::getConfiguredVaults());
173163
if (!empty($invalidVaults)) {
174164
$this->warn('Warning: Unknown vaults specified: ' . implode(', ', $invalidVaults));
175-
$vaults = array_intersect($vaults, Keep::available());
165+
$vaults = array_intersect($vaults, Keep::getConfiguredVaults());
176166
}
177167

178168
// Validate stages exist
179-
$invalidStages = array_diff($stages, Keep::stages());
169+
$invalidStages = array_diff($stages, Keep::getStages());
180170
if (!empty($invalidStages)) {
181171
$this->warn('Warning: Unknown stages specified: ' . implode(', ', $invalidStages));
182-
$stages = array_intersect($stages, Keep::stages());
172+
$stages = array_intersect($stages, Keep::getStages());
183173
}
184174

185175
return [$vaults, $stages];

src/KeepApplication.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public function __construct(protected KeepInstall $install)
4747
$this->add((new Commands\ListCommand()));
4848
$this->add((new Commands\CopyCommand()));
4949

50+
$this->add((new Commands\DiffCommand()));
51+
5052

5153
// TODO: Refactor these Laravel commands to Symfony Console:
5254
// - AbstractCommand.php (base class needs complete rewrite)

src/Services/DiffService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected function gatherSecrets(array $vaults, array $stages): array
5555
$vaultStageKey = "{$vaultName}.{$stage}";
5656

5757
try {
58-
$allSecrets[$vaultStageKey] = Keep::vault($vaultName)->forStage($stage)->list();
58+
$allSecrets[$vaultStageKey] = Keep::vault($vaultName, $stage)->list();
5959
} catch (\Exception $e) {
6060
// If we can't access a vault/stage combination, treat it as empty
6161
$allSecrets[$vaultStageKey] = new SecretCollection([]);

0 commit comments

Comments
 (0)