2023-03-15 20:40:49 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
2023-03-29 21:39:36 +08:00
|
|
|
use SPC\exception\WrongUsageException;
|
2023-03-26 22:27:51 +08:00
|
|
|
use SPC\util\UnixShell;
|
2023-03-15 20:40:49 +08:00
|
|
|
use ZM\Logger\ConsoleLogger;
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* Judge if an array is an associative array
|
2023-03-15 20:40:49 +08:00
|
|
|
*/
|
2023-08-20 19:51:45 +08:00
|
|
|
function is_assoc_array(mixed $array): bool
|
2023-03-15 20:40:49 +08:00
|
|
|
{
|
|
|
|
|
return is_array($array) && (!empty($array) && array_keys($array) !== range(0, count($array) - 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* Return a logger instance
|
2023-03-15 20:40:49 +08:00
|
|
|
*/
|
|
|
|
|
function logger(): LoggerInterface
|
|
|
|
|
{
|
|
|
|
|
global $ob_logger;
|
|
|
|
|
if ($ob_logger === null) {
|
|
|
|
|
return new ConsoleLogger();
|
|
|
|
|
}
|
|
|
|
|
return $ob_logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* Transfer architecture name to gnu triplet
|
|
|
|
|
*
|
2023-03-29 21:39:36 +08:00
|
|
|
* @throws WrongUsageException
|
2023-03-15 20:40:49 +08:00
|
|
|
*/
|
|
|
|
|
function arch2gnu(string $arch): string
|
|
|
|
|
{
|
|
|
|
|
$arch = strtolower($arch);
|
|
|
|
|
return match ($arch) {
|
|
|
|
|
'x86_64', 'x64', 'amd64' => 'x86_64',
|
|
|
|
|
'arm64', 'aarch64' => 'aarch64',
|
2023-03-29 21:39:36 +08:00
|
|
|
default => throw new WrongUsageException('Not support arch: ' . $arch),
|
2023-03-15 20:40:49 +08:00
|
|
|
// 'armv7' => 'arm',
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function quote(string $str, string $quote = '"'): string
|
|
|
|
|
{
|
|
|
|
|
return $quote . $str . $quote;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 21:39:36 +08:00
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* Get Family name of current OS
|
2023-03-29 21:39:36 +08:00
|
|
|
*
|
|
|
|
|
* @throws WrongUsageException
|
|
|
|
|
*/
|
2023-03-15 20:40:49 +08:00
|
|
|
function osfamily2dir(): string
|
|
|
|
|
{
|
|
|
|
|
return match (PHP_OS_FAMILY) {
|
|
|
|
|
/* @phpstan-ignore-next-line */
|
|
|
|
|
'Windows', 'WINNT', 'Cygwin' => 'windows',
|
|
|
|
|
'Darwin' => 'macos',
|
|
|
|
|
'Linux' => 'linux',
|
2023-10-15 13:07:13 +08:00
|
|
|
'BSD' => 'freebsd',
|
2023-03-29 21:39:36 +08:00
|
|
|
default => throw new WrongUsageException('Not support os: ' . PHP_OS_FAMILY),
|
2023-03-15 20:40:49 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* Execute the shell command, and the output will be directly printed in the terminal. If there is an error, an exception will be thrown
|
2023-05-10 02:04:08 +08:00
|
|
|
*
|
2023-03-15 20:40:49 +08:00
|
|
|
* @throws \SPC\exception\RuntimeException
|
|
|
|
|
*/
|
|
|
|
|
function f_passthru(string $cmd): ?bool
|
|
|
|
|
{
|
|
|
|
|
$danger = false;
|
|
|
|
|
foreach (DANGER_CMD as $danger_cmd) {
|
|
|
|
|
if (str_starts_with($cmd, $danger_cmd . ' ')) {
|
|
|
|
|
$danger = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($danger) {
|
|
|
|
|
logger()->notice('Running dangerous command: ' . $cmd);
|
|
|
|
|
} else {
|
|
|
|
|
logger()->debug('Running command with direct output: ' . $cmd);
|
|
|
|
|
}
|
|
|
|
|
$ret = passthru($cmd, $code);
|
|
|
|
|
if ($code !== 0) {
|
|
|
|
|
throw new \SPC\exception\RuntimeException('Command run failed with code[' . $code . ']: ' . $cmd, $code);
|
|
|
|
|
}
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 02:04:08 +08:00
|
|
|
/**
|
2023-08-20 19:51:45 +08:00
|
|
|
* Execute a command, return the output and result code
|
2023-05-10 02:04:08 +08:00
|
|
|
*/
|
2023-08-20 19:51:45 +08:00
|
|
|
function f_exec(string $command, mixed &$output, mixed &$result_code): bool|string
|
2023-03-15 20:40:49 +08:00
|
|
|
{
|
|
|
|
|
logger()->debug('Running command (no output) : ' . $command);
|
|
|
|
|
return exec($command, $output, $result_code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function f_mkdir(string $directory, int $permissions = 0777, bool $recursive = false): bool
|
|
|
|
|
{
|
|
|
|
|
if (file_exists($directory)) {
|
|
|
|
|
logger()->debug("Dir {$directory} already exists, ignored");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
logger()->debug('Making new directory ' . ($recursive ? 'recursive' : '') . ': ' . $directory);
|
|
|
|
|
return mkdir($directory, $permissions, $recursive);
|
|
|
|
|
}
|
2023-03-26 22:27:51 +08:00
|
|
|
|
2023-10-22 22:14:11 +08:00
|
|
|
function f_putenv(string $env): bool
|
|
|
|
|
{
|
|
|
|
|
logger()->debug('Setting env: ' . $env);
|
|
|
|
|
return putenv($env);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-22 21:23:12 +08:00
|
|
|
function shell(?bool $debug = null): UnixShell
|
2023-03-26 22:27:51 +08:00
|
|
|
{
|
2023-04-22 21:23:12 +08:00
|
|
|
return new UnixShell($debug);
|
2023-03-26 22:27:51 +08:00
|
|
|
}
|