Skip to content

Commit c2cc5ef

Browse files
authored
feat: Improve publish command (#392)
* feat: Improve publish command Ask for DSN * feat: Remove use of setDsn
1 parent 99ae5c3 commit c2cc5ef

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

src/Sentry/Laravel/PublishConfigCommand.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Sentry\Laravel;
44

55
use Illuminate\Console\Command;
6+
use Sentry\Dsn;
67

78
class PublishConfigCommand extends Command
89
{
@@ -18,7 +19,7 @@ class PublishConfigCommand extends Command
1819
*
1920
* @var string
2021
*/
21-
protected $signature = 'sentry:publish';
22+
protected $signature = 'sentry:publish {--dsn=}';
2223

2324
/**
2425
* The console command description.
@@ -39,23 +40,53 @@ public function handle()
3940
'--provider' => 'Sentry\Laravel\ServiceProvider'
4041
]);
4142

43+
$args = [];
44+
45+
$dsn = $this->option('dsn');
46+
47+
if (!$this->isKeySet('SENTRY_LARAVEL_DSN')) {
48+
while (empty($dsn)) {
49+
$this->info('');
50+
$this->question('[Sentry] Please paste the DSN here');
51+
$dsn = $this->ask('DSN');
52+
// In case someone copies it with SENTRY_LARAVEL_DSN=
53+
$dsn = str_replace('SENTRY_LARAVEL_DSN=', '', $dsn);
54+
try {
55+
$dsnObj = Dsn::createFromString($dsn);
56+
} catch (\Exception $e) {
57+
// Not a valid DSN do it again
58+
$this->error('[Sentry] The DSN is not valid, please make sure to paste a valid DSN');
59+
$dsn = '';
60+
continue;
61+
}
62+
};
63+
64+
$this->setEnvironmentValue(['SENTRY_LARAVEL_DSN' => $dsn]);
65+
$args = array_merge($args, ['--dsn' => $dsn]);
66+
}
67+
4268
if ($this->confirm('Enable Performance Monitoring?', true)) {
4369
$this->setEnvironmentValue(['SENTRY_TRACES_SAMPLE_RATE' => 1.0]);
4470

4571
$this->info('[Sentry] Added `SENTRY_TRACES_SAMPLE_RATE=1` to your .env file.');
4672

4773
$testCommandPrompt = 'Want to send a test Event & Transaction?';
48-
$args = ['--transaction' => true];
74+
$args = array_merge($args, ['--transaction' => true]);
4975
} else {
5076
$testCommandPrompt = 'Want to send a test Event?';
51-
$args = [];
5277
}
5378

5479
if ($this->confirm($testCommandPrompt, true)) {
5580
$this->call('sentry:test', $args);
5681
}
5782
}
5883

84+
public function isKeySet(string $key)
85+
{
86+
$envFile = app()->environmentFilePath();
87+
return strpos(file_get_contents($envFile), $key) !== false;
88+
}
89+
5990
public function setEnvironmentValue(array $values)
6091
{
6192
$envFile = app()->environmentFilePath();

src/Sentry/Laravel/TestCommand.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Exception;
66
use Illuminate\Console\Command;
7+
use Sentry\ClientBuilder;
8+
use Sentry\State\Hub;
79
use Sentry\Tracing\SpanContext;
810
use Sentry\Tracing\TransactionContext;
911

@@ -21,7 +23,7 @@ class TestCommand extends Command
2123
*
2224
* @var string
2325
*/
24-
protected $signature = 'sentry:test {--transaction}';
26+
protected $signature = 'sentry:test {--transaction} {--dsn=}';
2527

2628
/**
2729
* The console command description.
@@ -44,6 +46,10 @@ public function handle()
4446
/** @var \Sentry\State\Hub $hub */
4547
$hub = app('sentry');
4648

49+
if ($this->option('dsn')) {
50+
$hub = new Hub(ClientBuilder::create(['dsn' => $this->option('dsn')])->getClient());
51+
}
52+
4753
if ($hub->getClient()->getOptions()->getDsn()) {
4854
$this->info('[Sentry] DSN discovered!');
4955
} else {
@@ -70,23 +76,22 @@ public function handle()
7076

7177
$ex = $this->generateTestException('command name', ['foo' => 'bar']);
7278

73-
$hub->captureException($ex);
79+
$eventId = $hub->captureException($ex);
7480

7581
$this->info('[Sentry] Sending test Event');
7682

7783
$span1->finish();
7884
$result = $transaction->finish();
7985
if ($result) {
80-
$this->info('[Sentry] Sending test Transaction');
86+
$this->info("[Sentry] Transaction sent: {$result}");
8187
}
8288

83-
$lastEventId = $hub->getLastEventId();
8489

85-
if (!$lastEventId) {
90+
if (!$eventId) {
8691
$this->error('[Sentry] There was an error sending the test event.');
8792
$this->error('[Sentry] Please check if you DSN is set properly in your config or `.env` as `SENTRY_LARAVEL_DSN`.');
8893
} else {
89-
$this->info("[Sentry] Event sent with ID: {$lastEventId}");
94+
$this->info("[Sentry] Event sent with ID: {$eventId}");
9095
}
9196
} catch (Exception $e) {
9297
$this->error("[Sentry] {$e->getMessage()}");

0 commit comments

Comments
 (0)