Skip to content

Commit c484c34

Browse files
committed
force replace
1 parent 8105a1f commit c484c34

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

src/Core/Crypt/Command/CryptSecretCommand.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ public function configure(Command $command): void
7070
'Replace APP_SECRET env variable.',
7171
false,
7272
);
73+
74+
$command->addOption(
75+
'force',
76+
'f',
77+
InputOption::VALUE_NONE,
78+
'Force replace APP_SECRET env variable.',
79+
);
7380
}
7481

7582
/**
@@ -84,6 +91,7 @@ public function execute(IOInterface $io): int
8491
{
8592
$length = $io->getArgument('length');
8693
$replace = $io->getOption('replace');
94+
$force = $io->getOption('force');
8795

8896
$encode = $io->getOption('encode');
8997

@@ -109,7 +117,18 @@ public function execute(IOInterface $io): int
109117

110118
if (is_file($replace)) {
111119
$content = file_get_contents($replace);
112-
$content = preg_replace('/^APP_SECRET=.*$/m', 'APP_SECRET=' . $secret, $content);
120+
121+
if (!preg_match('/^APP_SECRET=.*$/m', $content)) {
122+
$content .= "\nAPP_SECRET=" . $secret . "\n";
123+
} else {
124+
if (!$force && !$io->askConfirmation("APP_SECRET already exists in file: <info>$replace</info>, do you want to replace it? [N/y] ", false)) {
125+
$io->writeln('User canceled.');
126+
return 0;
127+
}
128+
129+
$content = preg_replace('/^APP_SECRET=.*$/m', 'APP_SECRET=' . $secret, $content);
130+
}
131+
113132
file_put_contents($replace, $content);
114133

115134
$io->writeln('Replace APP_SECRET in file: ' . $replace);

0 commit comments

Comments
 (0)