Skip to content

Commit 6ad5766

Browse files
committed
Improve UnlinkCommand validation and error handling
1 parent 9128fe0 commit 6ad5766

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

packages/kibble/src/Commands/UnlinkCommand.php

+19-9
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,25 @@ class UnlinkCommand extends Command
1515

1616
public function handle(): int
1717
{
18+
app(KibbleGitIgnore::class)();
19+
20+
if (! file_exists('kibble.json')) {
21+
$this->error('kibble.json file not found');
22+
23+
return Command::FAILURE;
24+
}
25+
1826
$repository = $this->argument('repository');
27+
$kibble = json_decode(file_get_contents('kibble.json'), true);
28+
29+
if (! isset($kibble[$repository])) {
30+
$this->error("{$repository} repository not found in kibble.json");
31+
32+
return Command::FAILURE;
33+
}
1934

2035
$composer_json = file_get_contents('composer.json');
2136
$trailing_newline = str_ends_with($composer_json, "\n");
22-
2337
$composer = json_decode($composer_json, true);
2438

2539
if (! isset($composer['repositories'][$repository])) {
@@ -28,15 +42,11 @@ public function handle(): int
2842
return Command::FAILURE;
2943
}
3044

31-
app(KibbleGitIgnore::class)();
45+
if ($composer['repositories'][$repository] !== $kibble[$repository]) {
46+
$this->error("{$repository} repository in composer.json does not match {$repository} repository in kibble.json");
3247

33-
file_put_contents(
34-
filename: 'kibble.json',
35-
data: json_encode(
36-
value: [$repository => $composer['repositories'][$repository]],
37-
flags: JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
38-
).($trailing_newline ? "\n" : '')
39-
);
48+
return Command::FAILURE;
49+
}
4050

4151
unset($composer['repositories'][$repository]);
4252

0 commit comments

Comments
 (0)