forked from brefphp/bref
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bref
executable file
·133 lines (119 loc) · 4.09 KB
/
bref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env php
<?php
declare(strict_types=1);
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
} elseif (file_exists(__DIR__ . '/../autoload.php')) {
/** @noinspection PhpIncludeInspection */
require_once __DIR__ . '/../autoload.php';
} else {
/** @noinspection PhpIncludeInspection */
require_once __DIR__ . '/../../autoload.php';
}
telemetry();
switch ($argv[1] ?? '') {
case 'init':
require_once __DIR__ . '/src/Cli/init.php';
init();
break;
case 'cli':
cliWarning();
error('Since Bref 2.0, the "bref cli" command has been moved. Read https://bref.sh/docs/runtimes/console#usage');
case 'layers':
cliWarning();
echo "Bref layer ARNs can be found here: https://runtimes.bref.sh/\n\n";
echo "If you are using Serverless Framework, you can also run the 'serverless bref:layers' command.\n";
exit(1);
case 'local':
cliWarning();
error('Since Bref 2.0, the "bref local" command has been moved. Read https://bref.sh/docs/local-development/event-driven-functions\n');
case 'dashboard':
cliWarning();
echo "The Bref Dashboard is now available as a desktop application.\n\n";
echo "Check out https://dashboard.bref.sh/\n";
exit(1);
case '':
help();
break;
default:
error('Unknown command');
}
function help()
{
echo "Bref initialization command\n\n";
echo "Run 'bref init' to get started!\n";
exit(0);
}
/**
* @return never-return
*/
function error(string $message): void
{
echo "\033[31m⨯ $message\033[0m" . PHP_EOL;
exit(1);
}
function warning(string $message): void
{
echo "\033[33m$message\033[0m" . PHP_EOL;
}
function success(string $message): void
{
echo green($message) . PHP_EOL;
}
function green(string $message): string
{
return "\033[32m$message\033[0m";
}
function cliWarning(): void
{
warning("Warning: the 'vendor/bin/bref' CLI has been removed in Bref 2." . PHP_EOL);
}
/**
* Bref telemetry to estimate the number of users and which commands are most used.
*
* The data sent is anonymous, and sent over UDP.
* Unlike TCP, UDP does not check that the message correctly arrived to the server.
* It doesn't even establish a connection: the data is sent over the network and the code moves on to the next line.
* That means that UDP is extremely fast (150 micro-seconds) and will not impact the CLI.
* It can be disabled by setting the `SLS_TELEMETRY_DISABLED` environment variable to `1`.
*
* About UDP: https://en.wikipedia.org/wiki/User_Datagram_Protocol
*/
function telemetry(): void
{
global $argv;
// Respect the serverless framework env variable
if ($_SERVER['SLS_TELEMETRY_DISABLED'] ?? false) {
return;
}
// Support cases where the sockets extension is not installed
if (! function_exists('socket_create')) {
return;
}
// Read `~/.serverlessrc` if it exists
$userConfigPath = $_SERVER['HOME'] . '/.serverlessrc';
if (file_exists($userConfigPath)) {
$userConfig = json_decode(file_get_contents($userConfigPath), true, 512, JSON_THROW_ON_ERROR);
} else {
$userConfig = [];
}
// Check if we are running in CI
$ciVars = ['CI', 'CONTINUOUS_INTEGRATION', 'BUILD_NUMBER', 'CI_APP_ID', 'CI_NAME', 'RUN_ID', 'BUILD_ID'];
$ci = array_reduce($ciVars, function ($carry, $item) {
return $carry || (isset($_SERVER[$item]) && $_SERVER[$item]);
}, false);
$message = json_encode([
'cli' => 'vendor/bin/bref',
'v' => 2, // Bref version
'c' => $argv[1] ?? '',
'ci' => $ci,
// anonymous user ID created by the Serverless Framework
'uid' => $userConfig['frameworkId'] ?? '',
], JSON_THROW_ON_ERROR);
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
// This IP address is the Bref server.
// If this server is down or unreachable, there should be no difference in overhead
// or execution time.
socket_sendto($sock, $message, strlen($message), 0, '108.128.197.71', 8888);
socket_close($sock);
}