Skip to content

Commit 5e7eb91

Browse files
feat: add 'failed' status for delete operation
1 parent 6d0fcac commit 5e7eb91

4 files changed

Lines changed: 36 additions & 6 deletions

File tree

app/Commands/CacheKill.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class CacheKill extends Command
3535
* 'ready' — size known, awaiting user action
3636
* 'deleting' — rm -rf in progress
3737
* 'deleted' — rm -rf completed
38+
* 'failed' — rm -rf failed
3839
*
3940
* @var array<string, array{label: string, type: string, size: int|null, status: string, lastModified: int|null, order: int}>
4041
*/
@@ -287,6 +288,7 @@ protected function renderBadge(string $status, ?int $size): array
287288
'ready' => [' <fg=yellow>' . $this->formatSize($size) . '</>', ' ' . $this->formatSize($size)],
288289
'deleting' => [' <fg=yellow;options=bold>deleting...</>', ' deleting...'],
289290
'deleted' => [' <fg=green;options=bold>deleted ✓</>', ' deleted ✓'],
291+
'failed' => [' <fg=red;options=bold>delete failed</>', ' delete failed'],
290292
default => ['', ''],
291293
};
292294
}

app/Commands/CnKill.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class CnKill extends Command
3838
* 'ready' — size known, awaiting user action
3939
* 'deleting' — rm -rf in progress
4040
* 'deleted' — rm -rf completed
41+
* 'failed' — rm -rf failed
4142
*
4243
* @var array<string, array{project: string, size: int|null, status: string, type: string, lastModified: int|null, order: int}>
4344
*/
@@ -115,8 +116,16 @@ class CnKill extends Command
115116

116117
public function handle(): int
117118
{
118-
$searchPath = $this->argument('path') ?? getcwd();
119-
$this->searchPath = rtrim((string) realpath($searchPath), DIRECTORY_SEPARATOR);
119+
$searchPath = (string) ($this->argument('path') ?? getcwd());
120+
$resolvedSearchPath = realpath($searchPath);
121+
122+
if ($resolvedSearchPath === false || ! is_dir($resolvedSearchPath)) {
123+
$this->error('The provided path does not exist or is not a directory: ' . $searchPath);
124+
125+
return 1;
126+
}
127+
128+
$this->searchPath = $this->normalizePath($resolvedSearchPath);
120129

121130
// Cache options once — avoids repeated option() calls in the hot render loop
122131
$this->allMode = (bool) $this->option('all');
@@ -292,6 +301,13 @@ protected function startFindProcess(string $searchPath): void
292301
$this->findProc = ['proc' => $proc, 'pipe' => $pipes[1], 'buf' => ''];
293302
}
294303

304+
protected function normalizePath(string $path): string
305+
{
306+
$trimmed = rtrim($path, DIRECTORY_SEPARATOR);
307+
308+
return $trimmed === '' ? DIRECTORY_SEPARATOR : $trimmed;
309+
}
310+
295311
/**
296312
* Read whatever is available from find's stdout and register new dirs.
297313
*/
@@ -564,6 +580,7 @@ protected function renderBadge(string $status, ?int $size): array
564580
'ready' => ['<fg=yellow>' . $this->formatSize($size) . '</>', $this->formatSize($size)],
565581
'deleting' => ['<fg=yellow;options=bold>deleting...</>', 'deleting...'],
566582
'deleted' => ['<fg=green;options=bold>deleted ✓</>', 'deleted ✓'],
583+
'failed' => ['<fg=red;options=bold>delete failed</>', 'delete failed'],
567584
default => ['', ''],
568585
};
569586
}

app/Commands/Concerns/TuiCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ protected function startDeleteProcess(string $dir): void
202202
$proc = proc_open(['rm', '-rf', $dir], $descriptors, $pipes);
203203

204204
if (! is_resource($proc)) {
205-
$this->state[$dir]['status'] = 'deleted';
205+
$this->state[$dir]['status'] = 'failed';
206206

207207
return;
208208
}
@@ -226,10 +226,10 @@ protected function pollDeleteProcesses(): void
226226
continue;
227227
}
228228

229-
proc_close($proc);
229+
$exitCode = proc_close($proc);
230230
unset($this->deleteProcs[$dir]);
231231

232-
$this->state[$dir]['status'] = 'deleted';
232+
$this->state[$dir]['status'] = $exitCode === 0 ? 'deleted' : 'failed';
233233
}
234234
}
235235

@@ -309,7 +309,7 @@ protected function handleInput(): void
309309
$dir = $visibleDirs[$this->cursor];
310310
$status = $this->state[$dir]['status'];
311311

312-
if ($status === 'ready') {
312+
if ($status === 'ready' || $status === 'failed') {
313313
$this->state[$dir]['status'] = 'deleting';
314314
$this->startDeleteProcess($dir);
315315
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
it('fails when process path does not exist', function () {
6+
$missingPath = '/tmp/cnkill-path-that-does-not-exist';
7+
8+
$this->artisan('process ' . $missingPath)
9+
->expectsOutput('The provided path does not exist or is not a directory: ' . $missingPath)
10+
->assertExitCode(1);
11+
});

0 commit comments

Comments
 (0)