Skip to content

Commit 70c5551

Browse files
Refactor saveTemplateCommand to return success status
Changed saveTemplateCommand to return a boolean indicating success or failure instead of void. Updated relevant logic to handle failures gracefully, including error messages and return codes. Incremented version to 1.2.1 in preparation for these updates.
1 parent a624ba6 commit 70c5551

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

bin/laravelfs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if (file_exists(__DIR__.'/../../../autoload.php')) {
99
}
1010

1111
// Define our own version and the Laravel Installer version we aim to match.
12-
$laravelFSVersion = '1.2.0';
12+
$laravelFSVersion = '1.2.1';
1313
$laravelInstallerVersion = '5.13.0';
1414

1515
// Compose the displayed version string.

src/NewCommand.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
351351
}
352352

353353
// Save the template globally
354-
$this->saveTemplateCommand($templateName, $templateDescription, $templateCommand);
354+
if (!$this->saveTemplateCommand($templateName, $templateDescription, $templateCommand)) {
355+
error('Failed to save the template.');
356+
return Command::FAILURE;
357+
}
355358

356359
$output->writeln(" <bg=blue;fg=white> INFO </> Template <options=bold>[$templateName]</> created successfully.");
357360
$output->writeln(" <fg=gray>➜</> You can now use this template by running <options=bold>laravelfs use $templateName</>");
@@ -1202,7 +1205,7 @@ private function createTemplateCommand(InputInterface $input): string
12021205
return implode(' ', $commandParts);
12031206
}
12041207

1205-
protected function saveTemplateCommand(mixed $templateName, mixed $templateDescription, string $templateCommand): void
1208+
protected function saveTemplateCommand(mixed $templateName, mixed $templateDescription, string $templateCommand): bool
12061209
{
12071210
// Get the global config path for storing templates
12081211
$configPath = $this->getGlobalTemplatesPath();
@@ -1234,6 +1237,15 @@ protected function saveTemplateCommand(mixed $templateName, mixed $templateDescr
12341237
'description' => $templateDescription??"",
12351238
'command' => $templateCommand,
12361239
];
1237-
file_put_contents($configPath, json_encode($templatesConfig, JSON_PRETTY_PRINT));
1240+
$done = file_put_contents($configPath, json_encode($templatesConfig, JSON_PRETTY_PRINT)) !== false;
1241+
if (!$done) {
1242+
if (windows_os()) {
1243+
error('Failed to save the template. Please check the permissions of the directory.');
1244+
} else {
1245+
error('Failed to save the template. Please check the permissions of the file or use sudo.');
1246+
}
1247+
}
1248+
1249+
return $done;
12381250
}
12391251
}

tests/Unit/NewTemplateCommandTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
// Create a dummy NewTemplateCommand that overrides saveTemplateCommand() to capture the saved template.
1010
$this->command = new class extends NewTemplateCommand {
1111
public array|null $savedTemplate = null;
12-
public function saveTemplateCommand($templateName, $templateDescription, $templateCommand): void {
12+
public function saveTemplateCommand($templateName, $templateDescription, $templateCommand): bool {
1313
$this->savedTemplate = [
1414
'name' => $templateName,
1515
'description' => $templateDescription,
1616
'command' => $templateCommand,
1717
];
18+
return true;
1819
}
1920
};
2021

0 commit comments

Comments
 (0)