Skip to content

Commit 922513d

Browse files
committed
feat(CommitCommand): Add config option
- Add `config` option to `CommitCommand` - Specify config file for `CommitCommand` - Replace config from specified file
1 parent 5f63c68 commit 922513d

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

app/Commands/CommitCommand.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
use Illuminate\Support\Stringable;
2121
use LaravelZero\Framework\Commands\Command;
2222
use Symfony\Component\Console\Input\InputArgument;
23+
use Symfony\Component\Console\Input\InputInterface;
2324
use Symfony\Component\Console\Input\InputOption;
25+
use Symfony\Component\Console\Output\OutputInterface;
2426
use Symfony\Component\Process\Process;
2527

2628
class CommitCommand extends Command
@@ -57,16 +59,22 @@ protected function configure()
5759
new InputArgument('path', InputArgument::OPTIONAL, 'The working directory', $this->configManager::localPath('')),
5860
new InputOption('commit-options', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Append options for the `git commit` command', $this->configManager->get('commit_options')),
5961
new InputOption('diff-options', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Append options for the `git diff` command', $this->configManager->get('diff_options')),
62+
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Specify config file'),
6063
new InputOption('generator', 'g', InputOption::VALUE_REQUIRED, 'Specify generator name', $this->configManager->get('generator')),
6164
new InputOption('num', null, InputOption::VALUE_REQUIRED, 'Specify number of generated messages', $this->configManager->get('num')),
6265
new InputOption('prompt', 'p', InputOption::VALUE_REQUIRED, 'Specify prompt name of messages generated', $this->configManager->get('prompt')),
6366
new InputOption('no-edit', null, InputOption::VALUE_NONE, 'Force no edit mode'),
6467
]);
6568
}
6669

70+
protected function initialize(InputInterface $input, OutputInterface $output)
71+
{
72+
$config = $this->option('config') and $this->configManager->replaceFrom($config);
73+
}
74+
6775
public function handle()
6876
{
69-
$this->task(' Checking run environment', function () use (&$stagedDiff) {
77+
$this->task('1. Checking run environment', function () use (&$stagedDiff) {
7078
$isInsideWorkTree = $this->createProcess('git rev-parse --is-inside-work-tree')
7179
->mustRun()
7280
->getOutput();
@@ -85,7 +93,7 @@ public function handle()
8593
}
8694
}, 'checking...');
8795

88-
$this->task(' Generating commit messages', function () use (&$messages, $stagedDiff) {
96+
$this->task('2. Generating commit messages', function () use (&$messages, $stagedDiff) {
8997
$generator = $this->laravel->get(GeneratorManager::class)->driver($this->option('generator'));
9098
$messages = $generator->generate($this->getPromptOfAI($stagedDiff));
9199
if (\str($messages)->isEmpty()) {
@@ -101,15 +109,15 @@ public function handle()
101109
$this->line('');
102110
}, 'generating...');
103111

104-
$this->task(' Choosing commit message', function () use ($messages, &$message) {
112+
$this->task('3. Choosing commit message', function () use ($messages, &$message) {
105113
$messages = collect(json_decode($messages, true));
106114
$chosenSubject = $this->choice('Please choice a commit message', $messages->pluck('subject', 'id')->all());
107115
$message = $messages->first(function ($message) use ($chosenSubject) {
108116
return $message['subject'] === $chosenSubject;
109117
});
110118
}, 'choosing...');
111119

112-
$this->task(' Committing message', function () use ($message) {
120+
$this->task('4. Committing message', function () use ($message) {
113121
$this->createProcess($this->getCommitCommand($message))
114122
->setTty(true)
115123
->setTimeout(null)

0 commit comments

Comments
 (0)